Why is inherited member not allowed?
You have to declare the over-ridden functions as part of your class definition
class Circle : public Shape
{
public:
Circle(int);
virtual void area(); // overrides Shape::area
void perimeter(); // overrides Shape::perimeter
virtual void volume();
private:
int r;
};
Note that the use of virtual
here is optional.
As n.m. noted, you should also include a virtual destructor in Shape
. You may also want to make its virtual functions pure virtual (based on your comment about Shape
being abstract)
class Shape
{
public:
virtual ~Shape() {}
virtual void area() = 0;
virtual void perimeter() = 0;
virtual void volume() = 0;
};
you have to declare the override methods too in the Circle class
class Circle : public Shape
{
public:
Circle(int);
virtual void area();
virtual void perimeter();
virtual void volume();
private:
int r;
};
First you should make you Shape class explicitly abstract:
class Shape
{
public:
virtual void area() = 0;
virtual void perimeter() = 0;
virtual void volume() = 0;
};
This way you do not have to define that methods in class Shape, and what is more important if you forget to override any of abstract method in derived class and would try to create an instance of it, compiler will remind you. Second when you override virtual method in derived class you need to declare them:
class Circle : public Shape
{
public:
Circle(int);
virtual void area();
virtual void perimeter();
virtual void volume();
private:
int r;
};