Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Sep 21, 2020

Destructors in C++ | C++ Language | Coding Winds

                                                            Destructors

 

You all must have heard about constructors, it's uses and properties. If you haven't then I recommend you to first read the article about constructors (link given below)

 

https://codingwinds.blogspot.com/2020/08/constructors-and-destructors-in-c-c.html

 

So, A destructor, as the name suggests , is used to destroy the objects (free the memory space) that have been created by constructor.Just like a constructor, destructor is a member function whose name is same as class name but preceded by a tilde(~) i.e we have to use a tilde before writing a class name to call a destructor.

Now for the betting understand of the concept l, let's talk about It's properties to have a better insight.

 

Properties

-It is called automatically whenever object is destroyed or removed from the memory.

-It is used to perform any operation at the time of destruction of any object.

-It is called as per LIFO(Stack) method.(IMP)

-It cannot have parameters. Thus, it cannot be overloaded.

-It can be virtual.

 

Now let's take an example how the objects are released from the memory.

 

Suppose we have a class whose name is Complex.

Complex c1,c2,c3;

Here c1, c2, c3 are objects of Complex class. So, c3 will be destructed first from the memory, then after c2 will be destructed and finally c1 will be destructed.

 

I hope after reading this article, the topic of destructor will be clear but If you still face some kind of doubt or have any query then you can contact us.

 

Thank you.