c++ (运算符重载 && 基本运算符重载)

c++ (运算符重载 && 基本运算符重载)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Person
{
public:
	Person(){}
	Person(int a,int b):m_A(a),m_B(b){}
	int m_A;
	int m_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+ (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  text01()
{
	Person p1(10, 10);
	Person p2(10, 10);
	Person p3 = p1 + p2;
	cout << "p3的 m_A " << p3.m_A << " p3的m_B" << p3.m_B << endl;
}

int main()
{
	text01();
	return 0;
}

posted on 2021-04-23 15:48  lodger47  阅读(96)  评论(0)    收藏  举报

导航