### 加号运算符重载

#### 成员函数重载

#include <iostream>using namespace std;class Person{public:  //成员函数重载 +  Person operator+(Person& p)  {    Person temp;    temp.A = this->A + p.A;    temp.B = this->B + p.B;    return temp;  }  int A;  int B;};void test1(){  Person p1;  p1.A = 10;  p1.B = 20;  Person p2;  p2.A = 30;  p2.B = 40;  Person p3;  //成员函数重载本质调用  //Person p3 = p1.operator+(p2);  p3 = p1 + p2;  cout << "p3.A = " << p3.A << endl;  cout << "p3.B = " << p3.B << endl;}int main(){  test1();}

#### 全局函数重载

#include <iostream>using namespace std;class Person{public:  int A;  int B;};//全局函数重载 +Person operator + (Person& p1, Person& p2){  Person temp;  temp.A = p1.A + p2.A;  temp.B = p1.B + p2.B;  return temp;}void test1(){  Person p1;  p1.A = 10;  p1.B = 20;  Person p2;  p2.A = 30;  p2.B = 40;  Person p3;  //全局函数重载本质调用  //Person p3 = operator+ (p1, p2);  p3 = p1 + p2;  cout << "p3.A = " << p3.A << endl;  cout << "p3.B = " << p3.B << endl;}int main(){  test1();}

### 左移运算符重载

#include <iostream>using namespace std;class Person{  friend ostream& operator<<(ostream& cout, Person& p);public:  Person(int a, int b)  {    A = a;    B = b;  }private:  int A;  int B;};//只能利用全局函数重载 <<ostream& operator<<(ostream& cout, Person &p){  cout << "p1.A: " << p.A << endl;  cout << "p1.B: " << p.B << endl;  return cout;}void test1(){  Person p1(10, 20);  cout << p1 << endl;}int main(){  test1();}

### 递增运算符重载

#include <iostream>using namespace std;//自定义整型class MyInteger{  friend ostream& operator<<(ostream& cout, MyInteger& myint);public:  MyInteger()  {    Num = 0;  }  //重载前置++运算符  //返回引用是为了一直对一个数据进行递增操作  MyInteger& operator++()  {    Num++;    //将自身做返回    return *this;  }  //重载后置++运算符  //int代表占位参数,可以用于区分前置++和后置++  //返回值不返回引用，避免非法操作  MyInteger operator++(int)  {    //先记录结果    MyInteger temp = *this;    //后递增    Num++;    //最后返回记录的结果    return temp;  }private:  int Num;};//重载左移运算符ostream& operator<<(ostream& cout, MyInteger& myint){  cout << myint.Num;  return cout;}void test1(){  MyInteger myint;  cout << ++(++myint) << endl;  //2  cout << myint << endl;        //2}void test2(){  MyInteger myint;  //需要分开重载左移运算符  cout << myint++ << endl;     //0  cout << myint << endl;       //1}int main(){  //test1();  //test2();}

### 赋值运算符重载

C++编译器至少给一个类添加4个函数

#include <iostream>using namespace std;class Person{public:  Person(int age)  {    Age = new int(age);  }  ~Person()  {    if (Age != NULL)    {      delete Age;      Age = NULL;    }  }  //重载 赋值运算符  Person& operator=(Person& p)  {    //编译器提供浅拷贝    //Age = p.Age;    //应该先判断是否有属性在堆区，如果有，先释放干净，然后再深拷贝    if (Age != NULL)    {      delete Age;      Age = NULL;    }    //让自身指针指向新的内存空间 (深拷贝)    Age = new int(*p.Age);    //返回对象本身    return *this;  }  int *Age;};void test1(){  Person p1(18);  Person p2(20);  Person p3(22);  p3 = p2 = p1;  cout << "p1年龄为： " << *p1.Age << endl;  cout << "p2年龄为： " << *p2.Age << endl;  cout << "p3年龄为： " << *p3.Age << endl;}int main(){  test1();}

### 关系运算符重载

#include <iostream>#include <string>using namespace std;class Person{public:  Person(string name, int age)  {    Name = name;    Age = age;  }    //重载 ==  bool operator==(Person &p)  {    if (this->Name == p.Name && this->Age == p.Age)    {      return true;    }    return false;  }  string Name;  int Age;};void test1(){  Person p1("zhangsan", 20);  Person p2("lisi", 20);  if (p1 == p2)  {    cout << "p1和p2相等" << endl;  }  else  {    cout << "p1和p2不相等" << endl;  }}int main(){  test1();}

### 函数调用运算符重载

#include <iostream>#include <string>using namespace std;class MyPrint{public:  //重载函数调用运算符  void operator()(string test)  {    cout << test << endl;  }};void test1(){  MyPrint myprint;  myprint("hello");}int main(){  test1();}

#include <iostream>using namespace std;class MyAdd{public:  int operator()(int num1, int num2)  {    return num1 + num2;  }};void test2(){  MyAdd myadd;  int ret = myadd(100, 200);  cout << "ret = " << ret << endl;  //匿名函数对象  cout << MyAdd()(100, 200) << endl;}int main(){  test2();}