私はC++の初心者です。抽象クラスと継承に関する演習の1つをやっています。継承されたメンバーがなぜ許可されないのですか?
これは私の抽象クラスです:
#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()
、そして私のCircle
クラスでvolume()
これは"Error: inherited member is not allowed"
を示した。私は私のクラスpptを通って、答えのためにgoogledしかし運がない。どんな助けもありがとうございます。
あなたはまだ、派生クラスで仮想メソッドを宣言する必要があります。 –
'Shape'は抽象的に見えません。メンバ関数は純粋な仮想関数でなければなりません。さらに、仮想デストラクタを宣言する必要があります。 –
'virtual〜Shape(){}'を宣言するのを忘れないでください。 –