struggling with constructors in c++.. why in a code below base class default constructor is called despite of constructor with parameter being already executed? In D class constructor with parameter D:(int n) : B(n) constructor is called explicitly first but after that default constructor B() is still executed before anything else and I couldnt figure out why? Thank you for any help.
Code:
class B
{
public:
B();
B(int n);
};
B::B()
{
cout << "B::B()\n";
}
B::B(int n)
{
cout << "B::B(" << n << ")\n";
}
class D : public B
{
public:
D();
D(int n);
private:
B b;
};
D::D()
{
cout << "D::D()\n";
}
D::D(int n) : B(n)
{
b = B(-n);
cout << "D::D(" << n << ")\n";
}
int main()
{
D d(3);
return 0;
}
output is:
B::B(3)
B::B()
B::B(-3)
D:(3)
sorry about smiley above, should be :: there