C++ destructors: which statement is correct about naming, return type, and overloading?
-
AA destructor has the same name as the class in which it is present.
-
BA destructor has a different name than the class in which it is present.
-
CA destructor always returns an integer.
-
DA destructor can be overloaded.
Answer
Correct Answer: A destructor has the same name as the class in which it is present.
Explanation
Introduction / Context:Destructors are special member functions that finalize objects. Their syntax, naming, and constraints differ from ordinary member functions. This question asks which property correctly describes a destructor according to the C++ language rules.
Given Data / Assumptions:
- We are considering ordinary classes (no exotic extensions).
- Focus on the destructor’s spelling, return type, and overloadability.
Concept / Approach:
A destructor’s declarator uses the class name preceded by a tilde: ~ClassName(). It has no return type (not even void) and cannot take parameters. There can be only one destructor per class (no overloading), though it may be virtual, defaulted, deleted, or user-defined. Therefore, the correct statement is that a destructor has the same name as the class (with a leading ~).
Step-by-Step Solution:
Identify the naming rule: ~ClassName() — same base name as the class. Check return type: none; not int or void. Check overloading: disallowed; only one destructor per class. Select the statement that matches these rules.Verification / Alternative check:
Any attempt to declare two destructors or to assign a return type will yield compilation errors, confirming uniqueness and lack of return type.
Why Other Options Are Wrong:
Different name — false per syntax.
Returns integer — destructors return nothing.
Can be overloaded — not allowed; only one destructor is permitted.
Common Pitfalls:
- Confusing virtual destructors (for polymorphic deletion) with “multiple destructors.” Virtuality does not imply overloading.
Final Answer:
A destructor has the same name as the class in which it is present.