Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

C++ error: object of abstract class type is not allowed: pure virtual function has no overrider

Writer Matthew Barrera

Having trouble with inheritance. I have no idea what I'm doing wrong.

FigureGeometry.h

 #ifndef FIGUREGEOMETRY
#define FIGUREGEOMETRY
static const float PI = 3.14159f;
class FigureGeometry
{
public: virtual float getArea() const = 0; virtual float getPerimeter() const = 0;
};
#endif

Circle.h

 #ifndef CIRCLE
#define CIRCLE
#include "FigureGeometry.h"
class Circle:public FigureGeometry
{ float radius;
public: Circle(float theRadius) { radius = theRadius; } float getRadius() {return radius;} float getArea() {return getRadius() * getRadius() * PI;} float getPerimeter() {return getRadius() * 2 * PI;}
};
#endif

and then in main.cpp, on the line containing "Circle c1(5);" I get the error:

21 IntelliSense: object of abstract class type "Circle" is not allowed: pure virtual function "FigureGeometry::getArea" has no overrider pure virtual function "FigureGeometry::getPerimeter" has no overrider c:\Users\moog\Documents\Visual Studio 2012\Projects\data structures 3\data structures 3\main.cpp 9 9 data structures 3
1

3 Answers

Your functions should be:-

float getArea() const {return getRadius() * getRadius() * PI;}
float getPerimeter() const {return getRadius() * 2 * PI;}

REASON FOR THIS BEHAVIOR :-

When you re-define a function in derived class with same parameters as in base class then that's called as overriding. Whereas if you re-define that function with different parameter then it would be an attempt to use overloading from you side. But overloading is possible only in class scope. So, in this case corresponding base class function would be hidden.

For e.g:- Below is futile attempt on overloading.

class Base
{
public: virtual void display () const;
};
class Derived
{
public: virtual void display ();
};
int main()
{ const Derived d; d.display(); //Error::no version defined for const....
}

So you are getting error as display in derived would hide display in base.

Similarly your pure virtual function would be hidden i.e compiler treat that case as there is no function defined in derived corresponding to base class pure virtual function.That would make derived also a abstract class.

Hope things are crystal clear...

7

Your getArea() and getPerimeter() methods do not override the virtual methods declared in FigureGeometry because they do not match in const-qualification. Therefore Circle becomes abstract since those methods were pure virtual; and you can't create objects of abstract type.

To fix this, simply add const to both of your methods. Also, to make sure you have correctly overrided methods from the base class, use the override specifier:

float getArea() const override;
float getPerimeter() const override;

If they do not override the compiler will let you know with an error message.

You forgot to place qualifier const for these virtual functions in the derived class. Write

float getArea() const {return getRadius() * getRadius() * PI;}
float getPerimeter() const {return getRadius() * 2 * PI;}

So in fact in the derived class you declared new functions that hide virtual functions in the base class with the same name.

Also you should to declare the destructor also as virtual. For example

class FigureGeometry
{
public: // ... virtual ~FigureGeometry() = default;
};

Or

class FigureGeometry
{
public: // ... virtual ~FigureGeometry() {}
};

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy