explain the order of execution of constructors in multilevel inheritance using an example . in c++
Answers
example: part :1
#include <iostream>
using namespace std;
class Main1
{
public:
Main1()
{
cout << "\nConstructor of Main1" << endl;
}
~Main1()
{
cout << " \nDestructor of Main1" << endl;
}
};
class Main2
{
public:
Main2()
{
cout << "\nConstructor of Main2" << endl;
}
~Main2()
{
cout << " \nDestructor of Main2" << endl;
}
};
class Child : public Main1, public Main2
{
public:
Child()
{
cout << "\nConstructor of Child" << endl;
}
~ Child()
{
cout << "\nDestructor of Child" << endl;
}
};
int main()
{
Child obj;
return 0;
}
example part 2
#include <iostream>
using namespace std;
class Main1
{
public:
Main1()
{
cout << "\nConstructor of Main1" << endl;
}
~Main1()
{
cout << " \nDestructor of Main1" << endl;
}
};
class Main2
{
public:
Main2()
{
cout << "\nConstructor of Main2" << endl;
}
~Main2()
{
cout << " \nDestructor of Main2" << endl;
}
};
class Child : public Main2, public Main1
{
public:
Child()
{
cout << "\nConstructor of Child" << endl;
}
~ Child()
{
cout << "\nDestructor of Child" << endl;
}
};
int main()
{
Child obj;
return 0;
}