鱼C论坛

 找回密码
 立即注册
查看: 1174|回复: 0

[技术交流] C++旅程第六站-----运算符重载

[复制链接]
发表于 2020-5-14 22:33:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 这是她 于 2020-5-14 22:33 编辑

Although the world is full of suffering,it is also full of the overcoming of it.
                                                                                                                     运算符重载
          加法运算符重载------实现两个自定义数据类型相加的运算

          1、对于内置函数的数据类型的表达式运算符是不可能改变的;
          2、不要滥用运算符重载
  1. <font color="black">#include<iostream>

  2. using namespace std;

  3. class Student
  4. {
  5.         public:
  6.                 int m_A;
  7.                 int m_B;
  8.                
  9.                 Student() {};
  10.                
  11.                 //成员函数实现两个对象相加
  12.                 Student Add(Student &p)
  13.                 {
  14.                         Student temp;
  15.                         temp.m_A = this->m_A + p.m_A;
  16.                         temp.m_B = this->m_B + p.m_B;
  17.                         return temp;
  18.                 }
  19.         
  20.                 //通过成员函数进行重载+号
  21.                 Student operator+ (Student &p)
  22.                 {
  23.                         Student temp;
  24.                         temp.m_A = this->m_A + p.m_A;
  25.                         temp.m_B = this->m_B + p.m_B;
  26.                         return temp;
  27.                 }
  28. };

  29. //通过全局函数进行函数重载
  30. Student operator+(Student &p1, Student &p2)
  31. {
  32.         Student temp;
  33.         temp.m_A = p1.m_A + p2.m_A;
  34.         temp.m_B = p1.m_B + p2.m_B;
  35.         return temp;
  36. }

  37. //函数重载的版本
  38. Student operator+(Student &p1,int num)
  39. {
  40.         Student temp;
  41.         temp.m_A = p1.m_A + num;
  42.         temp.m_B = p1.m_B + num;
  43.         return temp;
  44. }

  45. int main()
  46. {
  47.         //内置数据类型
  48.         int a = 10;
  49.         int b = 10;
  50.         int c = a + b;
  51.         cout << "Value : c : " << c << endl;
  52.         
  53.         Student c1;
  54.         c1.m_A = 88;
  55.         c1.m_B = 22;
  56.         
  57.         Student c2;
  58.         c2.m_A = 66;
  59.         c2.m_B = 44;

  60.         //成员函数重载本质调用
  61.         Student c3 = c1.operator+(c2);
  62.         
  63.         Student c3 = c1 + c2;
  64.         cout << "Value : c3.m_A : " << c3.m_A << endl;
  65.         cout << "Value : c3.m_B : " << c3.m_B << endl;
  66.         
  67.         //全局变量重载本质调用
  68.         Student c3 = operator+(c1,c2);
  69.         
  70.         Student c4 = c1 + c2;
  71.         cout << "Value : c4.m_A : " << c4.m_A << endl;
  72.         cout << "Value : c4.m_B : " << c4.m_B << endl;
  73.         
  74.         //成员函数调用相加
  75.         Student c5 = c1.Add(c2);
  76.         cout << "Value : c5.m_A : " << c5.m_A << endl;
  77.         cout << "Value : c5.m_B : " << c5.m_B << endl;
  78.         
  79.         //运算符重载,函数重载
  80.         Student c6 = operator+(c1,100);
  81.         cout << "Value : c6.m_A : " << c6.m_A << endl;
  82.         cout << "Value : c6.m_B : " << c6.m_B << endl;
  83.         
  84.         return 0;
  85. }</font>
复制代码
         左移运算符------可以输出自定义数据类型
  1. #include<iostream>

  2. using namespace std;

  3. class Person
  4. {
  5.         friend ostream & operator<<(ostream &cout,Person &p);
  6.         private:
  7.                 int m_A;
  8.                 int m_B;
  9.                
  10.         public:
  11.                 Person(int a,int b)
  12.                 {
  13.                         this->m_A = a;
  14.                         this->m_B = b;
  15.                 }
  16. };
  17. //不能利用成员函数重载<<运算符,因为无法实现cout在左侧
  18. //void operator<<(cout)-----------p1 << cout

  19. //只能利用全局变量函数重载左移运算符
  20. //cout只能有一个,所以用& 引用方式
  21. //返回值为ostream-----cout属于ostream输出流
  22. ostream & operator<<(ostream &cout,Person &p)//本质:operator<<(cout,p1)-----cout << p1
  23. {
  24.         cout << "Value : m_A -> " << p.m_A << endl;
  25.         cout << "Value : m_B -> " << p.m_B << endl;
  26.         return cout;
  27. }

  28. int main()
  29. {
  30.         Person p1(99,66);
  31.         
  32.         cout << p1 << endl;
  33.         
  34.         return 0;
  35. }
