Can a pointer or reference to an abstract class be declared in C++?
class Shape
{
public:
virtual void draw() = 0;
};
Shape *ptr;
Options
-
A.
No, neither pointer nor reference is allowed.
-
B.
Yes, a pointer or reference can refer to a derived concrete object.
-
C.
Only an integer pointer can be used.
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
Yes, a pointer or reference can refer to a derived concrete object.
Detailed Explanation
Answer: (B) Yes, a pointer or reference can refer to a derived concrete object.
Although an object of an abstract class cannot be created directly, pointers and references to the abstract class type are allowed.
For example, a Shape* pointer can point to an object of a concrete class derived from Shape.
This is one of the important uses of abstract classes: they provide a common interface through which different derived objects can be accessed.