CPP Destructor

Unlike CPP Constructors, a CPP destructor is defined only once in a class in order to destroy the objects of that class. It has the same name as the name of the class, prefixed with a tilde sign (~).

Example:

#include <iostream.h>
using namespace std;
 
class Employees
{
public:  
string Name;  
float Salary;
Employees()   
{    
cout << "Hello Employee!!" << endl;
}
~Employees()   
{    
cout << "Bye Bye Employee!!" << endl;
}
};
int main()
{
Employees e1;    
Employees e2;  
return 0;
}

Output

Hello Employee!!
Hello Employee!!
Bye Bye Employee!!
Bye Bye Employee!!
Please follow and like us:
Content Protection by DMCA.com