What are the basics of adding destructor?
Every class can optionally have a destructor. A destructor is a special member function that is automatically called whenever an object of the class is destroyed. This feature of C++ allows the class developer to close any files the object has opened, release any memory the object has allocated, unlock any semaphores the object has locked, and so on. In general, this gives an object a chance to clean up after itself.
Every class can optionally have a destructor (a.k.a. dtor). A destructor is a special member function that is automatically called whenever an object of the class is destroyed. This feature of C++ allows the class developer to close any files the object has opened, release any memory the object has allocated, unlock any semaphores the object has locked, and so on. In general, this gives an object a chance to clean up after itself.
If a class doesn't have a destructor, the compiler conceptually gives the class a destructor that does nothing. Therefore if a class doesn't need to do anything special inside its destructor, the easiest thing to do is to not even declare a destructor. Unlike a constructor a destructor of a class cannot be overloaded. Also when classes are inhertited then the derived class destructor is called first and then the destructors of the base classes in the hierarchy are called.
Example:
class Foo
{
private:
int *arr[];
public:
~Foo(); //destructor
};
