I am new in C++, I want to know, why the pure virtual function is used in c++ ?. Somewhat I know to make class abstract and force all derived Classes to have implementation. But I want to know what is the situation where only pure virtual function can work ?
Asked
Active
Viewed 38 times
-1
-
[This](https://www.tutorialspoint.com/pure-virtual-functions-and-abstract-classes-in-cplusplus) is what a google search brings up – kesarling He-Him May 30 '20 at 07:20
-
A class with only pure virtual functions would be called an interface in other languages. – Lukas-T May 30 '20 at 07:21
-
7Does this answer your question? [Why do we need virtual functions in C++?](https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c) – Abhishek Bhagate May 30 '20 at 07:27
-
@Abhishek, I believe his question is only about pure virtual function (I may be wrong though) :) – kesarling He-Him May 30 '20 at 07:31
-
This will help you out : [Why do we use virtual functions in c++?](https://stackoverflow.com/q/2391679/12461060) – RNGesus.exe May 30 '20 at 09:39
1 Answers
1
Assume you have a certain base class defining a type like GeometricForm
which has a virtual function float calculateArea()
. You cannot calculate the area for GeometricForm
because GeometricForm
itself does not define a geometric form (hope this is understandable). But you can create a derived class e.g. Circle
of this base class which is an actual geometric form.
You now want to be sure that this derived class implements a valid version of your former virtual function calculateArea()
, which will be ensured by the compiler.

po.pe
- 1,047
- 1
- 12
- 27
-
Thanks for the answer. As you mentioned that GeometricForm is base class. So, in this case we create a pointer of base class ( GeometricForm) and assign object of derived class (Circle) under some condition (like if user is working on Circle we assign object of Circle to base class pointer ). But this can be done using only the classes like Circle and Triangle etc. That means if user is working on Circle then Create object of Circle and call calculateArea function of Circle. So far it's fine. So, my question is - what is that scenario where only pure virtual function will work? – Amol GAIKWAD May 30 '20 at 15:24