Which statement is correct about constructors and default parameters in C++? Pick the option that matches the language capabilities.

Difficulty: Easy

Correct Answer: Constructors can have default parameters.

Explanation:


Introduction / Context:
Constructors are ordinary functions with special roles. They follow most of the same parameter rules, including the ability to specify default values to streamline object creation across common cases.


Given Data / Assumptions:

  • C++ default-argument rules apply to constructors as well.
  • Defaults must be trailing, and they are specified in a declaration.
  • There is no arbitrary upper bound enforced by the language on how many defaults a constructor may have.


Concept / Approach:
Because constructors are functions, they can certainly include default parameters. This enables multiple construction patterns without writing numerous overloads. The same caveats apply: avoid ambiguous overloads, and place defaults only at the right end of the parameter list (or default all remaining parameters to the right).


Step-by-Step Solution:
1) Legal: struct T { T(int x = 0, int y = 0); };2) Calls: T a; T b(5); T c(5, 6); // all valid thanks to defaults3) There is no rule forbidding multiple defaults; the “five parameter” suggestion is fictitious.4) Therefore, the correct statement is that constructors can have default parameters.


Verification / Alternative check:
Implement such a constructor and instantiate objects with 0, 1, and 2 arguments; observe successful compilation and execution.



Why Other Options Are Wrong:
Cannot have defaults: contradicts standard practice.“Cannot have more than one” and “at most five”: both are arbitrary and false.



Common Pitfalls:
Combining defaulted constructors with similarly shaped overloads may create ambiguous calls; design parameter sets to avoid ambiguity.



Final Answer:
Constructors can have default parameters.

More Questions from Functions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion