C++ OOP: which is the correct syntax to declare a pure virtual function? Choose the exact declaration form used in an abstract base class.

Difficulty: Easy

Correct Answer: virtual void Display(void) = 0;

Explanation:


Introduction / Context:
In C++, a pure virtual function is a function in a base class that has no implementation in that class and must be overridden in derived classes. Declaring a pure virtual function makes the class abstract, meaning you cannot instantiate it directly. This question tests your ability to recognize the precise syntax that the C++ language uses to mark a virtual member function as pure (abstract).


Given Data / Assumptions:

  • Target declaration name: Display.
  • Return type: void.
  • Parameter list: (void) — i.e., no parameters.
  • Goal: mark the function as pure virtual in an abstract base class.


Concept / Approach:

The correct form for a pure virtual member function is: return_type function_signature = 0; combined with the virtual keyword in the class definition. The “= 0” portion is not an assignment; it is grammatically special and tells the compiler that the function has no definition in the current class and must be overridden (unless the derived class is also abstract). You may still provide an out-of-class definition for a pure virtual function in rare cases, but the class remains abstract until all pure virtuals are overridden in a concrete derived class.


Step-by-Step Solution:

1) Start with a normal virtual declaration: virtual void Display(void); 2) Add the pure specifier: = 0; directly after the closing parenthesis. 3) Final form inside the class: virtual void Display(void) = 0; 4) Result: The class becomes abstract and cannot be instantiated.


Verification / Alternative check:

Attempting to instantiate a class containing at least one pure virtual function results in a compile-time error. A derived class that overrides Display with an exact-matching signature becomes instantiable provided no other pure virtuals remain unimplemented.


Why Other Options Are Wrong:

virtual void Display(void){0}; — A function body with “{0}” is not the pure-virtual syntax; it attempts a definition.

virtual void Display = 0; — Missing the parameter list; not a valid member function declaration.

void Display(void) = 0; — Missing virtual; the pure marker applies to virtual functions only.


Common Pitfalls:

  • Confusing pure virtual with “unimplemented” non-virtual; only virtual members can be pure.
  • Forgetting that a destructor can also be pure virtual (often to enforce abstractness) but still should have a definition.


Final Answer:

virtual void Display(void) = 0;

Discussion & Comments

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