Difficulty: Easy
Correct Answer: A constructor has the same name as the class in which it is present.
Explanation:
Introduction / Context:
Constructors are special member functions responsible for initializing objects. Their naming and type rules differ from ordinary functions. This question asks which statement best matches the language definition for constructors in C++.
Given Data / Assumptions:
Concept / Approach:
A constructor has the same name as its class and has no return type (not even void). Constructors can be overloaded: you may define multiple constructors with different parameter lists (default, parameterized, copy, and move constructors). Therefore, the correct statement is that the constructor’s name matches the class name. The distractors claiming different names, integer return type, or prohibition on overloading are all incorrect.
Step-by-Step Solution:
Verification / Alternative check:
Define multiple constructors with different parameters; compilation succeeds, demonstrating overloading. Any attempt to specify a return type fails to compile.
Why Other Options Are Wrong:
Different name — violates constructor syntax.
Returns integer — constructors have no return type.
Cannot be overloaded — false; overloading is idiomatic in C++.
Common Pitfalls:
Final Answer:
A constructor has the same name as the class in which it is present.
Discussion & Comments