Point2D类对象

时间:2016-05-19 19:03:42

标签: c++ inheritance

这就是我需要做的事情:为Point2D类添加一个新的数据成员,字符串颜色,为颜色添加一个新的getter和setter函数。创建Point2D对象并设置其颜色。然后创建Point3D颜色并尝试设置其颜色。这个setColor行为是否适用于Point3D类?为什么或为什么不呢?

这是我的代码:

#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;

}

似乎我有很多错误阻止我成功运行。如果我得到任何反馈,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

以下是我在帖子中发现的一些问题:
1)重复课程 您首先是Point2D课程,然后是第二个Point2D课程 也许你希望第二课成为Point3D

2)公共继承。 您的class Point2D: Point2D私有继承 您可能希望使用 public 继承:
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)
{ ; }

在一篇文章中讨论太多其他错误。上面的内容应该让你开始并给你模式以应用于其他类。

相关问题