Oxyに矩形と円を描画するためのシェイプクラスを作成したいと思います。 私は仮想関数を使用する必要があり、それはエラーに行く:仮想関数、エラーについて
43 10は、D:\ CPP \ TurboC4 \ OOPCppの\ shape.cpp [エラー]は抽象的であるようにフィールド 'サークル:: r' を宣言することはできません 18 18 D:¥Cpp¥TurboC4¥OOPCpp¥shape.cpp仮想フロートシェイプ: 'Shape'内で純粋なものであるため、「Shape」と入力してください。 6 7 D:¥Cpp¥TurboC4¥OOPCpp¥shape.cpp :エリア()
は、ここに私のコードです:
class Shape{
public:
int x,y;
public:
void set_data (int a,int b){
x=a;
y=b;
}
void input(){
cout<<"x= "; cin>>x;
cout<<"y= "; cin>>y;
}
virtual float area()=0;
};
class Rectangle: public Shape{
public:
Rectangle(){
x=0;
y=0;
}
Rectangle(int x,int y){
this->x=x;
this->y=y;
}
void input(){
cout<<"Enter value of width and height: ";
cin>>x;
cin>>y;
}
float area(){
return x*y;
}
};
class Circle: public Shape{
protected:
Shape r;
public:
Circle(){
r.x=0;
r.y=0;
}
//center of Circle: default(0,0) , r is 1 point in the circle.
Circle(int x,int y){
r.x=x;
r.y=y;
}
void nhap(){
cout<<"Enter values x,y of r: ";
cin>>x;
cin>>y;
}
virtual float area(){
float a=sqrt(r.x*r.x+r.y*r.y);
return 3.14*a*a;
}
};
class ArrayOfShape{
private:
int n;
Shape **a;
public:
void nhap(){
int hinh;
cout<<"input number of shape: ";
cin>>n;
a=new Shape*[n];
for (int i=0;i<n;i++){
cout<<"\nEnter shape (1:rectangle,2:circle): ";
cin>>hinh;
if (hinh==1){
Rectangle *p=new Rectangle();
p->nhap();
a[i]=p;
}
else{
if(hinh==2){
Circle *e=new Circle();
e->input();
a[i]=e;
}
else{
cout<<"Invilid input";
}
}
}
}
void area(){
for (int i=0;i<n;i++)
cout<<"\nArea of shape"<<i+1<<" : "<<a[i]->area();
}
};
int main(){
ArrayOfShape a;
a.input();
a.area();
}
オブジェクトの抽象クラスはC++では使用できません。 –
あなたの問題は、 'Circle'の保護されたメンバーを' Shape r'のように追加しようとするときです。 'Shape'は抽象クラスなので、これはできません。 –
Shapeは抽象クラスで、Circleクラスの抽象クラスオブジェクトを作成しようとしています。 –