1.

Define destructors in C++. Give the syntax for Destructors in C++.

Answer»

In C++, DESTRUCTORS are instance member functions that are called automatically when an object is destroyed. A destructor, in other TERMS, is the final function called before an object is destroyed. It's worth noting that if an object was created with the "new" keyword, or if the CONSTRUCTOR USED the "new" keyword to allocate memory from the heap memory or the free store, the memory should be freed with the "delete" keyword in the destructor. Destructors are called for a class object when it goes out of scope or is expressly removed, and they are used to deallocate memory and do the ADDITIONAL cleanup for the object and its members when the object is destroyed.

In C++, a destructor has the following syntax: ~constructorName();

Hence, for instance, if the name of the class is "Complex", the destructor of the class would be as follows (the name of the constructor would be "Complex"):

~Complex();


Discussion

No Comment Found