CodesJava

Easy learning with example program codes

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:
Posted in CPP   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki