题目描述:
定义一个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;
输入描述:
2 4
输出描述:
12 14
-8 -6
12 14
-8 -6
代码:
#include<iostream>
using namespace std;
class Test
{
private:
int x, y;
public:
Test(int x, int y)
{
this->x = x;
this->y = y;
}
Test *setXY1(int x1, int y1)
{
this->x = x + x1;
this->y = y + y1;
return this;
}
Test &setXY2(int x1, int y2)
{
this->x = x - x1;
this->y = y -y2;
return *this;
}
void showXY()
{
cout << x <<" " << y << endl;
}
};