-1

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 ?

1 Answers1

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