Which of the following is the correct syntax for declaring a pure virtual function in C++?

class Base
{
public:
    virtual void display() = 0;
};
                        

Options

  • A. virtual void display();
  • B. virtual void display() = 0;
  • C. void virtual display() = 1;
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. virtual void display() = 0;

Detailed Explanation

Answer: (B) virtual void display() = 0;

A pure virtual function is declared by using the virtual keyword and assigning = 0 to the function declaration.

For example:

virtual void display() = 0;

The = 0 does not mean that the function returns zero. It specifies that the function is pure virtual.

A class containing at least one such function becomes an abstract class.