Google News
logo
CPP - Quiz(MCQ)
Why constructors are efficient instead of a function init() defined by the user to initialize the data members of an object?
A)
Because user may forget to define init() function
B)
Because user may call init() more than once which leads to overwriting values
C)
Because user may forget to call init() using that object leading segmentation fault
D)
All of the above

Correct Answer :   All of the above


Explanation : We cannot use init() because as mentioned in options that user may forget to initialize the members which will lead to a segmentation fault. Also if the user calls the init() function more than once it may overwrite the values and may result into disastrous results. Also if any user forgets to define init() function then no object will be initialized whereas if any constructor is not defined in any class the class provides a default constructor for initialization.

Advertisement