The destructor

#include <iostream.h>
#include <conio.h>
class Test
{
private :
static int count;
public :
Test ( )
{
cout<<”Object created =”<<++count<<”\n”;
}
~ Test( )
{
cout<<”Object destroyed =”<<count--<<”\n”;
}
};
int Test :: count;
void main( )
{
Test t1, t2, t3;
{
Test t4, t5;
cout<<”Leaving block…\n”;
}
cout<<”Leaving program…\n”;
}
Output :
Object created = 1
Object created = 2
Object created = 3
Object created = 4
Object created = 5
Leaving block…
Object destroyed = 5
Object destroyed = 4
Leaving program…
Object destroyed = 3
Object destroyed = 2
Object destroyed = 1

0 comments: