/*
02.2.*Copyright(c) 2016.烟台大学计算机与控制工程学院
03.3.*ALL rights reserved.
04. 4.*文件名称:main.cpp
05. 5.*作者:孙亚茹
06.6.*完成日期:2016年5月24日
07.7.*问题描述:阅读程序,写出运行结果。
08.8.*/
#include <iostream>
using namespace std;
class Sample
{
private:
int x;
public:
Sample () { } //1
Sample(int a){x=a;} // 2
void disp(){cout<<"x="<<x<<endl;} //3
friend Sample operator+( Sample &s1, Sample &s2);
};
Sample operator+( Sample &s1, Sample &s2) //4
{
return Sample(s1.x+s2.x);
}
int main()
{
Sample obj1(10);
Sample obj2(20);
Sample obj3;
obj3=obj1+obj2;
obj3.disp();
return 0;
}
总结:
运算符重载:将运行10+20=30,输出X=30。