In multilevel inheritance constructors will be executed
from the .... class to ... class
Answer Posted / ps
Constructors are always executed from the parent to child
ie in the example below:
#include <iostream>
using namespace std;
class base1
{
public:
base1()
{
cout<<"In constructor of base1"<<endl;
}
~base1()
{
cout<<"In destructor of base1"<<endl;
}
};
class base2:public base1
{
public:
base2()
{
cout<<"In constructor of base2"<<endl;
}
~base2()
{
cout<<"In destructor of base2"<<endl;
}
};
class derived :public base2
{
public:
derived()
{
cout<<"In constructor of derived"<<endl;
}
~derived()
{
cout<<"In destructor of derived"<<endl;
}
};
void main()
{
base1 b1;
base2 b2;
derived d1;
}
o/p:
In constructor of base1 --- for object b1
In constructor of base1 --- for object b2
In constructor of base2-----
In constructor of base1-for object d1
In constructor of base2
In constructor of derived
Is This Answer Correct ? | 19 Yes | 6 No |
Post New Answer View All Answers
What is encapsulation in oop?
Can destructor be overloaded?
What are properties in oop?
Will I be able to get a picture in D drive to the c++ program? If so, help me out?
What is inheritance in oop?
What is encapsulation and abstraction? How are they implemented in C++?
What are the 5 oop principles?
What does <> mean pseudocode?
What is difference between multiple inheritance and multilevel inheritance?
What is a class in oop?
Can enum be null?
What is encapsulation oop?
What is the problem with multiple inheritance?
Can we have inheritance without polymorphism?
What is multilevel inheritance in oop?