复制代码
        递增运算符重载------实现自己的整型变量
         前置递增返回的是引用,后置递增返回的是值;
  1. #include<iostream>

  2. using namespace std;

  3. class Library
  4. {
  5.         friend ostream & operator<<(ostream &cout,Library p);
  6.         
  7.         private:
  8.                 int m_num;
  9.                
  10.         public:
  11.                 Library(int a)
  12.                 {
  13.                         this->m_num = a;
  14.                         
  15.                 }
  16.         
  17.                 //重载前置++运算符
  18.                 //返回引用---为了一直对一个数据进行递增操作
  19.                 Library& operator++()
  20.                 {
  21.                         //先进行++运算
  22.                         m_num++;
  23.                         //再将自身做返回
  24.                         return *this;
  25.                 }
  26.                
  27.                 //重载后置++运算符
  28.                 //int代表占位参数,可以用于区分前置和后置递增
  29.                 //返回的是一个值
  30.                 Library operator++(int)
  31.                 {
  32.                         //先记录当前结果
  33.                         Library temp = *this;
  34.                         //在递增
  35.                         m_num++;
  36.                         //最后将记录结果做返回
  37.                         return temp;
  38.                 }
  39. };

  40. ostream & operator<<(ostream &cout,Library p)
  41. {
  42.         cout << "Value : m_num : " << p.m_num << endl;
  43.         return cout;
  44. }

  45. int main()
  46. {
  47.         Library y1(100);
  48.         
  49.         cout << ++y1 << endl;
  50.         cout << y1 << endl;
  51.         
  52.         Library y2(100);
  53.         
  54.         cout << y2++ << endl;
  55.         cout << y2 << endl;
  56.         
  57.         return 0;
  58. }
复制代码
         赋值运算符重载
         C++编译器至少给一个类添加4个函数:
         1、默认构造函数(无参,函数体为空);
         2、默认析构函数(无参,函数体为空);
         3、默认拷贝函数,对属性进行值拷贝;
         4、赋值运算符operator=,对属性进行值拷贝;
  1. #include<iostream>

  2. using namespace std;

  3. class Student
  4. {
  5.         public:
  6.                 //定义一个指针
  7.                 int *m_grade;
  8.                
  9.                 Student(int grade)
  10.                 {
  11.                         //将成绩数据开辟到堆区
  12.                         m_grade = new int(grade);
  13.                 }
  14.                
  15.                 //析构函数-->在这里来释放在堆区开辟的内存
  16.                 ~Student()
  17.                 {
  18.                         if(m_grade != NULL)
  19.                         {
  20.                                 delete m_grade;
  21.                                 m_grade = NULL;
  22.                     }
  23.                 }
  24.                
  25.                 //重载  赋值运算符
  26.                 //由于返回对象->在这里返回值是类名
  27.                 //返回是引用->可以继续进行赋值操作
  28.                 Student& operator=(Student &p)
  29.                 {
  30.                         //浅拷贝
  31.                         //m_grade = p.m_grade;
  32.                        
  33.                         //先判断一下对象是否有属性在堆区;有->释放
  34.                         if(m_grade != NULL)
  35.                         {
  36.                                 delete m_grade;
  37.                                 m_grade = NULL;
  38.                         }
  39.                        
  40.                         //深拷贝
  41.                         m_grade = new int(*p.m_grade);
  42.                        
  43.                         //返回对象本身
  44.                         return *this;
  45.                 }
  46. };

  47. int main()
  48. {
  49.         int a = 10;
  50.         int b = 10;
  51.         int c = 10;
  52.        
  53.         cout << "Value : a -> " << a << endl;
  54.         cout << "Value : b -> " << b << endl;
  55.         cout << "Value : c -> " << c << endl;
  56.        
  57.         Student s1(10);
  58.         Student s2(20);
  59.         Student s3(30);
  60.        
  61.         s3 = s2 = s1;
  62.         cout << "Value : s1 -> " << *s1.m_grade << endl;
  63.         cout << "Value : s2 -> " << *s2.m_grade << endl;
  64.         cout << "Value : s3 -> " << *s3.m_grade << endl;
  65.        
  66.         return 0;
  67. }
