Consider the following C++ code. Which statement is correct?

#include <iostream>
using namespace std;
class A {
public:
    virtual void f() = 0;
};
int main() {
    A* p = nullptr;
    return 0;
}
                        

Options

  • A. The code is invalid because a pointer to an abstract class is forbidden
  • B. The code is valid because a pointer to an abstract class can be declared
  • C. The code creates an object of class A
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. The code is valid because a pointer to an abstract class can be declared

Detailed Explanation

Correct Answer: Option B

Explanation:
A pointer or reference to an abstract class is allowed. The restriction is on creating an object of the abstract class itself. Here, p is a null pointer and no object of A is created, so the code is valid.