20 Apr 2017

Difference Between Constructor and Destructor


Constructor Vs DestructorSometimes 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

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison chart:

BASIS FOR COMPARISONCONSTRUCTORDESTRUCTOR
Purpose
It allocates the memory to an object.It deallocates the memory of an object.
Declarationclass_name( arguments if any ){ };~ class_name( no arguments ){ };
ArgumentsConstructor accepts argumentDestructor does not accepts any argument.
CallingConstructor is called automatically, while the object is created.Destructor is called automatically, as block is exited or program terminates.
WorkingConstructor 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 numbersThere can be multiple constructor in the class.But there is always a single destructor in the class.
Copy ConstructorCopy constructor allows a constructor to declare and initialize a object from another object.No such concept.
Over loadingConstructors 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:

  1. class Const{
  2. int a, b;
  3. public:
  4. Const(){ // constructor with no parameter
  5. a=0;
  6. b=0;
  7. }
  8. Const(int c, int d){ //constructor with parameter
  9. a=c;
  10. c=d;
  11. }
  12. };
  13. int main(){
  14. Const C1; C2(10,20); //this statement invokes constructor
  15. }
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:

  1. class Const{
  2. int a, b;
  3. public:
  4. Const(int c, int d){ //constructor with parameter.
  5. a=c;
  6. c=d;
  7. cout<<"value of a and b are"<<a<<b<<"\n";
  8. }
  9. ~Const(){ //destructor being called.
  10. cout<<"object C1 get destroyed"<<"\n";
  11. }
  12. };
  13. int main(){
  14. Const C1(10,20);
  15. }
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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. In class, there can be multiple constructors which are identified by the number arguments passed. In class, there is only one destructor.
  7. 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.
  8. 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