Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a
member of our community. Consider creating an
account or login.
Constructor is used to allocate the memory space to the object data members. It is also used to do some initializations. eg if we want to initially set the values of data members to some default values say zero, then constructor is used. Another use of constructor is that it is called immediately when the object is created and initialized. So if want some actions to perform at this time then we can use it inside constructor. For instance, if we want that whenever an object is created, we should ask for some values from the users. so we can do this step in the constructor.
Constructors can be default constructor (with no parameters), parametrized constructor (with one or more parameter) and copy constructor (to copy one object to another). If we have not created any constructor inside the class definition, then the compiler automatically provides the default constructor to initialize values to zero.
Similarly, destructor is used to de-allocate the memory location occupied by the object data members. It works exactly opposite to that of constructor.
Also we can have one or more than one constructor in a class but we can have at the most only one destructor in the class.