C++ Destructor

Destructor


  • Destructor is member function of a class having same name as of class prefixed with tild sign(~).
  • Destructor is automatically called when an object is destroyed.
  • A destructor contains no argument and no return value and not even void.
  • Destructor cleans up the memory area of object which is not longer required.

Program to show use of destrcutor in class



#inclue<iostream.h>
#inclue<conio.h>
class data
{
   int a;
   float b;
   public:
   data()
   {
       a=12;
       b=7.6;
   }
   void show()
   {
       cout<<"\na = "<<a;
       cout<<"\nb = "<<b;
   }
   ~data()
     {
       cout<<"\nObject Destroyed";
     }
};
void main()
{
    clrscr();
    data d;
    d.show();
    getch();
}


Output


a=12
b=7.6
Object Destroyed

Back To Top