复制代码
         关系运算符重载---重载关系运算符,可以让两个自定义类型对象进行对比操作
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. class Student
  5. {
  6.         public:
  7.                 string m_name;
  8.                 int m_num;
  9.                
  10.                 Student(string name,int num)
  11.                 {
  12.                         m_name = name;
  13.                         m_num = num;
  14.                 }
  15.                
  16.                 //重载关系运算符 ==
  17.                 bool operator==(Student &p)
  18.                 {
  19.                         //满足姓名和学号相等
  20.                         if(this->m_name == p.m_name && this->m_num == p.m_num)
  21.                         {
  22.                                 return true;
  23.                         }
  24.                        
  25.                         return false;
  26.                 }
  27.                
  28.                 //重载关系运算符  !=
  29.                 bool operator!=(Student &p)
  30.                 {
  31.                         if(this->m_name == p.m_name && this->m_num == p.m_num)
  32.                         {
  33.                                 return false;
  34.                         }
  35.                        
  36.                         return true;
  37.                 }
  38. };

  39. int main()
  40. {
  41.         Student p1("张三",3333);
  42.         Student p2("张三",3333);
  43.         Student p3("李四",3333);
  44.        
  45.         if (p1 == p2)
  46.         {
  47.                 cout << "p1和p2是相等的!!!!" << endl;
  48.         }
  49.         else
  50.         {
  51.                 cout << "p1和p2是不相等的!!!" << endl;
  52.         }
  53.        
  54.         if (p1 == p3)
  55.         {
  56.                 cout << "p1和p3是相等的!!!!" << endl;
  57.         }
  58.         else
  59.         {
  60.                 cout << "p1和p3是不相等的!!!!" << endl;
  61.         }
  62.         if (p1 != p2)
  63.         {
  64.                 cout << "p1和p2是不相等的!!!!" << endl;
  65.         }
  66.         else
  67.         {
  68.                 cout << "p1和p2是相等的!!!" << endl;
  69.         }
  70.        
  71.         if (p1 != p3)
  72.         {
  73.                 cout << "p1和p3是不相等的!!!!" << endl;
  74.         }
  75.         else
  76.         {
  77.                 cout << "p1和p3是相等的!!!!" << endl;
  78.         }
  79.        
  80.         return 0;
  81. }
复制代码
        函数调用运算符重载   ()-------由于重载使用的方法非常像函数的调用->仿函数(没有固定的写法,非常灵活)
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. class Student
  5. {
  6.         public:
  7.                 //重载函数调用运算符 ()
  8.                 void operator()(string something)
  9.                 {
  10.                         cout << something << endl;
  11.                 }
  12.             
  13. };

  14. void kuohao(string something)
  15. {
  16.         cout << something << endl;
  17. }

  18. //仿函数非常灵活,例如下面的乘
  19. class Mymul
  20. {
  21.         public:
  22.                 int operator()(int num1,int num2)
  23.                 {
  24.                         return num1 * num2;
  25.                 }
  26. };

  27. int main()
  28. {
  29.         Student p1;
  30.        
  31.         //由于使用起来非常类似于函数调用,一次成为仿函数
  32.         p1("Although the world is full of suffering,it is also full of the overcoming of it.");
  33.        
  34.         kuohao("Although the world is full of suffering,it is also full of the overcoming of it.");
  35.        
  36.         cout << "----------------------------------------------------------------------------" << endl;
  37.        
  38.         Mymul p2;
  39.         int a = p2(10,10);
  40.         cout << "a = " << a << endl;
  41.        
  42.         //匿名函数对象----一个匿名对象,又重载()---当前行执行完,立即释放
  43.         cout << Mymul()(100,10) << endl;
  44.        
  45.         return 0;
  46. }
复制代码

                                                渣渣一个但在努力中哦.........................................













本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-17 12:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表