+ 3
What exactly is the use of Constructors and Destructors?
It would be nice if examples are included in the answer.
13 Réponses
+ 5
constructors can be used to mark the beginning of class implementation and to initialize class variables, destructors can be used to free allocated memory
+ 2
for initializ or assign member varible
+ 1
constructors are used when class is instantiated while destructors are automatically invoked when an object is destroyed or deleted. eg
cow
{
~cow ()
// code statements
}
}
+ 1
When would you want to free allocated memory? I get the how, I just don't understand the why
+ 1
it can use for initialising the data members of the class and a message can be displayed when an object of the class is invoked
0
Constructor is used to initialize the class variables and more importantly it is used to initialize const variables and reference variables which were not initialized at the time of their declaration. Sometimes constructors are very useful when we need to perform a type conversion from primitive type to user defined variable(object) type and vice-versa.
Destructors are very useful for releasing the resources of an object before object dies. All we need to do is write delete operator with the object (object pointer) in the destructor of that object.
0
constructors are used when class is instantiated while destructors are used when object is automatically invoked destroyed or deleted, eg
class cow;
{
~cow;
{
//code statements;
}
};
0
allocation & deallocation of memory
0
destructor is used for dynamic memory allocation.we can overloading in constructor.destructor does not carry parameter so, its not do overloading
0
constructors and destructers are special kind of member function .When we declare class or structure we can't intialise the data members present in the class her we use constructors to assign some default values .whenever the variable comes out for its scope .
0
it's simple,
Whenever you create an object of any class, the constructor of that class is got invoked and whatever the variables you had declared in the constructor would be assigned to the variables of calling class through the object.
Constructor is used to initialize the the variables of calling class with some default values.
The destructor destroys all the constructors you have created.
Hope you understood!!!
0
构建函数,正如字面意思上的,就是用于建立一个类,析构函数,则与之相反。鄙人的见解,不喜勿喷_(:з」∠)_
0
we use constructer for code reusability...