Example of Constructors and Destructors in Inheritance

Estudies4you
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
#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
Example of Constructors and Destructors in Inheritance,concept of constructors and destructors in inheritance,examples of constructor,examples of destructor,what is constructor,what is destructor,difference between constructor and destructor,estudies4you,c++ notes,c++ lecture notes jntuh,c++ study material jntuh,oops using c++ lecture notes jntu
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.

To Top