Difference between constructor and destructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difference between constructor and destructor?

14th Nov 2016, 3:50 PM
Amit
3 Answers
+ 4
The constructor is a method defined in a class to do the initialization of an instance of the class (the object), while the purpose of the destructor, is to delete everything related with that instance. This is mostly used to close files, connections and clear the variables when an object is destroyed, so you ensure there will no be any memory leak.
14th Nov 2016, 4:04 PM
Alex Ruiz
Alex Ruiz - avatar
+ 1
constructor is a special member function in the class . it has no return type. it has same name as class name it is automatically invoked when you create an object for respected class it's main aim is allocate the memory to variable. destructor is special member function in the class. it is same name as class name before with tilde operator ~ it has no return type it's main aim is free the computers memory
15th Nov 2016, 3:22 PM
Kasireddy venkateswarlu
Kasireddy venkateswarlu - avatar
0
The constructor is the function which get called when object is created whereas destructor is the last function which is to be run before object is destroyed. In the other words contructor is the first function run by object at its iniatalization so we can do the work with the constructor which we want to do at the object initiation whereas destructor is the last function run by the object before it get destroyed so destructor cam we use to free the memory and to close the file!!
14th Nov 2016, 7:31 PM
ROHAN JOSHI
ROHAN JOSHI - avatar