Destructors in C++

Every class has a destructor, which is called automatically when an object is deleted.

Destructors are the opposite of constructors. A constructor is invoked when an object is cre­ated and a destructor is invoked automatically when the object is destroyed. Every class has a default destructor if the destructor is not explicitly defined. Sometimes, it is desirable to implement destructors to perform customized operations. Destructors are named the same as constructors, but you must put a tilde character (~) in front. Listing 11.11 shows a Circle class with a destructor defined.

Listing 11.11 CircleWithDestructor.h

1 #ifndef CIRCLE_H
2
#define CIRCLE_H
3
4
class Circle
5 {
6     
public:
7    Circle();
8    Circle(
double);
9    ~Circle();
// Destructor
10    double getArea() const;
11   
double getRadius() const;
12   
void setRadius(double);
13   
static int getNumberOfObjects();
14
15   
private:
16   
double radius;
17   
static int numberOfObjects;
18 };
19
20
#endif

A destructor for the Circle class is defined in line 9. Destructors have no return type and no arguments.

Listing 11.12 gives the implementation of the Circle class defined in CircleWith- Destructor.h.

Listing II.I2 CircleWithDestructor.cpp

1 #include “CircleWithDestructor.h”
2
3
int Circle::numberOfObjects = 0;
4
5
// Construct a default circle object

6 Circle::Circle()
7 {
8     radius =
1;
9     numberOfObjects++;
10 }
11
12
// Construct a circle object
13 Circle::Circle(double radius)
14 {
15   
this->radius = radius;
16    numberOfObjects++;
17 }
18
19
// Return the area of this circle
20 double Circle::getArea() const
21 {
22   
return radius * radius * 3.14159;
23 }
24
25
// Return the radius of this circle
26 double Circle::getRadius() const
27 {
28   
return radius;
29 }
30
31
// Set a new radius
32 void Circle::setRadius(double radius)
33 {
34   
this->radius = (radius >= 0) ? radius : 0;
35 }
36
37
// Return the number of circle objects
38 int Circle::getNumberOfObjects()
39 {
40   
return numberOfObjects;
41 }
42
43
// Destruct a circle object
44 Circle::~Circle()
45 {
46    numberOfObjects–;
47 }

The implementation is identical to CircleWithStaticDataFields.cpp in Listing 10.7, except that the destructor is implemented to decrement numberOfObjects in lines 44-47.

The program in Listing 11.13 demonstrates the effects of destructors.

Listing 11.13 TestCircleWithDestructor.cpp

1 #include <iostream>
2
#include “CircleWithDestructor.h”
3 using namespace std;
4
5
int main()
6 {
7    Circle* pCircle1 =
new Circle();
8    Circle* pCircle2 =
new Circle();
9    Circle* pCircle3 =
new Circle();
10
11   cout <<
“Number of circle objects created: “

12   << Circle::getNumberOfObjects() << endl;

13

14   delete pCircle1;

15   

16  << Circle::getNumberOfObjects() << endl; 

17  cout << “Number of circle objects created: “

18
19   
return 0;
20 }

The program creates three Circle objects using the new operator in lines 7-9. Afterwards, numberOfObjects becomes 3. The program deletes a Circle object in line 14. After this, numberOfObjects becomes 2.

Destructors are useful for deleting memory and other resources dynamically allocated by the object, as shown in the case study in the next section.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *