Destructor in C++

What is a destructor?
 Destructor is an instance member function which is invoked automatically whenever an object is going to    be destroyed. Meaning, a destructor is the last function that is going to be called before an object is destroyed.

The thing is to be noted here, if the object is created by using new or the constructor uses new to allocate memory which resides in the heap memory or the free store, the destructor should use delete to free the memory.

Syntax:
~constructor-name();

Properties of Destructor:
 1) Destructor function is automatically invoked when the objects are destroyed.
 2) It cannot be declared static or const.
3) The destructor does not have arguments.
4) It has no return type not even void.
5) An object of a class with a Destructor cannot become a member of the union.
6) A destructor should be declared in the public section of the class.
7) The programmer cannot access the address of destructor.

When is destructor called?
A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called  
Posted on by