What is a pure virtual function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is a pure virtual function?

23rd Oct 2016, 6:19 PM
Venus
Venus - avatar
2 Answers
+ 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.
23rd Oct 2016, 7:56 PM
Stefan
Stefan - avatar
+ 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.
24th Oct 2016, 8:19 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar