C++ constructors: which statement is correct about naming, return type, and overloading?

C++ Programming Constructors and Destructors Difficulty: Easy
Choose an option
  • A
    A constructor has the same name as the class in which it is present.
  • B
    A constructor has a different name than the class in which it is present.
  • C
    A constructor always returns an integer.
  • D
    A constructor cannot be overloaded.

Answer

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:

  • We review naming, return type, and overloading for constructors.
  • Applies to regular classes under standard C++.

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:

Confirm naming rule: ClassName(...); — same base name as the class. Confirm return type: none; constructors do not return values. Confirm overloading: allowed with distinct parameter lists. Choose the statement aligned with these facts.

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:

  • Assuming default constructor appears automatically even when other constructors are provided; rules depend on declarations.

Final Answer:

A constructor has the same name as the class in which it is present.

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