其他分享
首页 > 其他分享> > 友元函数的运算符重载operator+

友元函数的运算符重载operator+

作者:互联网

源程序:

#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);
}
void main()
{
Sample obj1(10);
Sample obj2(20);
Sample obj3;
obj3 = obj1 + obj2;
obj3.disp();
}

运行结果:

 

标签:友元,Sample,obj3,disp,s2,s1,运算符,operator
来源: https://www.cnblogs.com/duanqibo/p/14396258.html