Answer: – A constructor is a special member function whose main operation is to allocate the required resources such as memory and initialize the objects of its class. A constructor is distinct from other member function of the class and it has the same name as its class. It is executed automatically when a class is instantiated. It is generally used to initialize object member parameters and allocate the necessary resource to object members. The constructor has no return value specification.

The C++ run-time system makes sure that the constructor of a class is the first member function to be executed automatically when an object of class is created. In other words, the constructor executed every time an object of that class is defined. Normally constructors are used to initializing the class data members. It is of course possible to define a class which has no constructor at all, in such a case, the run time system calls a dummy constructor when an object is created. The syntax for defining a constructor with its prototype within the class body and the actual definition outside it, as shown below –

class <Class Name>
                - - - - -
                - - - - -
                <Class Name>() //Constructor
                                - - - - -
                                Body of Constructor
                                - - - - -

Similar to other members, the constructor can be defined either within or outside the body of the class. It can access any data member like all other member functions but cannot be invoked explicitly and must have public status to serve its purpose. The constructor which does not take argument explicitly is called default constructor.

When the object is no longer needed it can be destroyed by the destructor. Destructor is just opposite of constructor. Destructor invoked when an object is destroyed. Some of the important properties of destructor are as follows –

  1. The destructor has the same name as the class but prefixed by tilde. The tilde distinguishes it from a constructor of the same class.
  2. Unlike the constructor, the destructor does not take any argument. This is because there is only one way to destroy an object.
  3. The destructor has neither argument, nor a return value.
  4. The abstractor has no return type like the constructor, since it is invoked automatically whenever an object goes out of scope.
  5. There can be only one destructor in each class. This is essentially a violation of the rules that a function can take arguments, thereby making function overloading impossible.