C++ output type question - Derived constructor called

Q.  Predict the output for the following program
#include< iostream>

                using namesp std;
       class B1 {
public:
B1()
{ cout << " B1's constructor called" << endl; }
};

class B2 {
public:
B2()
{ cout << "B2's constructor called" << endl; }
};

class Derived: public B1, public B2 {
public:
Derived()
{ cout << "Derived's constructor called" << endl; }
};

int main()
{
Derived d;
return 0;
}

- Published on 16 Jun 15

a. B1's constructor called,B2's constructor called
b. B2's constructor called,Derived constructor called
c. B1's constructor called,B2's constructor called,Derived constructor called
d. Compiler error

ANSWER: B1's constructor called,B2's constructor called,Derived constructor called
 
When there is multiple inheritance, constructors of the base classes are always called in the derived order ie from left to right and destructors are called in the reverse order.

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)