Friday, 3 January 2014

Destructor

Destructor:

Destructor is a special function used to remove the memory allocated by an constructor to an object.It gets executed ,when the object gets deleted.

Syntax:

~Class_name()
{
        //body of destructor
}

Example:
~sample()
{
    delete v;
}

Program to illustrate the use of destructor:

#include<iostream.h>
#include<conio.h>
class sample()
{
   private:
         int a;
   public:
         sample()
         {
                     i=1;
                     cout<<"Object<<i<<created"
                     i++;
         }
          ~sample()
         {
                  cout<<"object gets deleted";
                  i--;
          }
};
void main()
{
      sample s;
      getch(0;
}
    

No comments:

Post a Comment