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


    .5 运算符重载

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

    4.5.1 加号运算符重载

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

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

    4.5.3递增运算符重载

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

    1.  #include
    2.  using namespace std;
    3.  ​
    4.  class Myinterger
    5.  {
    6.   friend ostream & operator<<(ostream &cout,Myinterger myint);
    7.   public:
    8.   //重载前置++函数
    9.   //返回引用是为了一直对一个数据进行递增操作
    10.   Myinterger& operator++()
    11.   {
    12.   m_Num++;
    13.   return *this;
    14.   }
    15.  ​
    16.   //重载后置++函数
    17.   //int 是占位参数
    18.   Myinterger operator++(int)
    19.   {
    20.   //先记录当时结果
    21.   Myinterger temp = *this;
    22.   m_Num++;
    23.   return temp;
    24.   }
    25.   Myinterger()
    26.   {
    27.   m_Num = 0;
    28.   }
    29.   private:
    30.   int m_Num;
    31.  };
    32.  //重载<<运算符
    33.  ostream& operator<<(ostream &cout,Myinterger myint)
    34.  {
    35.   cout << myint.m_Num;
    36.   return cout;
    37.  }
    38.  ​
    39.  void test01()
    40.  {
    41.   Myinterger myint;
    42.   ++myint;
    43.   cout << myint << endl;
    44.  }
    45.  ​
    46.  int main(int argc, const char *argv[])
    47.  {
    48.   test01();
    49.   return 0;
    50.  }
     ​
    4.5.4 赋值运算符重载

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

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

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

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

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

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

    1.  #include
    2.  using namespace std;
    3.  ​
    4.  class Person
    5.  {
    6.   public:
    7.   //等号重载函数
    8.   Person& operator=(Person &p)
    9.   {
    10.   //实现浅拷贝
    11.   //先判断是否有属性在堆区,先释放在深拷贝
    12.   if (m_age != NULL)
    13.   {
    14.   delete m_age;
    15.   m_age = NULL;
    16.   }
    17.   m_age = new int(*p.m_age);
    18.   return *this;
    19.   }
    20.   Person(int age)
    21.   {
    22.   m_age = new int(age);
    23.   }
    24.  ​
    25.   int *m_age;
    26.  ​
    27.   ~Person()
    28.   {
    29.   if (m_age != NULL)
    30.   {
    31.   delete m_age;
    32.   }
    33.   }
    34.  };
    35.  ​
    36.  ​
    37.  void test01()
    38.  {
    39.   Person p1(18);
    40.   Person p2(12);
    41.   p2 = p1;
    42.   cout << "p1的年龄为:"<<*p1.m_age<
    43.   cout << "p1的年龄为:"<<*p2.m_age<
    44.  }
    45.  ​
    46.  int main(int argc, const char *argv[])
    47.  {
    48.   test01();
    49.   return 0;
    50.  }
    51.  ​

    4.5.5 关系运算符重载

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

    1.  #include
    2.  #include
    3.  using namespace std;
    4.  class Person
    5.  {
    6.   public:
    7.   //重载==
    8.   bool operator==(Person &p)
    9.   {
    10.   if (m_name == p.m_name && m_age == p.m_age)
    11.   {
    12.   return true;
    13.   }
    14.   return false;
    15.   }
    16.   Person(string name , int age)
    17.   {
    18.   m_name = name;
    19.   m_age = age;
    20.   }
    21.   private:
    22.   string m_name;
    23.   int m_age;
    24.  };
    25.  ​
    26.  void test01()
    27.  {
    28.   Person p1("zyl",18);
    29.   Person p2("zyl",18);
    30.   bool b = (p1==p2);
    31.   cout << b  << endl;
    32.  ​
    33.  }
    34.  ​
    35.  int main(int argc, const char *argv[])
    36.  {
    37.   test01();
    38.   return 0;
    39.  }
     
    
    4.5.6 函数调用运算符重载
    • 函数调用运算符()也可以重载

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

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

    1.  #include
    2.  #include
    3.  using namespace std;
    4.  ​
    5.  class MyPrint
    6.  {
    7.   public:
    8.   void operator()(string text)
    9.   {
    10.   cout << text <
    11.   }
    12.  };
    13.  ​
    14.  void test01()
    15.  {
    16.   MyPrint mp;
    17.   mp("hello");//仿函数
    18.  }
    19.  ​
    20.  int main(int argc, const char *argv[])
    21.  {
    22.   test01();
    23.   return 0;
    24.  }
    25.  ​
  • 相关阅读:
    Docker容器脚本编写(Macvlan)
    电线电缆知识全面总结
    咖啡店怎么做才能赚钱?一个公式,简单粗暴简单做到
    使用Spring的 @Retryable 注解优雅实现重处理
    A-level经济难,但是想学好应该怎么做?
    Origin中增加一列并更新绘图
    技能大赛训练:交换机端口带宽限速配置
    REST风格
    虚拟化不是区块链的发展道路,也不是真正意义上的区块链的呈现形式
    NoSQL redis 过期key处理
  • 原文地址:https://blog.csdn.net/weixin_61882921/article/details/133595328