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


Please Help Members By Posting Answers For Below Questions

What is encapsulation in oop?

787


Can destructor be overloaded?

800


What are properties in oop?

810


Will I be able to get a picture in D drive to the c++ program? If so, help me out?

1879


What is inheritance in oop?

801


What is encapsulation and abstraction? How are they implemented in C++?

849


What are the 5 oop principles?

844


What does <> mean pseudocode?

842


What is difference between multiple inheritance and multilevel inheritance?

880


What is a class in oop?

778


Can enum be null?

768


What is encapsulation oop?

798


What is the problem with multiple inheritance?

831


Can we have inheritance without polymorphism?

795


What is multilevel inheritance in oop?

769