作业题:自定义point类,其对象表示平面上的一个点(x,y),并通过类成员方式对该类重载二目运算符“+”和“^”,用来求出两个对象的和以及两个对象(平面点)的距离。各运算符的使用含义(运算结果)如下所示:
(1.2, -3.5) + (-1.5, 6) = (-0.3, 2.5);
(1.2, -3.5) ^ (-1.5, 6) = 9.87623。
编写主函数,说明类对象,而后通过类对象实现所定义的相关运算

#include
#include
using namespace std;
class ponit
{
private:
double x;
double y;
public:
ponit() 
{
double x = y = 0;
}
ponit(double x_, double y_)
{
x = x_;
y = y_;
}
void display()
{
cout << "(" << x << "," << y << ")" << endl;
}
friend ponit operator +(ponit& p1, ponit& p2);
double operator ^( ponit& p2)
{
double distance = sqrt((this->x - p2.x) * (this->x - p2.x) -
(this->y - p2.y) * (this->y - p2.y));
return distance;
}
};
ponit operator +(ponit& p1, ponit &p2)
{
return ponit(p1.x + p2.x, p1.y + p2.y);
}

int main()
{
ponit p1(8, 8), p2(5, 4), p3;
p3 = p1 + p2;
p3.display();
double distance = p1.operator^(p2);
cout << distance << endl;
return 0;
}


  • Willson1991    2020-04-01 10:22:07
  • 阅读 805    收藏 0    回答 1
  • 邀请
  • 收藏
  • 分享
发送
登录 后发表评论
  • 推荐问答
  • 换一换
  • 51testing软件测试圈微信