I have always thought that base class constructors/destructors/friends are not inherited by derived class. This link confirms this : http://www.geeksforgeeks.org/g-fact-4/.
I was also aware that we can write base class constructors in the initialization list of derived class initializer list.
Having said that: I tried to check my skills today about it. But I failed to guess the output of this program.
#include<iostream> class A { int x, y; public: A(int a = 0, int b = 0) : x(a), y(b) { std::cout << "A ctor called" << std::endl; } void print_A() { std::cout << "x = " << x << std::endl; std::cout << "y = " << y << std::endl; } }; class B : public A { int z; public: // I knew that A member can be initilized like this. B(int a = 0, int b = 0, int c = 0) : z(a), A(b, c) { std::cout << "C ctor called" << std::endl; // I was not aware about that. A(b, c); } void print_B() { std::cout << "z = " << z << std::endl; } }; int main() { B b(1, 2, 3); b.print_A(); b.print_B(); } Output :
A ctor called C ctor called A ctor called x = 2 y = 3 z = 1 Couple of questions:
If constructors/desctructors/friends are not inherited from base, how can class 'B' is able to access constructor of class 'A' here.
How come you get this output? How come two constructors of 'A' have been called.