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
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With