+ 8
Constructor vs destructor ???
What's the functional difference between constrctor and destructor in a program?
8 odpowiedzi
+ 29
Sometimes it is required to initialize some part of an object before it can be utilized. For example, we are operating on the stack, before we perform any action, the top of the stack must always be set to zero. This feature of automatic initialization is performed through ‘Constructor’. Likely, if an object needs to execute some code before it is destroyed. For example, if an object needs to close a file that it had opened, before its destruction. It can be performed with the help of ‘Destructor’. Has same name as class but have ~ before it
+ 5
class Boy{
//constructor
Boy(){
}
//destructor
~Boy(){}
}
when an object is initialized constructor is called
Boy brains=new Boy();
destructor are automatically called from what I know
+ 4
constructors and destructors work against each other. while constructor creates or initializes, destructors deletes or diminishes a class.
+ 3
A constructor runs when the class is initiated and the deconstructor runs when the class is uninitiated
+ 3
Working:
Constructor
1.Constructor is a special member function whose task is to initialise an object of it's class.
2.constructor can accept arguments.
~ Destructor
1.Destructor is a special member function whose task is to destroy the object created by constructor.
2. Destructor cannot accept arguments.
+ 2
A constructor is used to initiate the class member and destructor is use to destroy the scope of object .
+ 1
Constructor is called when the object is created into the memory and destructor is called when object is deleted from the memory
0
ok got it.but in actual code how constructor and disconstructor works?