Constructor and destructor are the member functions with the same name as their class. The former type constructor helps in initializing an object. Conversely, a destructor is different from the constructor which deletes the created constructor when it is of no use.
Sometimes it is required to initialize some part of an object before it can be utilized. For example, we are operating on the stack, before we perform any action, the top of the stack must always be set to zero. This feature of automatic initialization is performed through ‘Constructor’.
Like, if an object needs to execute some code before it is destroyed. For example, if an object needs to close a file that it had opened, before its destruction. It can be performed with the help of ‘Destructor’. Now, let’s overview some of the fundamental differences between constructor and destructor with the help of a comparison chart
Content: Constructor Vs Destructor
Comparison chart:
Basis for comparison | Constructor | Destructor |
---|---|---|
Purpose | It allocates the memory to an object. | It deallocates the memory of an object. |
Declaration | class_name( arguments if any ){ }; | ~ class_name( no arguments ){ }; |
Arguments | Constructor accepts argument | Destructor does not accept any argument. |
Calling | Constructor is called automatically, while the object is created. | Destructor is called automatically, as block is exited or program terminates. |
Working | Constructor allows an object to initialize some of its value before, it is used. | Destructor allows an object to execute some code at the time of its destruction. |
Order of execution | Constructor are called in successive order. | Destructor are called in reverse order of constructor. |
In numbers | There can be multiple constructors in a class. | There is always a single destructor in the class. |
Copy Constructor | Copy constructor allows a constructor to declare and initialize a object from another object. | No such concept. |
Over loading | Constructors can be overloaded. | Destructor can not be overloaded. |
Definition of Constructor
A constructor is basically a member function of class, which initializes the object and allocates memory to it. Constructors can be easily identified as they are declared and defined with the same name as that of the class. A constructor does not have any return type; so, they do not return anything, not even ‘void’. A Constructor is always defined in the public section of a class.
There can be multiple constructors in a class; they can be distinguished based on the number and type of arguments passed. If there are multiple constructors in a class; implicit constructor (do-nothing constructor) must be defined along with them; it does nothing but, satisfies the compiler.
Constructors can also be defined with the default arguments. Whereas, they also initialize the object “dynamically”. Constructors can neither be inherited, nor it can be virtual but, they can be overloaded. They can’t be referred to their address.
Types of Constructors
There are basically three types of constructors- Default, Parameterized, and Copy Constructors.
- Default Constructor: It is a constructor where no argument is given to the constructor. Default constructor has no parameter, but the values to the default constructor can be passed by default (dynamically).
- Parameterized Constructor: This type of constructor take the arguments; we can pass different values to the data members as arguments.
- Copy Constructor: Copy constructor is different from other types of constructors because it accepts the address of the other object as an argument.
Implementation of constructor
class Const { int a, b; public: Const() // constructor with no parameter { a=0; b=0; } Const(int c, int d){ //constructor with parameter a=c; c=d; } }; int main(){ Const C1; C2(10,20); //this statement invokes constructor }
When C1 is created a constructor with no parameter gets executed, as C1 do not pass any parameter. Whereas, when C2 is created a constructor with parameter gets executed, as it is passing two integers to the constructor.
Definition of Destructor
A Destructor is also a member function of a class, which deallocates the memory allocated to an object. It is defined with the same name as that of a class, preceded by a tilde(~) symbol. Destructors are always called in the reverse order of the constructors.
There is always a single destructor in a class, as it does not accept any arguments. Local objects are destroyed as soon as the control of the execution lefts the block; on the other hand, global objects are destroyed when the entire program terminates.
A destructor is implicitly called by a compiler. If the classes are inherited, and a class is derived from a parent class, and both the child class and a parent class have destructors; then, the destructor of the derived class is called first, followed by the destructor of the parent class.
Implementation of Destructor
class Const { int a, b; public: Const(int c, int d) //constructor with parameter. { a=c; c=d; cout<<"value of a and b are"<<a<<b<<"\n"; } ~Const() //destructor being called. { cout<<"object C1 get destroyed"<<"\n"; } }; int main(){ Const C1(10,20); }
When C1 object is created, a constructor with two parameters of integer type is invoked and the member “a, b” gets initialized and the value of “a, b” are printed. After that destructor gets invoked and prints the message “object C1 get destroyed”.
Need of Destructor
The creation of the constructor consumes some amount of memory space, as it ultimately allocates memory to the objects. This allocated memory must be deallocated prior to destroying the objects to free up the resources for other tasks. Destructors are extremely useful for the intended purpose, which effectively destroys objects and carries out clean-up tasks for releasing the memory.
Key Difference Between Constructors and Destructors
- The primary purpose of a constructor is to allocate memory to the objects when they are created. As against, the main purpose of a destructor is to deallocate memory of the object when it is destroyed.
- A constructor is allowed to accept the arguments as the arguments can be used to initialize the data members of the class. On the other hand, a destructor does not accept any arguments as its only work is to deallocate the memory of the object.
- A constructor is called when an object is created. In contrast, a destructor is called when a program is terminated or the program exit the block in which an object is created.
- A constructor is generally used to initialize the data members of the class, whereas a destructor is used to let the object perform some action before it is destroyed.
- Constructors are executed in the successive order that means if there is a derived class that inherits the base class and the object of the derived class is created then it will call the constructor of base class first and then the constructor of the derived class. Conversely, the destructor of the derived class is called first and then the base class it means that a destructor is executed in reverse order of constructor.
- In class, there can be multiple constructors which are identified by the number arguments passed whereas it can have only one destructor.
- There is a concept of copy constructor which allows an object to get initialized from another object while the destructor has no such concept.
- Constructors can be overload to perform different action under the name of the same constructor. On the contrary, destructors can not be overloaded.
Conclusion
Besides the similarity, that constructor and destructor are the special member function of a class and possess the same name, the essential difference among both of them is, ‘constructor’ is called at the time of memory allocation and ‘destructor’ is called at the time of objects memory deallocation.
Both constructor and destructor are implicitly called by compiler even though they are not defined in the class.
Bedan says
Great work in clearly explaining the differences.