Xpode.com        Click here to Print this article.

Desctructor in c++

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.




http://


Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=58

Click here to go on website