Constructor and Destructor are the special methods which makes our crucial tasks easier while programming. If you have ever worked in programming language like C++ then you must have encountered these two terms. Being methods, they clearly let us implement the reusability of the code. But the talk of the town (esp. for the beginners) has clearly been the differences among them, their types, usage, etc. The somewhat rhyming names they possess and both being simply the types of the method or function makes it even more difficult for a beginner to understand the difference.
Here is a simple article, which clearly differentiates the two is sure to strike the right the chord in a programmer’s mind.
Difference Between Constructor and Destructor
|S.no.||Parameter of Difference||Constructor||Destructor|
|1.||Initialization||Initialized with the same as that of the class name. i.e.,
Example: if the class name is maxim, the constructor for the same will be maxim()
|Also initialized with the same name as that of the class name, but with a tilde (~) sign before the class name.
Example: if the class name is maxim, the constructor for the same will be ~maxim()
|2.||Objective||Creates the class objects.
Or, When a constructor is called, it allocates the memory.
|Used for destroying the objects. i.e., when a destructor is called, memory is released.|
|3.||Parameters/arguments||Accepts the arguments.
|Does not contain any arguments.
Example : maxim()
|4.||Overloading||Posses Overloading property. Multiple constructors exist in a class at a time.||Does not possess the property of overloading; (Also there is no need of destructor overloading). Only one destructor can exist in one class at a time.|
|5.||Invoking||As soon as the object is created, a constructor is called by default. Also, they are called in successive order; in the order they are designed.||When the concerned block is encountered or if the program is terminated, Destructor is called. They are called in the reverse order of the design.|
|6.||Types||Constructor can be of types: Default (with no arguments) and parameterized (with arguments).||Destructor can never be parameterized (as mentioned earlier).|
|7.||Copying the values||The copy constructor lets us declare and initialize the object from some other object.||There is no like functionality in the Destructor.|
//body of the constuctor
//body of the destructor
However similar they may sound, constructors and destructors are quite a pole apart and this can be easily perceived from the above mentioned differences.
Source link https://www.thecrazyprogrammer.com/2018/06/difference-between-constructor-and-destructor.html