Which statement correctly describes constructor and destructor naming rules in C++?

Difficulty: Easy

Correct Answer: Both A and B.

Explanation:


Introduction / Context:
Constructors and destructors are special member functions tightly bound to a class's identity. Their names follow strict syntactic rules so the compiler can recognize them and apply lifetime semantics. This question asks which statements about those names are correct.


Given Data / Assumptions:

  • A class is named, e.g., Gadget.
  • Constructors and destructors are declared within the class.
  • We compare naming forms.


Concept / Approach:
A constructor's name exactly matches the class name (Gadget::Gadget). A destructor's name matches the class name prefixed by a tilde (Gadget::~Gadget). This uniform naming enables the compiler to apply special rules: no return type, constructor overloading, and implicit invocation on creation/destruction.


Step-by-Step Solution:
1) For class Gadget, constructor is Gadget().2) Destructor is ~Gadget().3) Both statements together accurately describe the naming scheme.4) Therefore, choose “Both A and B.”


Verification / Alternative check:
Try compiling a class with differently named constructor or destructor; the compiler will not treat them as special members.



Why Other Options Are Wrong:
Naming a destructor after the first member function is meaningless.Constructors do not begin with a tilde; only destructors do.



Common Pitfalls:
Accidentally giving a return type to constructors/destructors or misspelling the class name, which turns them into ordinary functions and breaks object lifetime semantics.



Final Answer:
Both A and B.

More Questions from Constructors and Destructors

Discussion & Comments

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