64,649
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Test
{
public:
Test(int _a){a = _a;}
Test(){}
Test& operator+(const Test& t1){
*this += t1;
return *this;
}
Test& operator+=(const Test& t1){
this->a += t1.a;
return *this;
}
friend ostream& operator << (ostream& os,const Test& t){
os<<t.a<<endl;
return os;
}
private:
int a;
};
int main(void)
{
Test t1(6);
Test t2(7);
cout << t1 + t2 << t1 + t2;
return 0;
}
Test& operator+(const Test& t1){
return Test(this->a + t1.a);
}