0
Difference between constructor and destructor?
3 Respostas
+ 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.
+ 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
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!!