Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Destructors in C++

What is a destructor?
Destructor is an instance member function that is invoked
automatically whenever an object is going to be destroyed.
Meaning, a destructor is the last function that is going to be
called before an object is destroyed.
 A destructor is also a special member function like a
constructor. Destructor destroys the class objects created
by the constructor.
 Destructor has the same name as their class name
preceded by a tilde (~) symbol.
 It is not possible to define more than one destructor.
 The destructor is only one way to destroy the object
created by the constructor. Hence destructor can-not be
overloaded.
 Destructor neither requires any argument nor returns any
value.
 It is automatically called when an object goes out of
scope.
 Destructor release memory space occupied by the objects
created by the constructor.
 In destructor, objects are destroyed in the reverse of an
object creation.
The thing is to be noted here if the object is created by using
new or the constructor uses new to allocate memory that
resides in the heap memory or the free store, the destructor
should use delete to free the memory.
Syntax

The syntax for defining the destructor within the class:


~ <class-name>() {
// some instructions
}
The syntax for defining the destructor outside the class:
<class-name> :: ~<class-name>() {
// some instructions
}
Example 1

#include <iostream>
using namespace std;

class class_name{

// declaring private class data members


private:
int a,b;

public:

// declaring Constructor
class_name(int aa, int bb)
{
cout<<"Constructor is called"<<endl;
a = aa;
b = bb;

cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
cout<<endl;
}

// declaring destructor
~class_name()
{
cout<<"Destructor is called"<<endl;
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
}

};
int main()
{
// creating objects of class using parameterized constructor
class_name obj(5,6);

return 0;
}

The below code demonstrates the automatic execution of


constructors and destructors when objects are created and
destroyed, respectively.

// C++ program to demonstrate the


execution of constructor
// and destructor

#include <iostream>
using namespace std;

class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor
executed"; }

// User-Defined Destructor
~Test() { cout << "\nDestructor
executed"; }
};
main()
{
Test t;

return 0;
}

Output
Constructor executed
Destructor executed
Example 2
The below code demonstrates the automatic execution of
constructors and destructors each time when multiple objects
are created and destroyed, respectively.

// C++ program to demonstrate the


execution of constructor
// and destructor when multiple objects
are created

#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor
executed"; }

// User-Defined Destructor
~Test() { cout << "\n Destructor
executed"; }
};
main()
{
// Create multiple objects of the Test
class
Test t, t1, t2, t3;
return 0;
}

Output
Constructor executed
Constructor executed
Constructor executed
Constructor executed
Destructor executed
Destructor executed
Destructor executed
Destructor executed
Example 3
The below C++ program demonstrates the number of times
constructors and destructors are called.
// C++ program to demonstrate the
number of times
// constructor and destructors are called

#include <iostream>
using namespace std;
static int Count = 0; //It is static so
that every class object has the same
value
class Test {
public:
// User-Defined Constructor
Test()
{

// Number of times constructor is


called
Count++;
cout << "No. of Object created: "
<< Count
<< endl;
}
// User-Defined Destructor
~Test()
{

cout << "No. of Object destroyed:


" << Count //It will print count in
<< endl; //
decending order
Count--;
// Number of times destructor is
called
}
};

// driver code
int main()
{
Test t, t1, t2, t3;

return 0;
}
Output
No. of Object created: 1
No. of Object created: 2
No. of Object created: 3
No. of Object created: 4
No. of Object destroyed: 4
No. of Object destroyed: 3
No. of Object destroyed: 2
No. of Object destroyed: 1
Note: Objects are destroyed in the reverse order of their
creation. In this case, t3 is the first to be destroyed, while t is
the last.
Properties of Destructor
The following are the main properties of Destructor:
 The destructor function is automatically invoked when
the objects are destroyed.
 It cannot be declared static or const.
 The destructor does not have arguments.
 It has no return type not even void.
 An object of a class with a Destructor cannot become a
member of the union.
 A destructor should be declared in the public section of
the class.
 The programmer cannot access the address of the
destructor.
When is the destructor called?
A destructor function is called automatically when the object
goes out of scope:
1. the function ends
2. the program ends
3. a block containing local variables ends
4. a delete operator is called
Note: destructor can also be called explicitly for an object.
How to call destructors explicitly?
We can call the destructors explicitly using the following
statement:
object_name.~class_name()
How are destructors different from normal member
functions?
 Destructors have the same name as the class preceded by
a tilde (~)
 Destructors don’t take any argument and don’t return
anything

Examples:
#include <iostream>
using namespace std;
class Test {
int a;
public:
// User-Defined Constructor
Test(int x) {
a=x;cout << "\n Constructor executed"; }

// User-Defined Destructor
~Test() { cout <<a<< "\n Destructor executed"; }
};

main()
{
// Create multiple objects of the Test class
Test t(1), t1(2), t2(3), t3(4);
return 0;
}

You might also like