Q15. Write an example program to illustrate the concept of constructors and destructors in inheritance
Answer:
//C++ program to illustrate the concept of constructors and destructors in inheritance
Note that, the execution of constructors will be in the order of derivation and execution of destructors will be in the reverse order of derivation.
//C++ program to illustrate the concept of constructors and destructors in inheritance
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<“Constructing base
class\n”;
}
virtual ~A()
{
cout<<“Destructing base
class\n”;
}
};
class B: public A
{
public:
B()
{
cout<<“Constructing derived
class\n”;
}
~B()
{
cout<<“Destructing derived
class\n”;
}
};
int main()
{
B *d=new B();
A*b=d;
delete b;
return 0;
}
Output