The primary purpose of the destructor is return an objects dynamic memory so that it can be reused.
Properties of the destructor:
The name of the destructor is ALWAYS the tilde (~) followed by the class name.
The destructor takes no arguments and returns no value.
Destructors are automatically called when an object goes out of scope.
Common situations when the destructor is called automatically:
When an object is a local variable in a function and the function finishes execution.
When a function has an object as its passed in argument and that argument is NOT a reference.
When memory that was dynamically allocated for a class object is freed up.
class X { public: X(int = 20); //default constructor -- default capacity of 20 ~X() //destructor private: int capacity; int used; int *data; };