#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Person
{
public:
//成员函数重载运算符
//Person operator + (Person &p)
//{
// Person temp;
// temp.a = this->a + p.a;
// temp.b = this->b + p.b;
// return temp;
//}
Person(int _a, int _b);
Person(){}
int a;
int b;
};
Person::Person(int _a, int _b)
{
a = _a;
b = _b;
}
//全局函数重载运算符
Person operator + (Person &p1, Person &p2)
{
Person temp;
temp.a = p1.a + p2.a;
temp.b = p1.b + p2.b;
return temp;
}
//全局函数重载运算符函数重载
Person operator + (Person& p1, int num)
{
Person temp;
temp.a = p1.a + num;
temp.b = p1.b + num;
return temp;
}
void test01()
{
Person p1(10, 10);
Person p2(10, 10);
Person p3 = p1 + p2;
//成员函数重载运算符本质
//Person p3 = p1.operator+(p2);
//全局函数重载运算符本质
//Person p3 = operator+(p1, p2);
cout << "p3.a = " << p3.a << endl;
cout << "p3.b = " << p3.b << endl;
//运算符重载也可以发生函数重载
Person p4 = p1 + 10;
cout << "p4.a = " << p4.a << endl;
cout << "p4.b = " << p4.b << endl;
}
int main()
{
test01();
return 0;
}