Image

C++ - Destructor - Destructor

Destructor

It is a special function of a class which automatically invoked whenever execution controls goes out of scope.

Rules for constructing destructor

1. Destructor name and class name must be exactly same
2. They are pre fired by tiled (~) operator
3. It does not contain any return type
4. They are always define within public section
5. They invoked automatically whenever execution controls goes out of scope
e.g:
#include<conio.h>
#include<iostream.h>
class A
{
  public:
	A(){
	 cout<<"\n Constructor is called ";
	}
	~A(){
	 cout<<"\n Destructor is called";
	}
};
void main()
{
 int x;
 clrscr();
 A obj1;
 if(x){
       A obj2;
 }
 getch();
}
O/P
Constructor called
Constructor called
Destructor called