Question - Explain the concept of Destructor in detail. Explain it with an example.
Answer -
A destructor is a member that works just the opposite of the constructor. Unlike constructors, destructors mainly delete the object. The destructor name must match exactly with the class name just like a constructor. A destructor block always starts with the tilde (~) symbol.
Syntax:
~class_name()
{
//code
}
A destructor is called automatically:
- when the program finishes its execution.
- Whenever a scope of the program ends that defines a local variable.
- Whenever you call the delete operator from your program.