C plus plus destructor

C++ Destructor

C++ destructor function gets executed whenever an instance of the class to which it belongs goes out of existence. The primary usage of a C++ destructor function is to release memory space that the instance currently has reserved.
The characteristics of the C++ destructors are as follows:

•Its name is the same as that of the class to which it belongs, except that the first character of the name is the symbol tilde (~).

C++ destructor is declared with no return type (not even void) since it cannot ever return a value.

•It cannot be static, const or volatile.

C++ destructor takes no input arguments, and therefore cannot be overloaded.

•It should have public access in class declaration.

Generally the C++ destructor cannot be called explicitly (directly) from the program. The compiler generates a class to destructor when the object expires. Class destructor is normally used to clean up the mess from an object. Class destructors become extremely necessary when class constructor use the new operator, otherwise it can be given as an empty function. However, the destructor function may be called explicitly allowing you to release the memory not required and allocate this memory to new resources, in Borland C++ version 3.1.

Usage of C++ destructor

The following example illustrates the usage of C++ destructor:
#include<iostream.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<”\n No. of object created”<<count;
}
~alpha()
{
cout<<”\n No. of object destroyed”<<count;
count –;
}
};
main()
{
cout<<”Enter MAIN” <<endl;
alpha  A1,A2, A3, A4;
{
cout<<”Enter BLOCK1” <<endl;
alpha A5;
}
{
cout<<”Enter BLOCK2” <<endl;
alpha A6;
}
cout<<”Re- Enter MAIN” <<endl;
Output of the program is as follows:
Enter MAIN
No. of object created 1
No. of object created 2
No. of object created3
No. of object created 4
Enter BLOCK1
No. of object created 5
No. of object created 5
Enter BLOCK2
No. of object created 5
No. of object created 5
Re- Enter MAIN
No. of object created 4
No. of object created 3
No. of object created 2
No. of object created 1
As the objects are created and destroyed, they increase and decrease the count. After the first group of objects is created, A5 is created, and then destroyed; A6 is created, and then destroyed. Finally, the rest of the objects are also destroyed. When the closing brace of the scope is encountered, the C++ destructors for each object in the scope are called. The objects are destroyed in the reverse order of creation.

Comments

  1. gaspesie-travelguide

    Thank you a lot for providing individuals with an exceptionally marvellous opportunity to read from this website. It is usually very useful and as well , jam-packed with a lot of fun for me and my office fellow workers to search your blog at minimum thrice in a week to read the new items you have. And lastly, we are at all times fascinated concerning the very good inspiring ideas served by you. Some 3 ideas in this post are indeed the finest we have ever had.

  2. Bev Ganfield

    I like the valuable info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I’m quite sure I will learn many new stuff right here! Best of luck for the next!

  3. adidas eqt

    I was more than happy to search out this web-site.I wanted to thanks in your time for this glorious read!! I positively enjoying each little bit of it and I have you bookmarked to check out new stuff you weblog post.

  4. logo creator online

    I believe that is one of the sjch a lot significant information for me.
    And i’m happy reading yokur article. However want
    to observation on few general issues, The website taste is
    perfect, thhe articles iis actually great : D.
    Just right job, cheers

  5. yeezy boost 350

    Thanks a lot for giving everyone an extraordinarily nice chance to check tips from this web site. It can be so brilliant and also stuffed with a lot of fun for me and my office friends to search the blog no less than thrice weekly to read through the newest stuff you have got. And definitely, I am also actually contented with the fabulous techniques you give. Selected 3 areas in this article are completely the best I’ve had.

  6. Adidas NMD Men Women Mesh Surface Navy White

    I have to express my appreciation for your kindness supporting those people that need assistance with the study. Your special dedication to passing the solution all-around has been remarkably significant and have in most cases encouraged regular people much like me to arrive at their targets. The invaluable recommendations denotes this much to me and especially to my office colleagues. Regards; from all of us.

Leave a Reply

Your email address will not be published.