Introduction
In this post, I am going to write a program to count the number of objects created and destroyed in a CPP or C++ program.
Also Read: Tokens in C++ Programming
This program is so simple in c++ programming. You must know about constructor and destructor.
Constructor is a special member function whose name is the same as the class name. It is automatically invoked when the object of this class is created. So, by using a constructor we can count the number of objects which are created.
Similarly, destructor is also a special member function whose name is the same as the class name. But, it is invoked only when the objects of this class are deleted or the program is terminated. There is tilde (~) is preceded with the destructor. So, using destructor, we will count the number of objects destroyed.
Also Read: Best 5 Basic C++ Programs For Beginners
I am assuming that you have a little bit of idea about constructor and destructor. Now, let us write the program.
Count Number of Objects Created and Destroyed in CPP Program
#include <iostream> using namespace std; int cobj,dobj; class cobject { public: cobject() { cobj++; } ~cobject() { dobj++; } }; int main() { cobject *a1=new cobject(); cobject *a2=new cobject(); cobject *a3=new cobject(); cout<<"Total object created "<<cobj<<endl; delete a1; delete a2; delete a3; cout<<"Total object deleted "<<dobj<<endl; return 0; }
Before going further, let us see the output of the program.

Explanation
Global variables
Here, I have declared two global variables. Why? I need to keep the values of these two variables throughout the program. These variables are cobj and dobj are of int type.
How do you count the number of objects created in C++?
For this, I have defined a constructor. Just see the following code.
cobject() { cobj++; }
In the above code, I am incrementing the value of the variable cobj by 1. The statement cobje++ is written inside the constructor. So, whenever we will create the object, this constructor will get invoked and the value of this variable will get incremented by 1.
Also Read: A Simple C++ Program
In the main function, we will create the objects as shown below.
cobject *a1=new cobject(); cobject *a2=new cobject(); cobject *a3=new cobject();
Here, we have created three objects. You can create any number of objects using the concept array of objects.
I hope you have understood. Now, moving to the next section.
How do you count the number of objects deleted in C++?
Here, we will use destructor. See the following code.
~cobject() { dobj++; }
Maybe you don’t find any difference between constructor and destructor. But, while defining destructor, we use the symbol tilde(~) before writing the destructor.
Here, we are incrementing the value of dobj by 1 after calling the destructor. How can we call the destructor? There are two ways. One is by using the delete keyword or another is by terminating the program. Here, I am using the delete keyword.
Also Read: Simple Interest Program in C++ using Class
Now, see the following code.
delete a1; delete a2; delete a3;
Here, we have to delete three objects, so I have used three delete statements.
I hope you have understood this program. If you have any difficulties, please let me know.
Thank you.