复习 --- C++运算符重载

.5 运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另外一种功能,以适应不同的数据类型

4.5.1 加号运算符重载

作用:实现两个自定义数据类型相加的运算

 1  #include
      2  using namespace std;
      3  //加号运算符重载
      4  class Person
      5  {
      6      public:
      7          //成员函数重载+号
      8          Person operator+(Person &p)
      9          {
     10              Person temp;
     11              temp.m_A = p.m_A + this->m_A;
     12              temp.m_B = p.m_B + this->m_B;
     13              return temp;
     14          }
     15          int m_A;
     16          int m_B;
     17  };
     18  
     19  //全局函数重载
     20  Person operator+(Person &p1,Person &p2)
     21  {
     22      Person temp;
     23      temp.m_A = p1.m_A + p2.m_A;
     24      temp.m_B = p1.m_B + p2.m_B;
     25      return temp;
     26  }
     27  void test01()
     28  {
     29      Person p1;
     30      p1.m_A = 10;
     31      p1.m_B = 10;
     32      Person p2;
     33      p2.m_A = 10;
     34      p2.m_B = 10;
     35  
     36      Person p3 = p1 + p2;
     37      //成员函数重载的本质
     38      Person p3 = p1.operator+(p2);
     39      //全局函数重载的本质
     40      Person p5 = operator+(p1,p2);
     41      cout << p3.m_A << " " << p3.m_B<
     
4.5.2 左移运算符重载
 ​
 ​
     
 1  #include
      2  using namespace std;
      3  
      4  //左移
      5  class Person
      6  {
      7      public:
      8          //内置重载函数 左移运算符
      9          //实现不了
     10          int m_A;
     11          int m_B;
     12  };
     13  
     14  //全局重载函数
     15  void operator<<(ostream &cout ,Person &p)
     16  {
     17      cout << "m_A = " << p.m_A << "m_B = " <
 ​
 ​

4.5.3递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

 #include
 using namespace std;
 ​
 class Myinterger
 {
     friend ostream & operator<<(ostream &cout,Myinterger myint);
     public:
     //重载前置++函数
     //返回引用是为了一直对一个数据进行递增操作
         Myinterger& operator++()
         {
             m_Num++;
             return *this;
         }
 ​
         //重载后置++函数
         //int 是占位参数
         Myinterger operator++(int)
         {
             //先记录当时结果
             Myinterger temp = *this;
             m_Num++;
             return temp;
         }
         Myinterger()
         {
             m_Num = 0;
         }
     private:
         int m_Num;
 };
 //重载<<运算符
 ostream& operator<<(ostream &cout,Myinterger myint)
 {
     cout << myint.m_Num;
     return cout;
 }
 ​
 void test01()
 {
     Myinterger myint;
     ++myint;
     cout << myint << endl;
 }
 ​
 int main(int argc, const char *argv[])
 {
     test01();
     return 0;
 }
 ​
4.5.4 赋值运算符重载

1.编译器至少给一个类添加4个函数

  • 默认构造函数(无参,函数为空)

  • 默认析构函数(无参,函数为空)

  • 默认拷贝构造函数,对属性进行值拷贝

  • 赋值运算符 operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时会出现深浅拷贝问题

 #include
 using namespace std;
 ​
 class Person
 {
     public:
         //等号重载函数
         Person& operator=(Person &p)
         {
             //实现浅拷贝
             //先判断是否有属性在堆区,先释放在深拷贝
             if (m_age != NULL)
             {
                 delete m_age;
                 m_age = NULL;
             }
             m_age = new int(*p.m_age);
             return *this;
         }
         Person(int age)
         {
             m_age = new int(age);
         }
 ​
         int *m_age;
 ​
         ~Person()
         {
             if (m_age != NULL)
             {
                 delete m_age;
             }
         }
 };
 ​
 ​
 void test01()
 {
     Person p1(18);
     Person p2(12);
     p2 = p1;
     cout << "p1的年龄为:"<<*p1.m_age<

4.5.5 关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

 #include
 #include
 using namespace std;
 class Person
 {
     public:
         //重载==
         bool operator==(Person &p)
         {
             if (m_name == p.m_name && m_age == p.m_age)
             {
                 return true;
             }
             return false;
         }
         Person(string name , int age)
         {
             m_name = name;
             m_age = age;
         }
     private:
         string m_name;
         int m_age;
 };
 ​
 void test01()
 {
     Person p1("zyl",18);
     Person p2("zyl",18);
     bool b = (p1==p2);
     cout << b  << endl;
 ​
 }
 ​
 int main(int argc, const char *argv[])
 {
     test01();
     return 0;
 }

4.5.6 函数调用运算符重载
  • 函数调用运算符()也可以重载

  • 由于重载后使用的方式非常想函数的调用,因此也成为仿函数

  • 仿函数你没有固定写法,非常灵活

 #include
 #include
 using namespace std;
 ​
 class MyPrint
 {
     public:
         void operator()(string text)
         {
             cout << text <

你可能感兴趣的:(c++,开发语言)