我是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