#include<iostream>
using namespace std;
#include <string>
#include <ostream>
class Person {
friend ostream& operator<<(ostream& out,Person &p); //左移运算符需要放友元
public:
int m_A;
int m_B;
Person() {};
Person(int a ,int b) {
this->m_A = a;
this->m_B = b;
}
Person operator+(const Person &p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
};
Person operator+(const Person& p2,int val) {
Person temp;
temp.m_A = p2.m_A + val;
temp.m_B = p2.m_B + val;
return temp;
}
ostream& operator<<(ostream &out,Person &p) {
out << "m_A:" << p.m_A << "m_B" << p.m_B << endl;
return out;
}
void test() {
Person p1(10,20);
Person p2(10,10);
Person p3 = p1 + p2;
Person p4 = p3 + 20;
cout << "p3:" << p3 << endl;
cout << "p4:" << p4 << endl;
}
int main() {
test();
system("pause");
return 0;
}
c++ 左移和+结合
最新推荐文章于 2024-09-07 05:15:02 发布