C++: Inheritance |sequence of execution of constructors and destructors in inheritance

Hybrid Inheritance

Example:3

//Sequence of execution of constructors and destructors
//Multiple inheritance
#include<iostream>
using namespace std;
class A
{
public:
    A()
    {
      cout<<"Constructor function of class A"<<endl;
    }
    ~A()
    {
      cout<<"Destructor function of class A"<<endl;
    }
};
class B:public A
{
public:
    B()
    {
      cout<<"Constructor function of class B"<<endl;
    }
    ~B()
    {
      cout<<"Destructor function of class B"<<endl;
    }
};
class C
{
public:
    C()
    {
      cout<<"Constructor function of class C"<<endl;
    }
    ~C()
    {
      cout<<"Destructor function of class C"<<endl;
    }
};
class D:public C
{
public:
    D()
    {
      cout<<"Constructor function of class D"<<endl;
    }
    ~D()
    {
      cout<<"Destructor function of class D"<<endl;
    }
};
class E:public B,public D
{
public:
    E()
    {
      cout<<"Constructor function of class E"<<endl;
    }
    ~E()
    {
      cout<<"Destructor function of class E"<<endl;
    }
};
int main()
{
  E e1;
  return(0);
}


Output:

Constructor function of class A
Constructor function of class B
Constructor function of class C
Constructor function of class D
Constructor function of class E
Destructor function of class E
Destructor function of class D
Destructor function of class C
Destructor function of class B
Destructor function of class A

Example:4

//Sequence of execution of constructors and destructors
//Multiple inheritance
#include<iostream>
using namespace std;
class A
{
public:
    A()
    {
      cout<<"Constructor function of class A"<<endl;
    }
    ~A()
    {
      cout<<"Destructor function of class A"<<endl;
    }
};
class B:public A
{
public:
    B()
    {
      cout<<"Constructor function of class B"<<endl;
    }
    ~B()
    {
      cout<<"Destructor function of class B"<<endl;
    }
};
class C
{
public:
    C()
    {
      cout<<"Constructor function of class C"<<endl;
    }
    ~C()
    {
      cout<<"Destructor function of class C"<<endl;
    }
};
class D:public C
{
public:
    D()
    {
      cout<<"Constructor function of class D"<<endl;
    }
    ~D()
    {
      cout<<"Destructor function of class D"<<endl;
    }
};
class E:public D,public B
{
public:
    E()
    {
      cout<<"Constructor function of class E"<<endl;
    }
    ~E()
    {
      cout<<"Destructor function of class E"<<endl;
    }
};
int main()
{
  E e1;
  return(0);
}


Output:

Constructor function of class C
Constructor function of class D
Constructor function of class A
Constructor function of class B
Constructor function of class E
Destructor function of class E
Destructor function of class B
Destructor function of class A
Destructor function of class D
Destructor function of class C