2016-09-12 14 views
0

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(); 
    } 
+0

オブジェクトの抽象クラスはC++では使用できません。 –

+1

あなたの問題は、 'Circle'の保護されたメンバーを' Shape r'のように追加しようとするときです。 'Shape'は抽象クラスなので、これはできません。 –

+1

Shapeは抽象クラスで、Circleクラスの抽象クラスオブジェクトを作成しようとしています。 –

答えて

1

Circleにメンバー変数rを宣言すると、Shapeクラスのインスタンスが宣言されます。 Shapeは抽象クラスなので不可能です。

変数を参照またはポインタのいずれかにする必要があります。


もう少しあなたのコードを読んだ後、あなたはおそらく、すべてここにメンバ変数を使用しますが、あなたはShape基本クラスからを継承xyメンバーを使用しないでください。あなたはすでにRectangleクラスでやっているように。

+0

あなたのお手伝いをありがとう:v私はそれをしました:3再びありがとう –

1

問題はここにある:

class Circle: public Shape{ 
    protected: 
      Shape r; // Wrong 

Shapeが純粋仮想関数であるので、あなたは(Shape r試行が何をするかを正確です)、それのインスタンスを作成することはできません。

考えられる解決策:ALSO

class Circle: public Shape{ 
protected: 
    int x, y 
    ... 
    public Circle() { 
     x = y = 0; // Do you really need "r" at all? Why not just x and y? 
     ... 

あなたは本当にしないでください...ターボC++を使用している場合。時代遅れの20年...それは重要です。特にあなたが学んでいるならば。たくさんのフリーおよび/またはオープンソースのC++コンパイラがあり、おそらくあなたにはもっと良いサービスを提供するでしょう...

+0

ああ、私はちょうど "r"を見たいので、それはプロのようです。 :)私は、多分:3私の複雑さを残念申し訳ありません。ありがとうございました !私はそれをクリアします! 私の英語は良くありません! –