It is a special member function which is used to destroy the object
that is it is used to take back the memory given to the object at the
time of its creation. Since it destroys the object, it is called
‘Destructor’.
Certain characteristics of destructor are:
1. Its name is same as that of class name but is preceded by ‘tilde’ (~) symbol.
2. It is automatically executed when the object goes out of scope that
is there is no need to call it as we call ordinary member function.
3. It does not take arguments and it has no return type that is it does not return any value.
4. If we do not use our own destructor it is automatically provided by the system.
5. We can not overload destructor.
For example (In case of C++):
class sample
{
public:
sample( ) //Constructor
{
cout<<”Object Created”;
}
~sample( ) //Destructor
{
cout<<”Object Destroyed”;
}
};
void main( )
{
sample s1; //Constructor is called
clrscr( );
getch( );
} //Destructor is called since closing
brace of main ( ) is the end of scope
of object s1.