编辑代码

#include <iostream>
using namespace std;
class Person{
public:
    Person(){};
    Person(int a,int b):m_A(a),m_B(b){}
    //成员函数进行+运算符的重载
    Person operator+(Person& p){
        Person tmp;
        tmp.m_A=this->m_A+p.m_A;
        tmp.m_B=this->m_B+p.m_B;
        return tmp;
    }
    Person operator+(int a){
        Person tmp;
        tmp.m_A=this->m_A+a;
        tmp.m_B=this->m_B+a;
        return tmp;
    }

    int m_A;
    int m_B;
};
/*全员函数实现+运算符的重载
Person operator+(Person& p1,Person& p2){
    Person tmp;
    tmp.m_A=p1.m_A+p2.m_A;
    tmp.m_B=p1.m_B+p2.m_B;
    return tmp;
}*/
void test01(){
    Person p1(10,10);
    Person p2(10,10);
    Person p3=p1+p2;
    Person p4=p1+10;
    cout<<"p3的m_A:"<<p3.m_A<<" p3的m_B:"<<p3.m_B<<endl;
    cout<<"p4的m_A:"<<p4.m_A<<" p3的m_B:"<<p4.m_B<<endl;
}

int main() {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
	//cout << "Hello JSRUN!   \n\n         - from C++ ." << endl;
    test01();
	return 0;
}