constructor vs destructor
1 minute read
Difference between constructor and destructor.
Constructor | Destructor |
Constructor is Used to Initialize the Object. | Destructor is used to destroy the object that are previously created in memory. |
Constructor can takes arguments. | Destructor cannot take any arguments. |
Overloading of constructor can be possible means more than one constructor can be defined in same class. | Destructor overloading cannot be possible. |
Constructor has same name as class name. | Destructor has same name as class name with sign of tiled operator. |
Syntax of constructor: class class_name { clas_name(){} class_name(argulist){} } ; | Syntax of Destructor: class class_name { ~class-name(void){} }; |
Constructor has three types 1)Default Constructor. 2)Parameterized Constructor. 3)Copy Constructor. | Destructor has no any types. |
It is used to dynamically initialize the memory. | It can be used to deallocate the memory. |
Difference between constructor and member function
Constructor | Function |
Constructors get automatically called when its class object is created. | Function has to be explicitly called. |
Constructors need to be having the same name as class. | Function not to be the same name as class. |
There is no return statement with the body of the constructor. | There is a return statement in the body of the function. |