Difference between Constructors and Destructors

Constructor

Constructors are special member functions of a class which is used to initialize the object in desired way. Constructors has the same name as that of class. There could be multiple constructors defined in a class which could be selected based on invoking conditions.

Types of constructor

  • Default constructor – Any constructor without argument is called default constructor.
  • Parameterized constructor – Any constructor in which argument can be passed is called Parameterized constructor.
  • Copy constructor – This is a special kind of constructor which uses an existing object to create a new object.

Destructor

Destructor is a special member function of a class which is used to delete the object. Destructor also has the same name as that of class prefixed with “~” sign. In any class, one could define only one destructor. Destructor doesn’t take any argument.

Constructors vs Destructors

Let’s have a look into the differences between constructors and destructors.

ConstructorsDestructors
Used to initialize the object.Used to delete the object.
Has same name as class name.Has name starting with “~” followed by class name.
It can take zero or more arguments.It can’t take any arguments.
A class can have one or more constructors.A class can have only one destructor.
Constructor follows class hierarchy from base to derived.Destructor follows reverse hierarchy from derived to base.
Constructors can be overloaded.Destructors can’t be overloaded.
Constructor can’t be virtual.Destructors can be virtual.

Leave a Reply

Your email address will not be published. Required fields are marked *