我是C++的初学者,我正在做关于抽象类和继承的练习之一。为什么继承成员不被允许?
这是我的抽象类:
#ifndef SHAPE_H
#define SHAPE_H
class Shape
{
public:
virtual void area();
virtual void perimeter();
virtual void volume();
};
#endif
这是我的具体类实现抽象类:
#include <iostream>
#include <cmath>
#include "Shape.h"
using namespace std;
class Circle : public Shape
{
public:
Circle(int);
private:
int r;
};
Circle::Circle(int rad)
{
r = rad;
}
void Circle::area()
{
cout << "Area of this cirle = " << 3.14 * pow(r, 2) << endl;
}
void Circle::perimeter()
{
cout << "Perimeter of this cirle = " << 2 * 3.14 * r << endl;
}
void Circle::volume()
{
cout << "Volume is not defined for circle." << endl;
}
我有红线area()
下,perimeter()
和volume()
在我Circle
类,其中显示"Error: inherited member is not allowed"
。我经历了我的课程ppt并用Google搜索,但没有运气。任何帮助表示赞赏。
你还是要声明在派生类中的虚方法。 – 2013-02-27 16:48:11
'形状'看起来不抽象。它的成员函数应该是纯虚拟的。另外,它应该声明一个虚拟析构函数。 – 2013-02-27 16:52:27
不要忘记声明'virtual〜Shape(){}' – 2013-02-27 16:52:28