Which of the following constructors can be provided implicitly by the C++…
2023
Which of the following constructors can be provided implicitly by the C++ compiler if they are not defined in a class?
Answer: D. More than one of the above — In C++, when a class does not explicitly declare certain special member functions, the compiler implicitly generates a limited set of them so that basic…
- A.
Copy constructor
- B.
Default constructor
- C.
Parameterized constructor
- D.
More than one of the above
- E.
None of the above
Attempted by 424 students.
Show answer & explanation
Correct answer: D
In C++, when a class does not explicitly declare certain special member functions, the compiler implicitly generates a limited set of them so that basic object creation and copying still work. This applies to the default constructor and the copy constructor, but never to a parameterized constructor — the compiler cannot infer what arguments or custom initialization logic a parameterized constructor should use, so that one must always be written explicitly by the programmer.
Default constructor: the compiler implicitly declares one whenever the class has no user-declared constructor of any kind — this condition is satisfied here, so a default constructor can be compiler-provided.
Copy constructor: the compiler also implicitly declares one whenever the class has no user-declared copy constructor, although it is defined only if actually used and can be deleted in special cases (e.g. classes with move-only members, or members whose own copy constructor is deleted or inaccessible) — this condition is likewise satisfied here.
Parameterized constructor: this is never synthesized by the compiler; it always requires an explicit programmer-written definition, so it is not compiler-providable.
Because both the default constructor and the copy constructor meet the compiler’s implicit-generation conditions here — not just one of them — "more than one of the above" is the correct selection. Choosing only "default constructor" overlooks that the copy constructor is equally compiler-providable under the same class conditions, and "none of the above" is wrong because at least one (in fact two) of the listed constructor types can indeed be compiler-provided.