CONSTRUCTORS VS. DESTRUCTORS
Constructors and Destructors are a very important concept of C++ language. C++ is an OOP or Object Oriented Programming Language and it provides special member functions called Constructors used to automatically initialize objects of a class. In the complement of it, it also provides another member function called Destructor to destroy objects when their need is no longer required. It is necessary so as to keep memory free. Constructors, as the name implies, are automatically invoked and initialize object. Its name is same as that of class name, e.g.
Class A
{
int m,n;
public:
A( ) ; <---- Constructor
}
If no Constructor is defined in a program then default Constructor is supplied by the compiler automatically. They are declared in the Public section i.e., access mode is public. They have no return type i.e., they cannot return values to program. They cannot be inherited by other classes. We can pass arguments to a constructor. O a class can contain more than one class.
As indicated by name, Destructors destroy the objects that have been created by constructor. It is also a member function of a class having same name of class preceded by a tilde sign. E.g.,
Class A
{
int a;
Public;
A( ) ; <------Destructor
};
A destructor never takes any value and never returns a value. It destroys the value of the object in reverse order of their creation. It is invoked automatically by the compiler, whenever a block or function ends or a program exit. Destructors clear the memory area occupied by initialization of object making memory space available for future use. So it is a good practice to declare a Destructor.