What exactly is the use of Constructors and Destructors? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What exactly is the use of Constructors and Destructors?

It would be nice if examples are included in the answer.

8th Jul 2016, 12:27 PM
Neha Saxena
Neha Saxena - avatar
13 Answers
+ 5
constructors can be used to mark the beginning of class implementation and to initialize class variables, destructors can be used to free allocated memory
10th Jul 2016, 5:54 AM
Satya Prakash
Satya Prakash - avatar
+ 2
for initializ or assign member varible
8th Jul 2016, 1:37 PM
Bhautik Sutariya
Bhautik Sutariya - avatar
+ 1
constructors are used when class is instantiated while destructors are automatically invoked when an object is destroyed or deleted. eg cow { ~cow () // code statements } }
10th Jul 2016, 12:36 PM
Joyx Kish
Joyx Kish - avatar
+ 1
When would you want to free allocated memory? I get the how, I just don't understand the why
25th Jul 2016, 8:00 AM
ethan
ethan - avatar
+ 1
it can use for initialising the data members of the class and a message can be displayed when an object of the class is invoked
27th Nov 2016, 3:43 AM
Rahul
Rahul - avatar
0
Constructor is used to initialize the class variables and more importantly it is used to initialize const variables and reference variables which were not initialized at the time of their declaration. Sometimes constructors are very useful when we need to perform a type conversion from primitive type to user defined variable(object) type and vice-versa. Destructors are very useful for releasing the resources of an object before object dies. All we need to do is write delete operator with the object (object pointer) in the destructor of that object.
10th Jul 2016, 8:03 AM
Aditya Chauhan
Aditya Chauhan - avatar
0
constructors are used when class is instantiated while destructors are used when object is automatically invoked destroyed or deleted, eg class cow; { ~cow; { //code statements; } };
10th Jul 2016, 11:18 AM
Joyx Kish
Joyx Kish - avatar
0
allocation & deallocation of memory
14th Jul 2016, 11:43 AM
sankalp
0
destructor is used for dynamic memory allocation.we can overloading in constructor.destructor does not carry parameter so, its not do overloading
30th Jul 2016, 4:12 PM
Ajay Bamniya
Ajay Bamniya - avatar
0
constructors and destructers are special kind of member function .When we declare class or structure we can't intialise the data members present in the class her we use constructors to assign some default values .whenever the variable comes out for its scope .
5th Aug 2016, 11:10 PM
Shelcia Abi
Shelcia Abi - avatar
0
it's simple, Whenever you create an object of any class, the constructor of that class is got invoked and whatever the variables you had declared in the constructor would be assigned to the variables of calling class through the object. Constructor is used to initialize the the variables of calling class with some default values. The destructor destroys all the constructors you have created. Hope you understood!!!
26th Oct 2016, 8:41 AM
Taha Ansari
Taha Ansari - avatar
0
构建函数,正如字面意思上的,就是用于建立一个类,析构函数,则与之相反。鄙人的见解,不喜勿喷_(:з」∠)_
15th Nov 2016, 12:55 AM
idstudio_sl
idstudio_sl - avatar
0
we use constructer for code reusability...
23rd Nov 2016, 12:47 PM
Shivam kumar
Shivam kumar - avatar