加号运算符重载
运算符重载的本质是
//成员函数重载本质调用
Person p3 = p1.operator+(p2);
Person p3 = p1 +p2;
//全局函数重载本质调用
Person p3 = operator+(p1,p2);
//需注意 运算符重载 也可以发生函数重载
Person p3 = p1 +10;//person 数据类型 + int
Person operator+(Person &p1,int num){
Person temp;
temp.m_A = p1.m_A +num;
temp.m_B = p1.m_B +num;
return temp;
}
左移运算符重载
//只能利用全局函数重载左移运算符 ostream & operator <<(ostream &cout,Person &p) //本质 operator << (cout,p) 简化 cout<<p { cout <<"m_A=" << p.m_A <<"m_B=" <<p.m_B;
return cout; }
void test01()
{
cout <<p <<endl;//链式编程思想
}
递增运算符
赋值运算符重载
关系运算符重载
函数调用运算符重载