Destructors in C++
Destructor
A destructor is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde. A destructor never takes any argument nor it doesn’t return any value.
Program:
#include<stdio.h> #include<conio.h> using namespace std; int count=0; class temp { public: temp() { count++; cout<<"Constructor Message: Object No "<<count<<"created..."; } -temp() { cout<<"Destructor Message: Object No "<<count<<"created..."; count--; } }; int main() { cout<<"Inside the main block..."; cout<<"Creating first object R1.."; temp R1; { cout<<"Inside Block 1.."; cout<<"Creating two more objects R2 and R3.."; temp R2,R3; cout<<"Leaving Block 1.."; } cout<<"Back inside the main block.."; return 0; }