Home >>C++ Tutorial >C++ Destructor

C++ Destructor

C++ Destructor

A Destructor in C++ is basically an antonym of the constructor as it destructs the class's objects. Destructor is invoked automatically just like constructors and can only be defined once in a class.

The process of defining the constructor is just the same as the constructor in C++ the only difference is that the destructor is prefixed with a tilde sign (~).

Please note that the destructors can never have parameters and on the top of that modifiers cannot be applied to it.

Here is an example of the destructor in C++ that will help you understand it better:

#include <iostream>  
using namespace std;  
class Student  
 {  
   public:  
        Student()    
        {    
            cout<<"This is Default Constructor"<<endl;    
        }    
        ~Student()    
        {    
            cout<<"This is Desctructor(Bye Bye)"<<endl;    
        }  
};  
int main(void)   
{  
    Student stu1; //creating an object of Student class   
    return 0;  
}  
 
Output :
This is Default Constructor
This is Desctructor(Bye Bye)

No Sidebar ads