运算符重载test p159运行结果与书本不一样(最后2行) 应该书本错了

//
// Created by 刘彪 on 2020/3/1.
//p159运行结果与书本不一样(最后2行) 应该书本错了

#include <iostream>
#include <string.h>
#include <cstdio>

using namespace std;
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);
    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 << "("<<x<<","<<y<<")"<<endl;}




};

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) {
    //ziji
    return (x!=p.gety() || y!=p.gety()) ? 1: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;
}

int 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)<<endl;
    cout<<"4:"<<(p2!=p3)<<endl;
    p3+=p1;
    cout<<"5:";
    p3.disp();
    p3-=p2;
    cout<<"6:";
    p3.disp();
    p3=p1+p2;
    cout<<"7:";
    p3.disp();
    p3=p1-p2;
    cout<<"8:";
    p3.disp();


    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
Xistic:啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
09-22 22:22
中山大学 Java
双尔:赌对了,不用经历秋招的炼狱真的太好了,羡慕了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务