In C++, a class's destructor has the same base name as the constructor but is prefixed by which symbol? Select the correct leading character used in the destructor's syntax.

Difficulty: Easy

Correct Answer: ~

Explanation:


Introduction / Context:
Destructor syntax is intentionally simple and uniform across classes so programmers can quickly recognize cleanup code. The destructor name is derived from the class name using a specific prefix character.


Given Data / Assumptions:

  • Class name is, for example, Widget.
  • We want the exact symbol that denotes a destructor.
  • Syntactic correctness matters for compilation.


Concept / Approach:
The destructor is written as ~ClassName(). For a class Widget, the destructor is ~Widget(). No return type or parameters are allowed. The leading character is the tilde symbol, not any other punctuation character.


Step-by-Step Solution:
1) Recall: constructor → Widget(); destructor → ~Widget().2) Note there is no return type and no parameters for the destructor.3) Identify the prefix character: ~ (tilde).4) Select the correct option accordingly.


Verification / Alternative check:
Compile a class with ~ClassName(); omitting the tilde or using other symbols leads to errors.



Why Other Options Are Wrong:
! ? $ #: not part of destructor syntax; using them will not compile.



Common Pitfalls:
Attempting to add a return type to ~ClassName or parameters; both are illegal in C++.



Final Answer:
~

More Questions from Constructors and Destructors

Discussion & Comments

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