问题及代码:
/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:吴敬超
* 完成日期:2016年5月17日
* 版本号:codeblock
*
* 问题描述:
* 输入描述:
* 程序输出: 输出结果
*/
#include<iostream>
using namespace std;
class Sample
{
private:
int x;
public:
Sample(int a=0)
{
x=a;
}
void disp()
{
cout<<"x="<<x<<endl;
}
friend Sample operator+(Sample &s1,Sample &s2);
};
Sample operator+(Sample &s1,Sample &s2)
{
return Sample(s1.x+s2.x);
}
int main()
{
Sample obj1(10);
Sample obj2(20);
Sample obj3;
obj3=obj1+obj2;
obj3.disp();
return 0;
}
运行结果: