中易网

设计一个点类Point C++

答案:2  悬赏:0  
解决时间 2021-01-18 17:27
  • 提问者网友:焚苦与心
  • 2021-01-17 20:24
设计一个点类Point C++
最佳答案
  • 二级知识专家网友:往事埋风中
  • 2021-01-17 21:30
#include "iostream.h"
class Point
{
int x,y;
public:
Point() {x=y=0;}
Point(int i,int j) {x=i;y=j;}
Point(Point&);
~Point() {}
void offset(int,int); //提供对点的偏移
void offset(Point&); //重载,偏移量用Point类对象表示
bool operator==(Point&);//运算符重载,判断两个对象是否相同
bool operator!=(Point&);//运算符重载,判断两个运算符是否不相同
void operator+=(Point&);//运算符重载将两个对象相加
void operator-=(Point&);//运算符重载,将两个对象相减
Point operator+(Point&);//运算符重载,相加并将结果放在左操作数中
Point operator-(Point&);//运算符重载,相减并将结果放在左操作数中
int getx() {return x;}
int gety() {return y;}
void disp() {cout<<"("< }
};
Point::Point(Point& p) {x=p.x;y=p.y;}

void Point::offset(int i,int j) {x+=i;y+=j;}
void Point::offset(Point& p) { x+=p.getx();y+=p.gety(); }
bool Point::operator==(Point& p)
{if(x==p.getx()&&y==p.gety())
return 1;
else
return 0;}
bool Point::operator!=(Point& p)
{ if(x!=p.getx()||y!=p.gety())
return 1;
else return 0;
}
void Point::operator+=(Point& p) {x+=p.getx();y+=p.gety();}
void Point::operator-=(Point& p) {x-=p.getx();y-=p.gety();}
Point Point::operator+(Point& p) {this->x+=p.x; this->y+=p.y; return *this;}
Point Point::operator-(Point& p) {this->x-=p.x; this->y-=p.y; return *this;}
void main()
{Point p1(2,3),p2(3,4),p3(p2);
cout<<"1:";
p3.disp();
p3.offset(10,10);
cout<<"2:";
p3.disp();
cout<<"3:"<<(p2==p3)<cout<<"4:"<<(p2!=p3)<p3+=p1;
cout<<"5:";
p3.disp();
p3-=p2;
cout<<"6:";
p3.disp();
p3=p1+p3; //先将p3+p1的结果放在p1中然后赋给p3
cout<<"7:";
p3.disp();
p3=p1-p2;
cout<<"8:";
p3.disp();
}

供你参考,这个程序我以前学过。
全部回答
  • 1楼网友:孤独入客枕
  • 2021-01-17 21:39
我暂时保留我的看法!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息