+2 votes
in Class 12 by kratos

What is a destructor? Write its syntax and example.

1 Answer

+5 votes
by kratos
 
Best answer

It is a special function used to release the memory space allocated by the object.
→ Name of the Destructor is similar to the class, which it belongs.
→ It does not have argument(*) and doesn’t return any value (no return type)
→ Destructor is preceded by ~ (tilde) sign.

Following points should be kept in mind while defining and writing the syntax for the destructor:

  • A destructor function must be declared with the same name as that of the class to which it belongs.
  • The first character of the destructor name must begin with a tilde (~).
  • A destructor function is declared with no return types specified (not even void).
  • A destructor function must have public access in the class declaration.

General Syntax of Destructors:

~ classname();

The above is the general syntax of a destructor. In the above, the symbol tilde ~ represents a destructor which precedes the name of the class.
For example,

...