In C++, how do you make a class abstract (i.e., uninstantiable and intended as an interface/base)? Select the standard language feature used to enforce abstractness.

Difficulty: Easy

Correct Answer: Making at least one member function as pure virtual function.

Explanation:

Introduction:Abstract classes model shared interfaces/behaviors that derived classes must implement. This question asks which exact C++ mechanism enforces that a class cannot be directly instantiated.

Given Data / Assumptions:

  • You want a base class that cannot be constructed.
  • Language: C++ with virtual dispatch semantics.
  • Need a canonical, standard-compliant technique.

Concept / Approach:A class becomes abstract if it declares (and does not override with a definition) at least one pure virtual function, written with ”= 0“. This communicates that derived classes must supply an override before they become concrete. Merely having virtual functions is not sufficient; they can still have definitions and the class remains instantiable if no pure virtuals exist.

Step-by-Step Solution:1) Identify a function that must be implemented by derived types (e.g., draw()).2) Declare it pure virtual in the base: virtual void draw() = 0;3) Because at least one pure virtual exists, the base class becomes abstract.4) Any concrete derived class must implement draw() to be instantiable.

Verification / Alternative check:Attempting ”Base b;“ where Base has a pure virtual function triggers a compile-time error stating that the type is abstract.

Why Other Options Are Wrong:static: unrelated to abstractness; affects storage/linkage.virtual keyword alone: enables dynamic dispatch but does not force abstractness.“virtual function” (non-pure): class can still be instantiated.

Common Pitfalls:Forgetting to provide a virtual destructor in abstract bases used polymorphically. Declaring a pure virtual destructor still requires a definition.

Final Answer:Making at least one member function as pure virtual function.

More Questions from OOPS Concepts

Discussion & Comments

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