Constructor And Destructor In Inheritance
Constructors and destructors depends
on the type of inheritance being implemented.
- Base
class constructors are called first and the derived class constructors are
called next in single inheritance.
- Destructor
is called in reverse sequence of constructor invocation i.e. The
destructor of the derived class is called first and the destructor of the
base is called next.
Program:
#include<iostream.h>
#include<conio.h>
class base()
{
public:
base()
{
cout<<”base class
constructor”;
}
~base()
{
cout<<” base class
destructor”;
}
};
class derived: public
base
{
public:
derived()
{
cout<<”derived
class constructor”;
}
~derived()
{
cout<<” derived
class destructor”;
}
};
void main()
{
derived ob;
getch();
}
Output:
Base class constructor
Derived class constructor
Base class destructor
Derived class destructor
No comments:
Post a Comment