Q. Which class constructor is called in the following C++ code?
Code:
#include<iostream>
using namespace std;
class A {
public:
A()
{ cout << " The constructor of class A is called." << endl; }
};
class B {
public:
B()
{ cout << " The constructor of class B is called." << endl; }
};
class C: public A, public B {
public:
C()
{ cout << " The constructor of class C is called." << endl; }
};
int main()
{
C c;
return 0;
}
β
Correct Answer: (C)
Class A, B and C
Explanation: In case of multiple inheritance, the constructors of parent classes are always called in the order of inheritance from left to right and the destructors are called in reverse order.