C++ Constructor and Destructor

Constructor and Destructor

The derived class destructor is called first, and then the base class destructor gets called.
The base class constructor is called first.


inheriting classes, the base class' constructor and destructor are not inherited.

Lets create a sample class that includes a constructor and a destructor

we can define constructor and a destructor for the ''Sister'' class.
Sister ::Sister() {
 cout << "constructor" << endl;
Sister::~Sister () {
 cout << "destructor" << endl;

You can think of it as the following: The derived class needs its base class in order to work - that is why the base class is set up first.


Constructor is used to initialize the instance of a class.

Constructor is Called when new instance of a class is created.

Constructor allocates the memory.

Constructors can have arguments.

Overloading of constructor is possible.

Constructor has the same name as class name.


//Body of Constructor


Destructor destroys the objects when they are no longer needed.

Destructor is called when instance of a class is deleted or released.

Destructor releases the memory.

Destructor can not have any arguments.

Overloading of Destructor is not possible.

Destructor also has the same name as class name but with (~) tiled operator.


~ ClassName()

Purpose of Constructor and Destructor

Both constructors and destructors are special member functions of every class but with different concepts. A constructor is used to initialize the instance of a class, meaning it is invoked each time a class is instantiated, whereas a destructor is the opposite of a constructor which are called in the reverse order of constructors.