1.

What are Destructors in C++? Write down the syntax for a destructor in C++.

Answer»

Destructors in C++  are instance member functions that are automatically CALLED whenever an object is destroyed. In other WORDS, a destructor is the last function called before an object is destroyed. It is worth noting that if an object was formed with the "new" KEYWORD or if the constructor used the "new" keyword to ALLOCATE memory from the heap memory or the free store, the destructor should free the memory with the "delete" keyword.

Destructors are usually used to deallocate memory and do other cleanups for a class object and its class members when the object is destroyed and are called for a class object when that object passes out of scope or is explicitly deleted.

The syntax for a destructor in C++ is given below:

~constructor-name();

So, for example, if the name of the class is "Car", the destructor of the class would be as FOLLOWS (the name of the constructor would be "Car"):

~Car();


Discussion

No Comment Found