大哥大姐帮忙修改下C++程序

来源:百度知道 编辑:UC知道 时间:2024/06/23 12:31:20
#include <iostream>
using namespace std;
class Location
{
public:
Location (int xx,int yy)
{x=xx;
y=yy;

}
Location &operator +(Location &offset);
Location &operator -(Location &offset);
int getx()
{return x;}
int gety()
{return y;}
private:
int x;
int y;

};
Location &Location ::operator +(Location &offset)
{
x=x+offset.x;
y=y+offset.y;
return *this;}
Location &Location ::operator -(Location &offset)
{
x=x-offset.x;
y=y-offset.y;
return *this;
}

void main()
{
Location p1(10,20),off(5,5);
cout<<"("<<p1.getx()<<","<<p1.gety()<<")"<<endl;
p1=p1+off;
cout<<"("<<p1.getx()<<","<<p1.gety()<<")"<<endl;
p1=p1-off;
cout<<"

#include <iostream>
using namespace std;
class Location
{
public:
Location (int xx,int yy) {x=xx; y=yy; } //构造函数
Location operator +(Location &offset)//+运算符重载
{
x=x+offset.x;
y=y+offset.y;
return *this;
}
Location operator -(Location &offset)//-运算符重载
{
x=x-offset.x;
y=y-offset.y;
return *this;
}
int getx() {return x;}
int gety() {return y;}
private:
int x;
int y;
};
void main()
{
Location p1(10,20),off(5,5); //定义两个变量
cout<<"("<<p1.getx()<<","<<p1.gety()<<")"<<endl; //输出变量1的值
p1=p1+off;
cout<<"("<<p1.getx()<<","<<p1.gety()<<")"<<endl; //输出两数相加
p1=p1-off;
cout<<"("<<p1.getx()<<","<<p1.gety()<<")"<