Difference Between Constructor and Destructor


In object oriented programming, both constructor and destructor are the member functions of a class having the same name as the class. A constructor helps in initialization of an object, i.e., it allocates memory to an object. On the other hand, a destructor deletes the created constructor when it is of no use which means it deallocates the memory of an object.

In this article, we will discuss the important differences between constructors and destructors. Let's start with some basic concepts of constructors and destructors.

What is a Constructor?

A constructor is a member function of a class that initializes the object and allocates the memory. A constructor has the same name as that of its class, thus it can be easily identified. It is always declared and defined in the public section of a class. A constructor does not have any return type. Therefore, it does not return anything, but it is not even void.

class_name (arguments if any){
   ...
   ...
};

A single class may have multiple constructors that are differentiated based on the number and type of arguments passed. There are three main types of constructors, namely, default constructor, parameterized constructor, and copy constructor.

What is a Destructor?

A destructor is a member function of a class that deallocates the memory allocated to an object. A destructor is also declared and defined with the same name as that of the class. A destructor is preceded by a tilde (~) symbol. A single class has only a single destructor.

~ class_name (no arguments){
   ...
   ...
};

A destructor does not have any argument and is always called in the reverse order of the constructor. Destructor are required for destroying the objects to release the memory allocated to them.

Difference between Constructor and Destructor

The following table highlights all the major differences between a constructor and a destructor −

Constructor Destructor
Constructors help allocate memory to an object. Destructors deallocate the memory of an object.
Constructors can take arguments. Destructors don't take any arguments.
Constructors are called automatically when an object is created. Destructors are called automatically when the block is exited or when the program terminates.
Constructors allow an object to initialize a value before it is used. They allow objects to execute code when it is being destroyed.
They are called in the successive order of their creation. They are called in the reverse order of their creation.
There can be multiple constructors in a single class. There is a single destructor in a class.
Constructors can be overloaded. Destructors cannot be overloaded.
The concept of copy constructor allows an object to get initialized from another object. There is no such concept in the case of destructors.

Conclusion

It is to be noted that both constructors and destructors are the special member functions of a class having the same name as that of the class. However, the most significant difference between a constructor and a destructor is that a constructor is called when memory is allocated to an object, while a destructor is called when the memory is needed to be deallocated. Also, both constructors and destructors are implicitly called by the compiler.

Updated on: 31-Oct-2023

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements