1.重载为类的成员函数
#include<iostream>
using namespace std;
class Shu
{
public:
int m;
int n;
Shu operator+(Shu &a)
{
Shu t;
t.m = this->m + a.m;
t.n = this->n + a.n;
return t;
}
};
int main()
{
Shu a1;
a1.m = 10;
a1.n = 10;
Shu a2;
a2.m = 10;
a2.n = 10;
Shu a3 = a1 + a2;
cout << "a3.m=" << a3.m << endl;
cout << "a3.n=" << a3.n << endl;
}
2.重载为全局函数
#include<iostream>
using namespace std;
class Shu
{
public:
int m;
int n;
};
Shu operator+(Shu &a1,Shu &a2)
{
Shu t;
t.m = a1.m + a2.m;
t.n = a1.n + a2.n;
return t;
}
int main()
{
Shu a1;
a1.m = 10;
a1.n = 10;
Shu a2;
a2.m = 10;
a2.n = 10;
Shu a3 = a1 + a2;
cout << "a3.m=" << a3.m << endl;
cout << "a3.n=" << a3.n << endl;
}