Destructor

Destructor:

A destructor is a special member function of a class that is executed whenever an object of it’s class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

Example

#include<iostream.h>

#include<conio.h>

class dest

{

int l;

public: dest()

{

l=0;

cout<<“Hello”<<endl;

}

void f()

{

cout<<“l= “<<++l<<endl;

}

~dest()

{

cout<<“hi Henry”;

}

};

main()

{

clrscr();

dest d1,d2;

d1.f();

}

Output

Hello

Hello l=1

hi Henry hi Henry

Leave a Reply

Your email address will not be published. Required fields are marked *