What Is The Importance Of Constructors And Destructors In C++?


4 Answers

Rinav Gangar Profile
Rinav Gangar answered
Constructor and Destructor functions are Member Functions of a class having
some special property.

Constructor function is used to initialize member variables to pre-defined
values as soon as an object of a class is declared.

Constructor function gets invoked when an object of a class is constructed

So basically Constructor is used to initialize the objects (at the time of creation), and they are automatically invoked.

This saves us from the garbage values assigned to data members; during initializing.

Destructors have the opposite function of a constructor. The main use
of destructors is to release dynamic allocated memory. Destructors are
used to free memory, release resources and to perform other clean up.
Destructors are automatically named when an object is destroyed.
constructors, destructors also take the same name as that of the class

Balaji Gunasekar Profile
It is the same.The basic difference between constructor and destructor is constructor is called in the beginning of the program whereas the destructor is called at the end of the program.The former allocates memory whereas the later removes the memory.Constructor can be overloaded whereas the destructor cannot be overloaded.So constructor allocates memory for objects and member functions while the destructor destroys them at the end of the program to avoid wasted memory space.
Anonymous Profile
Anonymous answered
Importance of constructors  and destructors as member functions of an object
Rajat Dhande Profile
Rajat Dhande answered

Whenever a C++ program is executed, execution of the program starts and ends at main(). The main is the driver function of the program. If it is not present in a program, no execution can take place.

for more info in visite

Answer Question