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 online 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 online 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 online 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 online here

Examples/Implementations of Constructor and Destructor:

Practice your Code online 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.


Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

Hey Programmer ?

Enter your email address to stay updated with the latest programming languages and articles.

Scroll Up
Would love your thoughts, please comment.x

Sing Up

  • Name

  • Minimum length of 8 characters.

    Log In