Which of the following C++ statements correctly declares an abstract class?

2024

Which of the following C++ statements correctly declares an abstract class?

  1. A.

    class A { virtual void show()=0; };

  2. B.

    class A { void show()=0; };

  3. C.

    class A { void show() { } };

  4. D.

    class A { show()=0; };

Attempted by 101 students.

Show answer & explanation

Correct answer: A

Key point: A class becomes abstract in C++ if it has at least one pure virtual function. A pure virtual function is declared with the virtual keyword and =0.

Correct declaration example:

class A { virtual void show()=0; };

  • class A { void show()=0; }; — Invalid: =0 requires the function to be declared virtual. Without the virtual keyword this is ill-formed.

  • class A { void show() { } }; — This provides a function body, so the class is concrete (not abstract) and can be instantiated.

  • class A { show()=0; }; — Invalid syntax: missing a return type (e.g., void) and the virtual keyword; this is not a valid declaration.

Because the correct declaration contains a pure virtual function, the class cannot be instantiated directly; derived classes must override the pure virtual function to become concrete.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor