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’. Likely, 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’.
Let’s overview some of the basic differences between constructor and destructor with the help of 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 accepts 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 object to initialize some of its value before, it is used. | Destructor allows 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 constructor in the class. | But 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.
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 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 nad the value of “a, b” are printed. After that destructor get invoked and print the message “object C1 get destroyed”.
Key Difference Between Constructors and Destructors
- The main purpose of a constructor is to allocate memory to the objects when they are created whereas, 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. 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. On the other hand, a destructor is called when a program is terminated or the program exit the block in which object is created.
- A constructor is generally used to initialize the data members of the class whereas, a destructor are 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 derived class. On the other hand, the destructor of 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. In class, there is only one destructor.
- There is a concept of copy constructor which allows an object to get initialized from an another object whereas, the destructor has no such concept.
- Constructors can be overload to perform different action under the name of the same constructor whereas, destructors can not be overloaded.
Conclusion:
Besides the similarity, that constructor and destructor are the special member function of a class, and possess same name, the basic 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.
No comments:
Post a Comment
commnet here