往之不谏519 2021-06-25 10:53 采纳率: 0%
浏览 24

这个怎么写呀,挺急的

结合main函数的代码,编写一个Point类,需要满足如下要求:1)数据成员包含:横坐标,纵坐标2)定义构造函数和析构函数3)重载+(加法)、-(减法)运算符 (提示:相应坐标值的加减)4)重载=(赋值)运算符5)重载> (提取运算符)int main(){       Point p1,p2,p3(3,4);    cin>>p2;    p1=p2+p3;    cout<<p1;    p1=p2-p3;    cout<<p1;    return 0;}

  • 写回答

2条回答 默认 最新

  • 浪客 2021-06-25 11:47
    关注
    #include <iostream>
    using namespace std;
    
    class Point
    {
    private:
        int x,y;
    
    public:
        Point(int x,int y);
        Point();
        ~Point();
    
        Point operator+(const Point &a);
        Point operator-(const Point &a);
        Point& operator=(const Point &a);
    
        friend ostream &operator<<(ostream &os, const Point &a);
        friend istream &operator>>(istream &in, Point &a);
    };
    
    Point::Point(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
    
    Point::Point()
    {
        Point(0,0);
    }
    
    Point::~Point()
    {
    }
    
    Point Point::operator+(const Point &a)
    {
        Point tmp;
        tmp.x=this->x + a.x ;
        tmp.y=this->y + a.y ;
    
        return tmp;
    }
    
    Point Point::operator-(const Point &a)
    {
        Point tmp;
        tmp.x=this->x - a.x ;
        tmp.y=this->y - a.y ;
    
        return tmp;
    }
    
    Point& Point::operator=(const Point &a)
    {
        this->x=a.x;
        this->y=a.y;
        return *this;
    }
    
    ostream& operator<<(ostream &os, const Point &a)
    {
        os << "x=" << a.x << " y=" << a.y << endl ;
        return os;
    }
    
    istream& operator>>(istream &in, Point &a)
    {
        in >> a.x >> a.y;
        return in;
    }
    
    
    int main()
    {
        Point p1, p2, p3(3, 4);
        cin >> p2;
        p1 = p2 + p3;
        cout << p1;
        p1 = p2 - p3;
        cout << p1;
    
        return 0;
    }
    评论

报告相同问题?

悬赏问题

  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33
  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