Explain the concept of destructor?

Technology CommunityCategory: OOPExplain the concept of destructor?
VietMX Staff asked 3 years ago

A destructor is a special method for a class and is invoked automatically when an object is finally destroyed. The name of the destructor is also same as that of the class but is followed by a prefix tilde (~).

A destructor is used to free the dynamic allocated memory and release the resources. You can, however, implement a custom method that allows you to control object destruction by calling the destructor.

The main features of a destructor are as follows:

  • Destructors do not have any return type
  • Similar to constructors, destructors are also always public
  • Destructors cannot be overloaded.