---Advertisement---
Learn C plus plus

C++ : Destructors

By Smart Answer

Updated on:

---Advertisement---

A destructor, as the name implies 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 same as the class name but is preceded by a tilde (~). For example, the destructor for the class integer can be defined as shown below:

~integer() { }

A destructor never takes any argument nor it return any value.It will be invoked implicitly by the compiler upon exit from the program to clean up the storage that is no longer accessible.It is a good practice to declare destructors in a program since it releases memory space for future use.

Whenever new is used to allocate memory in the constructors, we would use delete to free that memory.Lets see the example of destructor which will delete the memory allocated by new keyword:

 

/* p is the float pointer which used to create dynamic memory using new keyword

float *p;
p = new float; */

example :: ~example()        // example is the class name
{
delete p;
}

 

 

This is required because when the pointers to the objects go out of scope,a destructor is not called implicitly.

The example below illustrates that the destructor has been invoked by the compiler.

 

#include <iostream>
using namespace std;

int count = 0;

class test
{
public:
test()
{
count++;
cout << “nn Constructor Message: Object number” << count << ” created.”;
}
~test()
{
cout << “nn Destructor Message: Object number” << count << ” destroyed.”;
count–;
}
}

int main()
{
cout << “Inside the main block”;
cout << “n Creating first object T1”;
test T1;
{         // Block 1
cout << “nn Inside Block 1”;
cout << “n Creating two more object T2 and T3”;
test T2,T3;
cout << “nn Leaving Block 1”;
}
cout << “nn Back inside main block”;
return 0;
}

Output:
Inside the main block
Creating first object T1
Constructor Message: Object number 1 created.
Inside Block 1
Creating two more object T2 and T3
Constructor Message: Object number 2 created.
Constructor Message: Object number 3 created.
Leaving Block 1
Destructor Message: Object number 3 destroyed.
Destructor Message: Object number 2 destroyed.
Back inside main block
Destructor Message: Object number 1 destroyed.

 

 

A class constructor is called everytime an object is created.Similarly, as the program control leaves the current block the objects in the block start getting destroyed and destructors are called for each one of them. Note that the objects are destroyed in the reverse order of their creation.Finally, the main block is exited, destructors are called corresponding to the remaining objects present inside main.

Similar functionality as depicted in the above program can be attained by using static sata members with constructors and destructors.We can declare a static integer variable count inside a class to keep a track of the number of its object instantiations.Being static, the variable will be initialized only once i.e. when the first object instance created.During all subsequent object creation, the constructor will increment the count variable by one.Similarly, the destructor will decrement the count variable by one as and when object gets destroyed.

The primary use of destructors is in freeing up the memory reserved by the object before it gets destroyed.
Lets see the demonstration of destructor releases the memory allocated to an object

 

#include <iostream>
#include <conio.h>
using namespace std;

int count = 0;

class test
{
int *a;
public:
test(int size)
{
a = new int[size];
cout << “n Constructor Message: Integer array of size” << size << ” created.”;
}
~test()
{
delete a;
cout << “n Destructor Message: Free the memory allocation of integer array.”;
}
}

int main()
{
int s;
cout << “Enter the size of array – “;
cin >> s;
cout << “n Creating an object of Test class”;
test T(s);
cout << “n Press any key to end the program”;
return 0;
}

Output:
Enter the size of array – 5
Creating an object of Test class
Constructor Message: Integer array of size 5 created.
Press any key to end the program
Destructor Message: Free the memory allocation of integer array.

 

 

Smart Answer

---Advertisement---

Related Post

C++ : Type Conversions

We know that when constants and variables of different types are mixed in an expression, C applies automatic type conversion to the operands as per certain rules.Similarly,an assignment ...

C++ : Functions

Introduction We know that function play an important role in C program development.Dividing a program into function is one of the major principles of top-down, structured programming.Another advantages ...

C++ : Constructors

Introduction A constructor is a special member function whose task is to initialise the object of its class.it is special because its name is same as the class ...

C++ : Overloading Unary, Binary Operator

Operator Let’s start off by overloading a unary operator. unary operators act on only one operand. (An operand is simply a variable acted on by an operator.) Examples ...

Leave a Comment