Destructors is used to destroy the objects that have been created by a constructor. The destructor is member function whose name is the same as of the class, but it is preceded by tilde(~).
For example the destructors for the class integer can be defined as shown below:
~integer() {}
A destructor never takes any arguments nor does it return any value. It will be invoked implicitly by compiler upon exit from the program(or block or function as the case may be) to clean up storage that is no longer accessible.
It is good practice to declare destructors in a program since it releases memory space for future use. Whenever new operator is used to allocate memory in constructor, we should use delete to free that memory. This is required because when the pointer to object goes out of scope, destructors is not called implicitly.
Administrador de ENGGDRCAOS. Canal dedicado especialmente a la formación del estudiante que aspira a ser ingeniero. Todos los videos son Ingles. Apasionado del universo Apple, estudiante de Ingeniería y Gamer por vocación.