C++ Destructor Basics



In the previous chapter we have learnt about the C++ constructors and basic type of constructors. In this chapter we will be learning about C++ Destructor Basics

C++ Destructor Basics

  1. Destructors are special member functions of the class required to free the memory of the object whenever it goes out of scope.
  2. Destructors are parameterless functions.
  3. Name of the Destructor should be exactly same as that of name of the class. But preceded by ‘~’ (tilde).
  4. Destructors does not have any return type. Not even void.
  5. The Destructor of class is automatically called when object goes out of scope.

C++ Destructor programs

C++ Destructor Program #1 : Simple Example

#include<iostream> 
using namespace std;
 
class Marks
{
public:
   int maths;
   int science;
   
   //constructor
   Marks() {
      cout << "Inside Constructor"<<endl;
      cout << "C++ Object created"<<endl;
   }
	
   //Destructor
   ~Marks() {
      cout << "Inside Destructor"<<endl;
      cout << "C++ Object destructed"<<endl;
   }
};

int main( )
{
   Marks m1;
   Marks m2;
   return 0;
}

Output

Inside Constructor
C++ Object created
Inside Constructor
C++ Object created

Inside Destructor
C++ Object destructed
Inside Destructor
C++ Object destructed

Explanation :

You can see destructor gets called just before the return statement of main function. You can see destructor code below –

~Marks() {
   cout << "Inside Destructor"<<endl;
   cout << "C++ Object destructed"<<endl;
}

C++ Destructor always have same name as that of constructor but it just identified by tilde (~) symbol before constructor name.

Below example will show you how C++ destructor gets called just before C++ object goes out of scope.

C++ Destructor Program #2 : Out of scope

#include<iostream> 
using namespace std;
 
class Marks
{
public:
   int maths;
   int science;
   
   //constructor
   Marks() {
      cout << "Inside Constructor"<<endl;
      cout << "C++ Object created"<<endl;
   }
	
   //Destructor
   ~Marks() {
      cout << "Inside Destructor"<<endl;
      cout << "C++ Object destructed"<<endl;
   }
};

int main( )
{
   {
   Marks m1;
   }

   cout<<"Hello World !!" <<endl;
   cout<<"Hello World !!" <<endl;
   cout<<"Hello World !!" <<endl;
   cout<<"Hello World !!" <<endl;

   return 0;
}

Output :

Inside Constructor
C++ Object created
Inside Destructor
C++ Object destructed

Hello World !!
Hello World !!
Hello World !!
Hello World !!

Explanation :

In this example we have defined the scope of the object as we declared object inside the curly braces i.e inside the inner block.

int main( )
{
   {
   Marks m1;
   }

So object will be accessible only inside the curly block. Outside the curly block we cannot access the object so destructor gets called when inner curly block ends

In this example Hello World !! gets printed after the destructor. however for below example Hello World !! gets printed before constructor

C++ Destructor Program #3 : before destructor

#include<iostream> 
using namespace std;
 
class Marks
{
public:
   int maths;
   int science;
   
   //constructor
   Marks() {
      cout << "Inside Constructor"<<endl;
      cout << "C++ Object created"<<endl;
   }
	
   //Destructor
   ~Marks() {
      cout << "Inside Destructor"<<endl;
      cout << "C++ Object destructed"<<endl;
   }
};

int main( )
{
   Marks m1;
   
   cout<<"Hello World !!" <<endl;
   cout<<"Hello World !!" <<endl;
   cout<<"Hello World !!" <<endl;
   cout<<"Hello World !!" <<endl;

   return 0;
}

Output :

Inside Constructor
C++ Object created

Hello World !!
Hello World !!
Hello World !!
Hello World !!

Inside Destructor
C++ Object destructed