Explain the order of execution of constructors in multilevel inheritance using an example.
The order of execution of constructors in multilevel inheritance is; base class's constructor are executed first followed by execution of derived classes constructors. Given a base class A, class B that inherits from A, and class C that inherits from B:
Example
#include <iostream>
using namespace std;
//define class A
class A{
public:
//define a default constructor
A(){
cout<<"I am in class A"<<endl;
}
};
//define class B
class B: public A{
public:
//define a default constructor
B(){
cout<<"I am in class B"<<endl;
}
};
//define class C
class C: public B{
public:
//define a default constructor
C(){
cout<<"I am in class C"<<endl;
}
};
int main()
{
C c1;
return 0;
}
Output:
I am in class A
I am in class B
I am in class C
Comments
Leave a comment