Destructor In CPP

Destructor: Destructor is a member function of a class. It has the same name as a class but followed by a tilde sign (~). 
It does not have any return type. (neither void nor int) 
It automatically gets executed when the object is destroyed.
#include<iostream.h>
#include<conio.h>
class demo
{
public :
int a,b;

demo()  // constructor
 {
  a=10;
  b=20;
 }


~demo() // destructor
 {
  cout << "Thanx for using my program" << endl;
 }

void displaydata()
 {
  cout << a << endl;
  cout << b << endl;
 }
};

main()
{
clrscr();
demo d1;
d1.displaydata();

getch();
}

Leave a Reply