+ 1
what is the difference between constructor and destructor
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.
+ 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
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
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.