c++207 运算重载
调入 op+
#include<iostream>
using namespace std;
class Complex
{
public:
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printfCom()
{
cout << a << "+" << b << "i" << endl;
}
};
Complex myAdd(Complex& c1, Complex& c2)
{
//Complex tmp(1, 2);
Complex tmp(c1.a+c2.a, c1.b+c2.b);
return tmp;
}
void main()
{
int a = 0, b = 0;
int c;
c = a + b;//编译器知道如何进行加减
//a+bi 复数运算规则
Complex c1(1, 2), c2(3, 4);
Complex c3;
//c3 = c1 + c2;//类也是数据类型 用户自定义数据类型 c++ 编译器不知道如何运算
//提供自定义运算符操作-》运算重载
//步骤:
Complex c4=myAdd(c1, c2);
c4.printfCom();
return;
}
//运算符重载 本质:函数
//根据类型找函数调用
//Complex opreat+(Complex& c1, Complex& c2)
//{
// //Complex tmp(1, 2);
// Complex tmp(c1.a + c2.a, c1.b + c2.b);
// return tmp;
//
//}