What is abstract class in C++?

2023

What is abstract class in C++?

Answer: C. Class specifically used as a base class with at least one pure virtual functionConcept: In C++, a class is abstract when it has at least one pure virtual function whose final overrider in that class remains pure, either because the class…

  1. A.

    Any class in C++ is an abstract class/Class from which any class is derived

  2. B.

    Class specifically used as a base class with at least one virtual function

  3. C.

    Class specifically used as a base class with at least one pure virtual function

  4. D.

    More than one of the other descriptions is correct

  5. E.

    None of the other descriptions is correct

Attempted by 437 students.

Show answer & explanation

Correct answer: C

Concept: In C++, a class is abstract when it has at least one pure virtual function whose final overrider in that class remains pure, either because the class itself declares such a function (written with the pure-specifier "= 0", for example virtual void draw() = 0;), or because it inherits one without providing an override. Because at least one such function's final overrider is pure, the compiler will not allow an object of the class to be created directly; the class can only be used as a base class, and a class derived from it becomes concrete only once every such pure virtual function is given a non-pure overriding definition.

Application: The five answer descriptions differ in what they require of a class. The one that matches the ISO C++ rule must satisfy both parts of it at once: the class is meant to serve as a base class, AND it has at least one pure virtual function, typically declared with the pure-specifier "= 0". A description that drops the word "pure", or drops the virtual-function requirement altogether, describes something broader than an abstract class.

class Shape {
public:
    virtual void draw() = 0; // pure virtual, Shape abstract
};

class Base {
public:
    virtual void show(); // ordinary virtual, Base not abstract
};

Cross-check: Shape cannot be instantiated directly (Shape s; fails to compile) because draw() is pure virtual, while Base compiles fine as a concrete class because show() is an ordinary virtual function, for which providing an override in a derived class is optional, not mandatory. This confirms that the pure-virtual-function requirement, not the mere presence of any virtual function or of being a base class alone, is what decides whether a class is abstract.

Contrasting each description against the rule:

  • "Any class in C++ is an abstract class / class from which any class is derived" drops the pure-virtual-function requirement entirely; it covers ordinary concrete classes too, so it is broader than the rule.

  • "Base class with at least one virtual function" allows an ordinary (non-pure) virtual function; such a class can still be instantiated directly, so it does not meet the "pure" requirement.

  • "Base class with at least one pure virtual function" satisfies both parts of the rule at once: intended as a base class, and carrying a pure virtual function.

  • "More than one of the other descriptions is correct" would need at least two of the descriptions to each independently satisfy the rule; only one does.

  • "None of the other descriptions is correct" would apply only if no description matched the rule; one does.

Result: the description "class specifically used as a base class with at least one pure virtual function" is the one that matches the C++ standard's definition of an abstract class (ISO C++, [class.abstract]; cppreference.com/w/cpp/language/abstract_class).

Explore the full course: Rssb Senior Computer Instructor

Loading lesson…