Destructor:
1. A destructor is a special member function whose task is to destroy the objects that have been created by constructor.
2. It does not construct the values for the data members of the class.
3. It is invoked implicitly by the compiler upon exit of a program/block/function.
4. Destructors are not classified in any types.
5. Destructor never accepts any parameter.
6. Destructor name is preceded with tilde operator.
Syntax:
~classname()
{….
….
}
Example:
#include<iostream.h>
#include<conio.h>
class time
{
private:
int hrs, mins,sec;
public:
time(int h,int m,int s)
{
hrs=h;
mins=m;
sec=s;
}
~time() // Destructor
{
cout<<”hours deleted”;
cout<<”minutes deleted”;
cout<<”seconds deleted”;
}
void display()
{
cout<<”hours=”<<hrs;
cout<<”Minutes=”<<mins;
cout<<”seconds=”<<sec;
}
};
void main()
{
time t(2,43,56);
t.display();
getch();
}