我该如何编写这个类的这个函数?

时间:2017-06-03 20:07:26

标签: class

我是C ++的初学者,所以我不确定如何更详细地提出问题。对不起。 所以我的教授给了我这个头文件并告诉我写函数定义:

    class Move
    { private:
    double x;
    double y;
    public:
    Move( double a = 0, double b = 0 ); // sets x_, y_ to a, b

    void showmove(Move number) const; // shows current x_, y_ values

    // add : Move --> Move
    // to add x_ of input object to x_ of invoking object to get new x_,
    // to add y_ of input object to y_ of invoking object to get new y_,
    // to create new object initialized to new values and return it
    Move add( const Move &m ) const;

    void reset( double a = 0, double b = 0 ); // resets x, y to a, b
    };

我不明白函数移动添加(const Move& m)const; 。通常,add函数看起来像 int add(int a,int b)。因此将2个输入组合在一起以产生1个输出。但函数Move add只有一个输入。我不知道如何为它编写定义。我问过我的朋友,我们想出了像A.add(B)这样的东西,但我不确定它是否有意义。 感谢您阅读并抱歉我的英文

1 个答案:

答案 0 :(得分:0)

定义应该是

Move add(const Move& m) {
  return Move(this->x+m.x, this->y+m.y);
} 

它希望你使用m的属性直接用一个参数(对Move对象的引用)而不是两个(坐标)来获得结果。

确实,要调用此方法:

Move A/*sthg*/;
Move B/*sthg*/;
Move C = A.add(B);
相关问题