2016-05-19 4 views
-2

これは私が行う必要があります:新しいデータメンバ、文字列の色をPoint2Dクラスに追加し、色の新しいgetterとsetter関数を追加します。 Point2Dオブジェクトを作成し、その色を設定します。その後、Point3Dカラーを作成し、そのカラーを設定しようとします。このsetColorビヘイビアはPoint3Dクラスで使用できますか?なぜ、なぜそうではないのですか?Point2Dクラスオブジェクト

これは私のコードです:

#include<iostream> 
#include<vector> 
using namespace std; 

class Point2D 
{ 
    friend class SPALops; 
    protected: 
     float x; 
     float y; 

    protected: 
     float getX(){ 
      return x; 
     }; 
     float getY(){ 
      return y; 
     }; 
     void setX(float xc){ 
      x = xc; 
     }; 
     void setY(float yc){ 
      y = yc; 
     }; 

     Point2D(int xcoord, int ycoord){ 
      x = xcoord; 
      y = ycoord; 
     }; 
}; 


class Point2D : Point2D 
{ 
    friend class SPALops; 
     public: 

     Point2D(int x, int y) : Point2D(x,y){} 

     Point2D() : Point2D(0,0){} 

    float getX(){ 
     return this->Point2D::getX(); 
    }; 

    float getY(){ 
     return this->Point2D::getY(); 
    }; 

    void setX(float x){ 
     this->Point2D::setX(x); 
    }; 

    void setY(float y){ 
     this->Point2D::setY(y); 
    }; 



}; 


class RectangleImplementation{ 
    friend class SPALops; 
    protected: 
     Point2D ll; 
     Point2D ur; 

     RectangleImplementation(float llx, float lly, float urx, float ury){ 
      ll.setX(llx); ll.setY(lly); 
      ur.setX(urx); ur.setY(ury); 
     } 

     void setLLx(float x){ 
      ll.setX(x); 
     }; 
     void setLLy(float y){ 
      ll.setY(y); 
     }; 
     void setURx(float x){ 
      ur.setX(x); 
     }; 
     void setURy(float y){ 
      ur.setY(y); 
     }; 
     float getLLx(){ 
      return ll.getX(); 
     }; 
     float getLLy(){ 
      return ll.getY(); 
     }; 
     float getURx(){ 
      return ur.getX(); 
     }; 
     float getURy(){ 
      return ur.getY(); 
     }; 

     vector<vector<float> > getPointList(){ 
      vector<vector<float> > v(4); 
      vector<float> llv(2); llv[0] = ll.getX(); llv[1] = ll.getY(); 
      v[0] = llv; 
      vector<float> luv(2); luv[0] = ll.getX(); luv[1] = ur.getY(); 
      v[1] = luv; 
      vector<float> ruv(2); ruv[0] = ur.getX(); ruv[1] = ur.getY(); 
      v[2] = ruv; 
      vector<float> rlv(2); rlv[0] = ur.getX(); rlv[1] = ll.getY(); 
      v[3] = rlv;  
      return v;  
     }; 

     void printPointList(){ 
      vector<vector<float>> v = this->getPointList(); 
      cout << "ll = " << v[0][0] << " , " << v[0][1] << endl; 
      cout << "lu = " << v[1][0] << " , " << v[1][1] << endl; 
      cout << "ru = " << v[2][0] << " , " << v[2][1] << endl; 
      cout << "rl = " << v[3][0] << " , " << v[3][1] << endl; 
     }; 

}; 

class Rectangle : RectangleImplementation{ 
    friend class SPALops; 
    public: 

    Rectangle(Point2D &p, Point2D &q) : RectangleImplementation(p.getX(), p.getY(), q.getX(), q.getY()){}; 
    Rectangle(float llx, float lly, float urx, float ury): RectangleImplementation(llx,lly,urx,ury){}; 

    float getLLx(){ 
     return this->RectangleImplementation::getLLx(); 
    }; 
    float getLLy(){ 
     return this->RectangleImplementation::getLLy(); 
    }; 
    float getURx(){ 
     return this->RectangleImplementation::getURx(); 
    }; 
    float getURy(){ 
     return this->RectangleImplementation::getURy(); 
    }; 

    void setLLx(float x){ 
     this->RectangleImplementation::setLLx(x); 
    }; 
    void setLLy(float y){ 
     this->RectangleImplementation::setLLy(y); 
    }; 
    void setURx(float x){ 
     this->RectangleImplementation::setURx(x); 
    }; 
    void setURy(float y){ 
     this->RectangleImplementation::setURx(y); 
    }; 

    void printPointList(){ 
     cout << "In rectangle: " << endl; 
     cout << "ll = " << ll.getX() << " , " << this->RectangleImplementation::getLLy() << endl; 
     cout << "ru = " << this->getURx() << " , " << this->RectangleImplementation::getURy() << endl; 
    }; 
}; 


class SPALops{ 
public: 
static bool touches(Rectangle &r, Point2D &p){ 
     vector<vector<float> > v = r.RectangleImplementation::getPointList(); 
     if((v[0][0] == p.getX() and v[0][1] == p.getY()) or 
      (v[1][0] == p.getX() and v[1][1] == p.getY()) or 
      (v[2][0] == p.getX() and v[2][1] == p.getY()) or 
      (v[3][0] == p.getX() and v[3][1] == p.getY())) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 

    }; 
}; 

int main(){ 
    Point2D p(10,10); 
    Point2D q(15,15); 
    Point2D s(20,20); 
    Point2D t(10,12); 

    Rectangle r(p,q); 

    r.printPointList(); 

    cout << "Do rectangle 'r' and point 'p' touch = " << SPALops::touches(r,p) << endl; 
    cout << "Do rectangle 'r' and point 's' touch = " << SPALops::touches(r,s) << endl; 
    cout << "Do rectangle 'r' and point 't' touch = " << SPALops::touches(r,t) << endl; 

    return 0; 

} 

私が正常に動作してから私を停止する多くのエラーを持っているようです。フィードバックがあれば感謝します。

+3

'class Point2D:Point2D'はこれですか? –

+3

コンパイラを使用します。エラーメッセージに関してフィードバックを提供します。真剣に、あなたはコンパイラエラーがあり、それらを修正するために助けが必要な場合は、エラーが何であるか教えてください – user463035818

+0

自己相続、クールな近親交配。問題文のように、 'Point2D'を継承する' Point3D'クラスが必要になるでしょう。 –

答えて

1

あなたの投稿には次のような問題があります。
1)重複するクラス。
まず、Point2Dクラスがあり、次に第2のPoint2Dクラスがあります。
2番目のクラスをPoint3Dにしたかったでしょうか?

2)公開継承。 class Point2D: Point2Dです。継承です。
あなたは公共継承を使用することもできます。
class Point3D : public Point2D

3)ではない必要this->表記。
メンバーまたはクラスの機能にアクセスする場合、直接アクセス:

int getX() const 
{ return x; } 

4)は、親の機能を複製しないでください。 子クラスで親関数を繰り返す必要はありません。例えば、子クラスはgetXメソッドを必要としません。これは継承のためのものです - だからメソッドを複製する必要はありません。

5)子コンストラクタが親コンストラクタを呼び出します。

Point3D(int new_x, int new_y, int new_z) 
: Point2D(new_x, new_y), 
    z(new_z) 
{ ; } 

つのポストで議論するにはあまりにも多くの他のエラー: あなたのコンストラクタは次のようになります。上記はあなたを始めて、他のクラスに適用するパターンを与えるはずです。

関連する問題