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 are the ways in which a constructors can be called?

1816


what is difference between class template and template class?

2425


hi, this is raju,iam studying b.tech 2nd year,iam want know about group1 and group2 details, and we can studying without going to any instutions? please help me.

1760


Why is abstraction used?

860


How do you define a class in oop?

880


what are the different types of qualifier in java?

2041


What is for loop and its syntax?

823


How do you answer polymorphism?

814


What is a superclass in oop?

931


What is cohesion in oop?

864


what are the realtime excercises in C++?

2570


What is the real time example of inheritance?

885


How do you use inheritance in unity?

823


write a code for this. serial_number contained in the header of the file will be read , if this serial number is less than a previous serial number within a successfully processed file, or is the same as another serial number within a successfully processed file, or if the field contains anything other than 7 digits, then the file must error with the reason ‘Invalid SERIAL_NUMBER’.

2008


Where is pseudocode used?

765