Hello everyone. I had a small doubt regarding the above program. The destructors of all three classes are being executed just before cout<<"Executing getch()..."<<endl; as well as after the getch() statement, in the main function. I was wondering why this is happening, as I was expecting the destructors to be executed only once, i.e. after the getch() statement in main(). Please help me out.Code:/* Program to study constructors using inheritance... */ #include<iostream.h> #include<conio.h> class base1 { int x; public: base1(){} base1(int a) { x=a; cout<<"Constructing base1..."<<x<<endl; getch(); } ~base1(){cout<<"Destroying base1..."<<endl; getch();} }; class derived1 { int y; public: derived1(){} derived1(int b) { y=b; cout<<"Constructing derived1..."<<y<<endl; getch(); } ~derived1(){cout<<"Destroying derived1..."<<endl; getch();} }; class derived2:public base1,public derived1 { protected: int z; public: derived2(){} derived2(int a,int b,int c): base1(a), derived1(b) { z=c; cout<<"Constructing derived2..."<<z<<endl; getch(); } ~derived2(){cout<<"Destroying derived2..."<<endl; getch();} }; int main() { clrscr(); derived2 d1; d1=derived2(1,2,3); cout<<"Executing getch()..."<<endl; getch(); return(0); }
Compiler : Borland C++ 5.5



LinkBack URL
About LinkBacks



CornedBee