You are on page 1of 3

/*******************************************************************

How parameters are passed to constructor Functions in case of


MULTI LEVEL INHERITANCE
*******************************************************************/
#include<iostream.h>
# include<conio.h>
class A
{ int a1;
public:
A(int y) //PARAMETERIZED CONSTRUCTOR of BASE CLASS
{
cout <<"Constructor of Class A invoked\n";
a1=y;
}
~A()
{ cout <<"Destructor of Class A invoked\n";
getch();
}
void getA()
{ cout<<"getA() invoked\n";
cin>>a1;
}
void showA()
{cout <<"a1="<<a1<<endl;
getch();
}

};
class B:public A
{ int b1,b2;
public:
B(int z): A(z)
{

cout<<"Constructor of class B invoked\n";


b1=5;
b2=7;
}
~B()
{ cout <<"Destructor of Class B invoked\n";
getch();
}
void getB(){cin>>b1>>b2;}
void showB()
{ showA();
cout<<"b1="<<b1<<" b2="<<b2<<endl;
}
};
//*******************************************************************************
class C : public B
{ int c1;
public:
C(int y,int z):B(y) //CLASS C accepting 2 parameters
//one to be passed to CLASS B Constructor
// other parameter for its own member
{
cout <<"Constructor of Class C invoked\n";
c1=z;
}
~C()
{ cout <<"Destructor of Class C invoked\n";
getch();
}
void getC()
{ cout<<"getC() invoked\n";
cin>>c1;
}
void showC()
{showB();
cout <<"c1="<<c1<<endl;
getch();
}

};

void main()
{
clrscr();
cout<<"*******************************************************************\n";
cout<<"How parameters are passed to constructor Functions in case of\n";
cout<<"MULTI-LEVEL INHERITANCE :\n"
<<" class A BASE OF B\n"
<<" ^\n"
<<" |\n"
<<" class B DERIVED FROM A & BASE OF C\n"
<<" ^\n"
<<" |\n"
<<" class C\n";

cout<<"*******************************************************************\n\n\n
";
cout <<"Object of class C is going to be created ......\n"
<<"Press a key to do so.....\n";
getch();
C objC(50,100);

objC.showC();
cout<<"Now Object of Class C is going to be destroyed...\n"
<<"Check the Order in which destructors are going to be invoked...\n\n";
getch();
}

//******************************** OUTPUT *****************************


/********************************************************************
How parameters are passed to constructor Functions in case of
MULTI LEVEL INHERITANCE

class A BASE OF B
^
|
class B DERIVED FROM A & BASE OF C
^
|
class C DERIVED FROM B
*******************************************************************
Object of class C is going to be created in next line......
Press a key to do so.....
Constructor of Class A invoked
Constructor of class B invoked
Constructor of Class C invoked
a1=50
b1=5 b2=7
c1=100
Now Object of Class C is going to be destroyed...
Check the Order in which destructors are going to be invoked...

Destructor of Class C invoked


Destructor of Class B invoked
Destructor of Class A invoked

*/

You might also like