#include<iostream>
using namespace std;
//实现 对象3 = 对象1 + 对象2
//1普通实现方式
//1.1成员函数方式
//1.2全局函数方式
//2加号重载方式
//2.1成员函数加号重载方式
//2.2全局函数加号重载方式
class Person
{
public:
int m_A;
int m_B;
//Person personAddPerson(Person &k) //1.1成员函数方式
//{
// Person temp;
// temp.m_A = this->m_A + k.m_A;
// temp.m_B = this->m_B + k.m_B;
// return temp;
//}
//Person operator+(Person &k) //2.1成员函数加号重载方式
//{
// Person temp;
// temp.m_A = this->m_A + k.m_A;
// temp.m_B = this->m_B + k.m_B;
// return temp;
//}
};
//Person perAper(Person &x, Person &y) //1.2全局函数方式
//{
// Person temp;
// temp.m_A = x.m_A + y.m_A;
// temp.m_B = x.m_B + y.m_B;
// return temp;
//}
Person operator+(Person &x,Person &y) //2.2全局函数加号重载方式
{
Person temp;
temp.m_A = x.m_A + y.m_A;
temp.m_B = x.m_B + y.m_B;
return temp;
}
void test01()
{
Person p;
p.m_A = 10;
p.m_B = 10;
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
//p2 = p.personAddPerson(p1); //1.1成员函数方式
//p2 = perAper(p, p1); //1.2全局函数方式
//p2 = p + p1; //2.1.成员函数加号重载方式 相当于p2=p.operator+(p1);
p2 = p + p1; //2.2全局函数加号重载方式 相当于p2=operator+(p, p1);
cout << "p2.m_A = " << p2.m_A << endl;
cout << "p2.m_B = " << p2.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}