Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.
General Syntax of Destructors

     ~ classname();

 

The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

Some important points about destructors:

•           Destructors take the same name as the class name.

•           Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.

•           The Destructor does not take any argument which means that destructors cannot be overloaded.

•           No return type is specified for destructors.

Example :

   class A{

        public:

           A(){

               cout<<"Constructor";

           }

           ~A(){

              cout<<"Destructor";

           }

};

void main(){

        A obj,obj1;

        {

            A obj2;

        }

        {

            A obj3,obj4;

        }

        getch();

}



String reverse using operator overloading
String concat using operator overloading
Multilevel Inheritance in C++
Single level Inheritance
Overloading Constructors / Multiple Constructors in C++
Destructors in C++
Copy Constructor in C++
Parameterized constructor in C++
Default constructor in C++
Initialization of class member and display on screen