Program to show sequence of execution of constructors in inheritance

 

#include<iostream.h>

#include<conio.h>

class parent

{

            public:

            parent()

            {

                        cout<<endl<<"I am the parent";

            }

};

 

class child1 : public parent

{

            public:

            child1()

            {

                        cout<<endl<<"I am the child1";

            }

};

 

class child2 : public parent

{

            public:

            child2()

            {

                        cout<<endl<<"I am the child2";

            }

};

 

class grandchild : public child1,child2

{

            public:

            grandchild()

            {

                        cout<<endl<<"I am the grandchild";

            }

};

 

 

void main()

{

            clrscr();

            cout<<endl<<"Creating object of child 1 and child 2 class";

            child1 c1;

            child2 c2;

            cout<<endl<<"Creating object of grandchild class";

            grandchild g;

            getch();

}