Inheritance : The Order of Invocation of Constructor and Destructor functions is explained in this module. It also explains how Parameters are passed to base class constructor.
Inheritance : The Order of Invocation of Constructor and Destructor functions is explained in this module. It also explains how Parameters are passed to base class constructor.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
Inheritance : The Order of Invocation of Constructor and Destructor functions is explained in this module. It also explains how Parameters are passed to base class constructor.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
How parameters are passed to constructor Functions in case of
MULTIPLE INHERITANCE class C:public A,public B *******************************************************************/ #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 { int b1,b2; public: B(int z) { cout<<"Constructor of class B invoked\n"; b1=z; b2=7; } ~B() { cout <<"Destructor of Class B invoked\n"; getch(); } void getB(){cin>>b1>>b2;} void showB() { cout<<"b1="<<b1<<" b2="<<b2<<endl; } }; //****************************************************************************** * class C : public A,public B { int c1; public: C(int x,int y,int z):A(x),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() {showA(); showB(); cout <<"c1="<<c1<<endl; getch(); } }; void main() { clrscr(); cout<<"*******************************************************************\n"; cout<<"How parameters are passed to constructor Functions in case of\n"; cout<<"MULTIPLE INHERITANCE : class C:public A,public B\n"; cout<<"*******************************************************************\n\n \n"; cout <<"Object of class C is going to be created in next line......\n" <<"Press a key to do so.....\n"; getch(); C objC(50,100,150); 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 ***************************** /* 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=100 b2=7 c1=150 */