Saturday, September 3, 2011

Destructors in C++ programs



When an object is destroyed in C++ programs, a function called destructor automatically gets called. A destructor has the same name as the constructor but it proceeded by a tilde. During the creation of an object constructor is called and destructor function is invoked when the control leaves the main () function. The most common use of destructor is to de-allocate memory that was allocated for the object by constructor.

#include< iostream.h >
#include< conio.h >
class Rect
{
private:
int x;
public:
Rect ()
{
cout<< "Inside constructor.";
}
~Rect ()
{
cout<< "Inside destructor.";
}
};
void main ()
{
Rect r1;
getch ();
}


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner