oj 贵州大学 第四次作业(c++)(运算符重载)第十题

定义一个Test类,该类有私有成员x和y
为该类设计两个成员函数setXY1和setXY2,其中setXY1返回目的对象的指针,而setXY2返回目的对象的引用
main函数已经写好,请根据main函数的内容完成该类的设计:
int main(){
int x,y;
cin>>x>>y;
Test t(x,y);
t.setXY1(10,10)->showXY();
t.setXY2(20,20).showXY();
return 0;
}

输入描述输入x和y

输出描述按样例输出

提示你需要提交除了main函数之外的其他代码

样例输入  

2 4

样例输出

12 14
-8 -6
 

#include<stdio.h>
#include <iostream>
#pragma warning(disable:4996)
#include <string>
using namespace std;
class Test
{
private:
    int x, y;
public:
    Test() { x = 0; y = 0; }
    Test(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
    Test* setXY1(int x, int y)
    {
        this->x += x;
        this->y += y;
        return this;
    }
    Test& setXY2(int x, int y)
    {
        this->x -= x;
        this->y -= y;
        return *this;
    }
    void showXY()
    {
        cout << this->x << " " << this->y << endl;
    }
};

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值