Behavior of constructors and destructors in inheritance
For
Example: Write a program to demonstrate the use of constructors and destructors in inheritance.
#include<iostream.h>
#include<conio.h>
class base //base
class
{
public:
base()
//constructor
{
cout<<"Base class constructor"<<endl;
}
~base()
//destructor
{
cout<<"Base class Desctructor"<<endl;
}
};
class derived : public base //Derived class/ child class
{
public:
derived() //constructor
of derived class
{
cout<<"Derived class
constructor"<<endl;
}
~ derived() //destructor
of derived class
{
cout<<"Derived class destructor"<<endl;
}
};
int main()
{
derived d; //Automatically executes both derived class and
base class
getch();
return 0;
}
output :
Comments
Post a Comment