您的位置:首页 > 其它

第12周-阅读程序(1)

2016-05-16 19:38 381 查看
问题及代码:

#include <iostream>
using namespace std;
class Sample
{
private:
int x;
public:
Sample () { }
Sample(int a){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;
}


运行结果:



知识点总结:

友元类可以存取另一个类的私有成员

operator+重载运算符函数进行想要进行的运算


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: