Destructor in C++-:
A destructor is a special member function of a class, which is used for destroying the objects that has been created by the constructor.
So, basically as the name implies; the destructor is a kind of a thing in programming that destructs everything. It is like Lord Shiva in universe. You must have known that Lord Shiva, according to spiritual books is a destructor of the creation.
So, destructor in C++ is also similar to Lord Shiva. Lord Shiva destroys the creation, in the similar way, the destructor in C++, destroys the created memory/constructed memory by the constructor.
Destructor doesn’t contain any argument, nor does it have any return type.
Again, I am repeating that destructor is used to destroy the objects which were created by constructor in a reverse order.
So, now let’s move to a program that shows us the concept of destructors in C++.
The question is as below-:
Write a program that can store department name and department ID with a constructor. Also write the destructor in the same class and show that the objects are destroyed in reverse order of creation with a suitable message.
using namespace std;
Department(char *DN,int Id)
cout<<"\nThe number of objects created=\n"<<c;
cout<<"\nThe number of objects destroyed\n"<<c;
cout<<"\nEnd of the main function";
The output of the above code will come as belows. Let’s see the output-:
So, now let’s try to understand the program flow.
It all starts with the int main() function.
Inside that main() function, here in the above code, we have, Department D1(“Computer”,5). So, basically, doing this, we are invoking a parameterized constructor. Inside that parameterized constructor, called as Department; we have as follows-:
Department(char *DN,int Id)
So, the; input “Computer” will go and stay inside DN, whereas the other input 5 will go and stay inside the Id.
Again, let’s go further inside the constructor.
As soon as we enter inside the constructor hole(as in black hole LOL), we can see that, it will say-:
The number of objects created==c;
Then, it will copy, by using the string copy function. And to use that string copy function, we have included the string.h header file, and it is very important point to notice.
Then, inside the strcpy, there is as follows-:
Where, we copy name from DepartmentName, into DN.
And, we initialized departmentId with Id. Then, we did as-:
Department Name=Department Name
It will show the string that is copied from DN, as in above we discussed that strcpy function copied DepartmentName into DN.
After that, we went to show -:
Department Id=Department Id; obviously using the cout<< operator.
Once, the things in this constructor hole (I mean to say inside constructor) gets finished, we will go towards destructor department.
Oh! The name of destructor is in itself Department. Just for humor!!!
And, there is one ~ before it. What is this??? OH MY GOD, WHAT IS THIS???
No need to worry, as discussed at first, this is the sign of tilde.
And it MUST BE used to point destructors.
Inside destructor, it will show the following thing-:
The number of objects destroyed=c;
And the count will be c–. i.e decreased.
So, now let’s match the output with the above process. At first the (“computer”,5) goes. So, count c will be increased by c++. So c=1. And, it will lead us to the number of objects created=1.
strcpy will copy DepartmentName with DN. i.e computer gets copied and gets saved in DepartmentName.
Also, similarly, the id also gets copied and, DepartmentId=5.
After that, we can see the cout operator working on. It will start to show the DepartmentName=Computer
After that we will do the same thing for BEX and Electrical.
Then, the destructor will come into action.
Then, it will show the destruction in reverse order as the count will be decreased accordingly as c–.
The number of objects destroyed=3……2……..1.
That means that it will destroy Electrical item at first, then BEX item then only computer item.
It seems like the destructor is like a scope in the market here! Just for humor!!!