运算符重载的同时也可以发生函数重载!
#include
usingnamespace std;
//加号运算符重载
class xiMeng
{
public:
int M_A;
int M_B;
//通过成员函数运算符重载
/*xiMeng operator + (xiMeng& p)
{
xiMeng temp;
temp.M_A = this->M_A + p.M_A;
temp.M_B = this->M_B + p.M_B;
return temp;
}*/
};
//通过全局函数运算符重载
xiMeng operator+(xiMeng& p1, xiMeng& p2)
{
xiMeng temp;
temp.M_A = p1.M_A + p2.M_A;
temp.M_B = p1.M_B + p2.M_B;
return temp;
}
//运算符重载也可以发生函数重载
xiMeng operator+(xiMeng& p,int num)
{
xiMeng temp;
temp.M_A = p.M_A + num;
temp.M_B = p.M_B + num;
return temp;
}
void xiMengTest(){
xiMeng p1;
p1.M_A =15;
p1.M_B =25;
xiMeng p2;
p2.M_A =10;
p2.M_B =30;
//通过全局函数运算符重载
xiMeng p3 = p1 + p2;
cout <<"p3.M_A = "<< p3.M_A << endl;
cout <<"p3.M_B = "<< p3.M_B << endl;
//运算符重载也可以发生函数重载
xiMeng p4 = p1 +100;
cout <<"p4.M_A = "<< p4.M_A << endl;
cout <<"p4.M_B = "<< p4.M_B << endl;
}
int main()
{
xiMengTest();
return0;
}