Difference Between Constructor and Destructor in C++
Constructor:
A constructor is a member function of a class that has the same name as the class name. It helps to initialize the object of a class. It can either accept the arguments or not. It is used to allocate the memory to an object of the class. It is called whenever an instance of the class is created. It can be defined manually with arguments are without arguments. There can be many constructors in class. It can neither be overloaded nor inherited but it can be virtual. There is a concept of copy constructor which is used to initialize a object from another object.
Syntax:
ClassName()
{
//Constructor's Body
}
Destructor:
Like constructor, deconstructor is also a member function of a class that has the same name as the class name preceded by a tilde(~) operator. It helps to deallocate the memory of an object. It is called while object of the class is freed or deleted. In a class, there is always a single destructor without any parameters so it can’t be oveloaded. It is always called in the reverse order of the constructor. if the a class inherited by another class and both the classes have destructor then the destructor of the child class is called first, followed by the destructor of the parent or base class.
Syntax:
~ClassName()
{
}
Example/Implementation of Constructor and Destructor:
class Z
{
// constructor
Z()
{
cout<<"Constructor called"<<endl;
}
// destructor
~Z()
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
Z z1; // Constructor Called
int a = 1
if(a==1)
{
Z z2; // Constructor Called
} // Destructor Called for z2
} // Destructor called for z1
Output:
Constructor called Constructor called Destructor called Destructor called
Difference between Constructor and Destructor in C++ :
| S.NO | Constructor | Destructor |
|---|---|---|
| 1. | Constructor helps to initialize the object of a class. | Whereas destructor is used to destroy the instances. |
| 2. | It is declared as Classname( arguments if any ){Constructor’s Body }. | Whereas it is declared as ~ ClassName( no arguments ){ };. |
| 3. | Constructor can either accept the arguments or not. | While it can’t have any argument. |
| 4. | A constructor is called when the instance or object of the class is created. | It is called while object of the class is freed or deleted. |
| 5. | Constructor is used to allocate the memory to an instance or object. | While it is used to deallocate the memory of an object of a class. |
| 6. | Constructor can be overloaded. | While it can’t be overloaded. |
| 7. | The constructor’s name is same as the class name. | Here, it’s name is also same as the class name preceded by tiled (~) operator. |
| 8. | In a class, there can be multiple constructors. | While in a class, there is always a single destructor. |
| 9. | There is a concept of copy constructor which is used to initialize a object from another object. | While here, there is no copy constructor concept. |
Recommended Posts:
- Order of Constructor/ Destructor Call in C++
- Is it possible to call constructor and destructor explicitly?
- Calling virtual methods in constructor/destructor in C++
- __attribute__((constructor)) and __attribute__((destructor)) syntaxes in C
- Virtual Destructor
- Private Destructor
- Pure virtual destructor in C++
- Constructor Overloading in C++
- Copy Constructor in C++
- Can a constructor be private in C++ ?
- Constructor Delegation in C++
- What is conversion constructor in C++?
- How to create a List with Constructor in C++ STL
- Passing a vector to constructor in C++
- Dynamic Constructor in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

