In this practical we will observe the invocation order of constructor and destructor in inheritance. For instance, If there is one base class and one derieved class with each having their own contructor and destructor then the invocation order is as follows:

  1. Base constructor
  2. Derived constructor
  3. Derived destructor
  4. Base destructor

 

C++ program to explain Invocation order of Constructor and Destructor in Inheritance

#include<iostream.h>
#include<conio.h>
class Base
{
  public:

  Base ( )
  {
    cout << "Inside Base constructor" << endl;
  } 

  
  ~Base ( )
  {
    cout << "Inside Base destructor" << endl;
  } 

};

class Derived : public Base
{

  public:

  Derived  ( )
  {
    cout << "Inside Derived constructor" << endl;
  } 

  ~Derived ( )
  {
    cout << "Inside Derived destructor" << endl;
  } 

};

void main( )
{
  clrscr();
  Derived x;
  getch();
}

Check out – All CSE Basic Practicals

Thanks for visiting us. Do share us among your friends.

C++ program Invocation order of Constructor and Destructor in Inheritance – Computer Practical
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *