What happens if an attempt is made to create an object of an abstract class in C++?
class Shape
{
public:
virtual void draw() = 0;
};
Shape s;
Options
-
A.
The object is created successfully.
-
B.
Compilation error occurs.
-
C.
The object is automatically converted into a derived object.
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
Compilation error occurs.
Detailed Explanation
Answer: (B) Compilation error occurs.
An abstract class cannot be instantiated directly.
In the example, Shape contains the pure virtual function draw(), so Shape is abstract.
Therefore, the statement Shape s; attempts to create an object of an abstract class and results in a compilation error.
Objects can instead be created from a concrete derived class that provides implementations for the required pure virtual functions.