0% found this document useful (0 votes)
40 views1 page

Destructor Example

The document demonstrates how constructors and destructors work in C++ by creating objects of a Test class and tracking how many objects are created and destroyed through a count variable. It creates Test objects both inside and outside code blocks to show how destructors are automatically called when objects go out of scope.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views1 page

Destructor Example

The document demonstrates how constructors and destructors work in C++ by creating objects of a Test class and tracking how many objects are created and destroyed through a count variable. It creates Test objects both inside and outside code blocks to show how destructors are automatically called when objects go out of scope.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<iostream.

h>
#include<conio.h>

int count = 0;
class Test
{
public :
Test()
{
count++;
cout<<"\n Object Created Number : "<<count;
}
~Test()
{
cout<<"\n Object Destroyed Number :"<<count;
count--;
}
};

int main()
{
clrscr();
cout<<"\n Inside Main";
Test t1; //constructor for t1 count = 1

{
cout<<"\n Inside Block";
Test t2,t3; //constructor for t2 and t3 count=2 and count =3
cout<<"\n Exit from Block";
} //destructor for t2 and t3 count=3 count =2
cout<<"\n Inside Main Again";
Test t4;//constructor for t4 count =2
cout<<"\n Exit from main";
getch();
return 0;
} //destructor for t1 and t4 count =2 count=1

You might also like