Constructors & destructors in C++:
#include <iostream.h> #include <conio.h> // Class definition class ConDes { public: // Acess specifier int W1; ConDes(int Pi); // Constructor ~ConDes(); // Destructor } GObj1(1),Gobj2(2); ConDes::ConDes(int Pi) { cout<<"\n Beginning:"<<Pi<<"\n"; W1=Pi; } ConDes::~ConDes() { cout<<"Destructing:"<<W1; cout<<"\n"; } int main() { clrscr(); ConDes LocObj1(3); cout<<"Karthikh Venkat - Students3k.com \n"; ConDes LocObj2(4); getch(); return 0; }
Output of this Program:
Beginning: 1
Beginning: 2
Beginning: 3
Karthikh Venkat - Students3k.com
Beginning: 4
Destructing: 4
Destructing: 3
Destructing: 2
Destructing: 1
Note: Based on the computer system’s environment and compiler models the result will be varied.
Pages: 1 2