So i'm doing a beginner c++ course and i'm confused about something regarding parenthesis and curly brackets..
#include "Savings_Account.h"
Savings_Account::Savings_Account(double balance, double int_rate)
: Account(balance), int_rate{int_rate} {
}
Savings_Account::Savings_Account()
: Savings_Account{0.0, 0.0} {
}
Note:Savings_account is a class derived from Account class
In the second line of code where the tutor writes :Account(balance), he says he is delegating to the account class's overloaded constructor which kinda makes sense but...there's more
class Derived : public Base {
private:
int doubled_value;
public:
Derived() :
Base {} {
cout << "Derived no-args constructor " << endl;
}
Derived(int x)
: Base{x} , doubled_value {x * 2} {
cout << "int Derived constructor" << endl;
}
Derived(const Derived &other)
: Base(other), doubled_value {other.doubled_value} {
cout << "Derived copy constructor" << endl;
}
In this code above which is from a different video that is part of the course, at line 10 where he writes :Base{x} he says he is delegating to the Base class's constructor which is where i'm confused cuz he used curly brackets and in line 14 where he writes :Base(other) he says he is delegating to the base class's copy constructor and he used parenthesis here so i'm getting quite confused.... Does using parenthesis or curly brackets matter? And How does the compiler know when he is referring to the copy constructor or normal args constructor?
And this is the base class in case u need it
private:
int value;
public:
Base()
: value {0} {
cout << "Base no-args constructor" << endl;
}
Base(int x)
: value {x} {
cout << "int Base constructor" << endl;
}
Base(const Base &other)
: value {other.value} {
cout << "Base copy constructor" << endl;
}
Base &operator=(const Base &rhs) {
cout << "Base operator=" << endl;
if (this == &rhs)
return *this;
value = rhs.value;
return *this;
}
~Base(){ cout << "Base destructor" << endl; }
};
This is the derived class
private:
int doubled_value;
public:
Derived() :
Base {} {
cout << "Derived no-args constructor " << endl;
}
Derived(int x)
: Base{x} , doubled_value {x * 2} {
cout << "int Derived constructor" << endl;
}
Derived(const Derived &other)
: Base(other), doubled_value {other.doubled_value} {
cout << "Derived copy constructor" << endl;
}
Derived &operator=(const Derived &rhs) {
cout << "Derived operator=" << endl;
if (this == &rhs)
return *this;
Base::operator=(rhs);
doubled_value = rhs.doubled_value;
return *this;
}
~Derived(){ cout << "Derived destructor " << endl; }
};