0
What is a pure virtual function?
2 odpowiedzi
+ 1
A pure virtual function does not have an implementation (function body). It is a guarantee that a child of this class that wants to be instantiated must implement it.
+ 1
Take universal example Shape class.
class Shape
{
public:
virtual void draw (){}
};
Now there is no point of creating a object for this class. Because we don't have any information about the shape to be draw. So to restrict from creating a object for undefined behaviour. We are declaring the draw method as pure virtual method. like this
virtual void draw() = 0;
It means,
1. You're not allowed to create a object for this class.
2. Derived class should override all pure virtual methods of base.
3. It means YOU ARE ALLOWED TO HAVE METHOD DEFINITION for pure virtual methods in shape class itself. Still you won't able to create object. This is allowed to do some initialization or common task irrespective of derived class.
In Our example, you may have to initialize the device context for draw.
Hope you understand and clear. If possible/you need I will share the example via code playground.