what is the difference between constructor and destructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the difference between constructor and destructor

23rd Sep 2016, 5:32 AM
SAHIB SHAIKH
SAHIB SHAIKH - avatar
4 Answers
+ 1
A constructor is called when you create an object of the class, generally is used to assing your variables to the value you want. The destructor is called when the program end or when you call it, used to delete the memory assigned to the object.
23rd Sep 2016, 7:53 AM
Hector Sulbaran
Hector Sulbaran - avatar
+ 1
constructs are use to initialize the data members while destructors are use to de initialize. In java not need to use destructors as garbage collector take care of it
27th Sep 2016, 6:01 PM
Akshay Shelke
Akshay Shelke - avatar
0
As Hector said, constructors are called when you create an object. But you don't call a destructor and they are not called just when you program finishes. Destructors are called when your object is destroyed. It can be when it runs out of scope, like: void function() { YourClass obj; // When the program exitis this function, obj's destructor is called } Or you can explicitly do it by: obj = NULL
23rd Sep 2016, 12:17 PM
Vinícius Dias
Vinícius Dias - avatar
0
1. constructor are used to initialize the data member of the class not creating the object. 2. destructor are used to deallocate the memory location. like a sweeper we put aside the important thing safe before sweeper clean so that we can use it later. 3. both constructor and destructor have same name as that of class name but destructor is followed by a (~)tilde sign. 4. constructor have no return type 5 constructor have no arguments. 6. constructor cannot be inherited to the child class. 7. destructor can be inherited.
25th Sep 2016, 7:15 AM
ANKUSH CHAUHAN
ANKUSH CHAUHAN - avatar