What is Constructor and Destructor in C++


Constructor is a member function of class. It is mainly used to initialize objects in a class. A constructor called automatically when object is created. This is a special kind of member function of a class.

Syntax of Constructor:

Types of constructors:

In C++, three types of constructors.

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy the Constructor

1. Default Constructor:

The default constructor is a no-argument constructor. There are no parameters, but programmers can write initialization statements there.

For example:

Practice Your Code Here

2. Parameterized Constructor:

The default constructor has no parameters, but programmers can add and use parameters within the constructor as needed. This helps programmers to assign initial values ​​to objects when they are created.

For example:

Practice Your Code Here

3. Copy Constructor:

C ++ takes an object as an argument and provides a special type of constructor used to copy the values ​​of data members from one object to another. In this case, the copy constructor is used to declare and initialize an object from another object.

For example:

Practice Your Code Here


A destructor is another type of member function that is responsible for destroying or removing objects. Free up space occupied by objects when they are no longer needed. The destructor is called automatically when the object is out of range and is no longer needed. The name of the destructor is the same as the name of the class, the only difference is that the name is preceded by a tile (~).

Syntax of Destructor:
For example:

Practice Your Code Here

Examples/Implementations of Constructor and Destructor:

Practice Your Code Here

In C++, difference between Constructors and Destructors:

Constructor Destructor
1. The constructor helps to initialize the objects of the class.1. The destructor is used to destroy the instance.
2. Class name (if there are arguments) Declared as {constructor body}.2. Declared as ~ ClassName (no arguments) {} ;..
3. The constructor can specify whether to accept arguments.3. I cannot accept an argument
4. The constructor is called when an instance or object of the class is created.4. Called when an object of the class has been freed or removed.
5. The constructor is used to allocate memory for an instance or object.5. Used to deallocate the memory of the objects in the class.
6. The constructor may be overloaded.6. Although it cannot be overloaded.
7. The name of the constructor is the same as the name of the class.7. Here, its name is the same as the class name preceded by the tile operator (~).


Therefore, constructors and destructors play an important role in object-oriented programming languages. The constructor is used to initialize the object and the destructor is used to destroy the object. Both constructors and destructors are very useful when programming in the C ++ language.


(Visited 56 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x