加号运算符重载: 重载加号运算符,让 p3 = p1 + p2 改成 p3.mage = p1.mage + p2.mage 实现两个数据成员的相加。

告诉编译器,两个类中的数据成员应该怎么相加。

成员函数相加 +号运算符重载 成员函数 二元

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person
  5. {
  6. public:
  7. int m_x;
  8. int m_y;
  9. Person(){};
  10. Person(int x, int y) :m_x(x), m_y(y) {}
  11. // 加号运算符重载,这里其实是二元,因为隐藏了一个this指针。
  12. Person operator + (Person &p)
  13. {
  14. Person tmp;
  15. tmp.m_x = this->m_x + p.m_x;
  16. tmp.m_y = this->m_y + p.m_y;
  17. return tmp;
  18. }
  19. };
  20. int main(int argc, char *argv[])
  21. {
  22. Person p1(10, 10);
  23. Person p2(20, 20);
  24. Person p3 = p1 + p2;
  25. cout << "p3 m_x = > " << p3.m_x << endl;
  26. cout << "p3 m_y = > " << p3.m_y << endl;
  27. system("pause");
  28. return 0;
  29. }

全局函数相加,实现运算符重载

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person
  5. {
  6. public:
  7. int m_x;
  8. int m_y;
  9. Person(){};
  10. Person(int x, int y) :m_x(x), m_y(y) {}
  11. };
  12. // 全局函数实现运算符重载,这个就属于二元运算符重载
  13. Person operator +(Person &p1, Person &p2)
  14. {
  15. Person tmp;
  16. tmp.m_x = p1.m_x + p2.m_x;
  17. tmp.m_y = p1.m_y + p2.m_y;
  18. return tmp;
  19. }
  20. int main(int argc, char *argv[])
  21. {
  22. Person p1(10, 30);
  23. Person p2(20, 50);
  24. Person p3 = p1 + p2;
  25. cout << "p3 m_x = > " << p3.m_x << endl;
  26. cout << "p3 m_y = > " << p3.m_y << endl;
  27. system("pause");
  28. return 0;
  29. }

左移运算符重载: 使用 << 重载左移运算符,让cout 直接输出两个变量。重载左移运算符不可以写成成员函数,只能写全局运算符。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person
  5. {
  6. public:
  7. int m_x;
  8. int m_y;
  9. Person(){};
  10. Person(int x, int y) :m_x(x), m_y(y) {}
  11. };
  12. ostream& operator << (ostream &cout, Person &ptr)
  13. {
  14. cout << "m_x = " << ptr.m_x << " ----> " << "m_y = " << ptr.m_y << endl;
  15. return cout;
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19. Person p1(10, 30);
  20. Person p2(20, 10);
  21. cout << p1 << endl;
  22. cout << p2 << endl;
  23. system("pause");
  24. return 0;
  25. }

使用友元函数,访问私有的数据。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person
  5. {
  6. friend ostream& operator<<(ostream &cout, Person &ptr);
  7. private:
  8. int m_x;
  9. int m_y;
  10. public:
  11. Person(){};
  12. Person(int x, int y) :m_x(x), m_y(y) {}
  13. };
  14. ostream& operator << (ostream &cout, Person &ptr)
  15. {
  16. cout << "m_x = " << ptr.m_x << " ----> " << "m_y = " << ptr.m_y << endl;
  17. return cout;
  18. }
  19. int main(int argc, char *argv[])
  20. {
  21. Person p1(10, 30);
  22. Person p2(20, 10);
  23. cout << p1 << endl;
  24. cout << p2 << endl;
  25. system("pause");
  26. return 0;
  27. }

前置/后置运算符的重载:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class MyInteger
  5. {
  6. friend ostream& operator<<(ostream& cout, MyInteger & myInt);
  7. public:
  8. int m_count;
  9. public:
  10. MyInteger() { m_count = 0; }
  11. // 重载前置 ++x 运算符
  12. MyInteger& operator ++ ()
  13. {
  14. this->m_count++;
  15. return *this;
  16. }
  17. // 重载后置 x++ 运算符,为了区分前后置,需要在参数后面增加一个int占位符
  18. // 此时编译器才会认为我们需要使用后置重载运算符了
  19. MyInteger operator ++ (int)
  20. {
  21. MyInteger tmp = *this;
  22. m_count++;
  23. return tmp;
  24. }
  25. };
  26. ostream& operator<<(ostream& cout, MyInteger & myInt)
  27. {
  28. cout << myInt.m_count;
  29. return cout;
  30. }
  31. int main(int argc, char *argv[])
  32. {
  33. MyInteger myInt;
  34. cout << ++myInt << endl;
  35. cout << myInt++ << endl;
  36. cout << ++(++myInt) << endl;
  37. system("pause");
  38. return 0;
  39. }

指针运算符重载(智能指针) 用来托管自定义的对象,让对象可以自动的释放数据,

当我们使用一个对象结束以后,无需手动释放堆空间,智能指针会帮助我们完成这个过程。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student
  5. {
  6. public:
  7. char *m_name;
  8. int m_age;
  9. public:
  10. Student(char *name, int age)
  11. {
  12. this->m_name = name;
  13. this->m_age = age;
  14. }
  15. void Print()
  16. {
  17. cout << "Name: " << this->m_name << endl;
  18. cout << "Age: " << this->m_age << endl;
  19. }
  20. };
  21. // 定义智能指针,用于自动释放对象所占用的空间
  22. class Smart_Pointer
  23. {
  24. private:
  25. Student *ptr;
  26. public:
  27. // 先来执行构造函数,将传入的指针复制到内部
  28. Smart_Pointer(Student *ptr)
  29. { this->ptr = ptr; }
  30. // 重载运算符 -> 让智能指针能够直接指向Student
  31. Student * operator -> ()
  32. { return this->ptr; }
  33. // 重载运算符 *
  34. Student & operator * ()
  35. { return *this->ptr; }
  36. // 定义析构函数,这是智能指针的关键部分,对象会被自动释放
  37. ~Smart_Pointer()
  38. {
  39. if (this->ptr != NULL)
  40. {
  41. delete this->ptr;
  42. this->ptr = NULL;
  43. }
  44. }
  45. };
  46. int main(int argc, char *argv[])
  47. {
  48. // 手动释放的案例:平常的使用方式
  49. Student *stu = new Student("lyshark", 10);
  50. stu->Print();
  51. delete stu;
  52. // 使用智能指针:则无需考虑释放的问题
  53. Smart_Pointer ptr(new Student("lyshark", 10));
  54. ptr->Print();
  55. (*ptr).Print();
  56. system("pause");
  57. return 0;
  58. }

赋值运算符重载: 我们将等于号进行重载,实现对类中数据成员的赋值拷贝。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student
  5. {
  6. public:
  7. int m_uid;
  8. char *m_name;
  9. public:
  10. Student(int uid, char *name)
  11. {
  12. this->m_uid = uid;
  13. this->m_name = new char[strlen(name) + 1];
  14. strcpy(this->m_name, name);
  15. }
  16. // 重载 = 实现类数据成员的赋值运算
  17. Student& operator = (const Student &ptr)
  18. {
  19. // 先来判断原来的堆区是否有内容,如果有则先来释放
  20. if (this->m_name != NULL)
  21. {
  22. this->m_uid = 0;
  23. delete[] this->m_name;
  24. this->m_name = NULL;
  25. }
  26. // 否则,我们直接开辟空间完成内存拷贝
  27. this->m_name = new char[strlen(ptr.m_name) + 1];
  28. strcpy(this->m_name, ptr.m_name);
  29. this->m_uid = ptr.m_uid;
  30. return *this;
  31. }
  32. // 析构函数,则需要释放内存
  33. ~Student()
  34. {
  35. if (this->m_name != NULL)
  36. {
  37. this->m_uid = 0;
  38. delete[] this->m_name;
  39. this->m_name = NULL;
  40. }
  41. }
  42. };
  43. int main(int argc, char *argv[])
  44. {
  45. Student stu1(1,"lyshark");
  46. Student stu2(2, "admin");
  47. Student stu3(0, "");
  48. stu3 = stu2 = stu1;
  49. cout << stu3.m_name << endl;
  50. cout << stu2.m_name << endl;
  51. cout << stu1.m_name << endl;
  52. system("pause");
  53. return 0;
  54. }

关系运算符重载:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student
  5. {
  6. public:
  7. int m_uid;
  8. char * m_name;
  9. public:
  10. Student(int uid,char *name)
  11. {
  12. this->m_uid = uid;
  13. this->m_name = name;
  14. }
  15. bool operator == (Student &ptr)
  16. {
  17. if (this->m_uid == ptr.m_uid && this->m_name == ptr.m_name)
  18. return true;
  19. return false;
  20. }
  21. bool operator != (Student &ptr)
  22. {
  23. if (this->m_uid != ptr.m_uid && this->m_name != ptr.m_name)
  24. return true;
  25. return false;
  26. }
  27. };
  28. int main(int argc, char *argv[])
  29. {
  30. Student stu1(1, "lyshark");
  31. Student stu2(1, "lyshark");
  32. Student stu3(2, "admin");
  33. if (stu1 == stu2)
  34. cout << "stu1 = stu2" << endl;
  35. if (stu1 != stu3)
  36. cout << "stu1 != stu3" << endl;
  37. system("pause");
  38. return 0;
  39. }

重载与仿函数:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class MyPrint
  5. {
  6. public: void operator()(string text)
  7. {
  8. cout << text << endl;
  9. }
  10. };
  11. class MyAdd
  12. {
  13. public: int operator()(int x, int y)
  14. {
  15. return x + y;
  16. }
  17. };
  18. int main(int argc, char *argv[])
  19. {
  20. MyPrint print;
  21. print("hello lyshark"); // 使用仿函数
  22. cout << MyAdd()(100, 200) << endl; // 匿名仿函数
  23. system("pause");
  24. return 0;
  25. }

C/C++ 关于运算符重载笔记的更多相关文章

  1. c++运算符重载笔记

    运算符重载的概念:给原有的运算符赋予新的功能: 比如:+ 不仅可以做算术运算也可以连接俩个字符串 一元运算符:只与一个操作数进行运算 比如 正负号 运算符重载的本质是:函数重载. <<与& ...

  2. C++学习笔记之运算符重载

    一.运算符重载基本知识 在前面的一篇博文 C++学习笔记之模板(1)——从函数重载到函数模板 中,介绍了函数重载的概念,定义及用法,函数重载(也被称之为函数多态)就是使用户能够定义多个名称相同但特征标 ...

  3. 初步C++运算符重载学习笔记&lt;3&gt; 增量递减运算符重载

    初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数     增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...

  4. 初探C++运算符重载学习笔记&lt;2&gt; 重载为友元函数

    初探C++运算符重载学习笔记 在上面那篇博客中,写了将运算符重载为普通函数或类的成员函数这两种情况. 以下的两种情况发生.则我们须要将运算符重载为类的友元函数 <1>成员函数不能满足要求 ...

  5. c++中的运算符重载operator2(翁恺c++公开课[31-33]学习笔记)

    上一篇operator1中,大概说了下重载的基本用法,接下来对c++中常见的可重载运算符归一下类,说一下它们的返回值,讨论下较为复杂的运算符重载上的坑

  6. c++中的运算符重载operator1(翁恺c++公开课[30]学习笔记)

    运算符重载规则: 只有已经存在的运算符才能被重载,不能自己制造一个c++中没有的运算符进行重载 重载可以在类或枚举类型内进行,也可以是全局函数,但int.float这种已有的类型内是不被允许的 不能二 ...

  7. C++基础 学习笔记五:重载之运算符重载

    C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...

  8. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  9. C++ Primer笔记10_运算符重载_赋值运算符_进入/输出操作符

    1.颂值运营商 首先来福值运算符引入后面要说的运算符重载.上一节说了构造函数.拷贝构造函数:一个类要想进行更好的控制.须要定义自己的构造函数.拷贝构造函数.析构函数.当然,还有赋值运算符.常说的三大函 ...

  10. 《Inside C#》笔记(十一) 运算符重载

    运算符重载与之前的索引器类似,目的是为了让语言本身使用起来更方便直接,也是一种语法糖. 一 运算符重载(Operator Overloading) 运算符重载的存在,使得现有的各种运算符可以被重新定义 ...

随机推荐

  1. Android gradle dependency tree change(依赖树变化)监控实现,sdk version 变化一目了然

    @ 目录 前言 基本原理 执行流程 diff 报告 不同分支 merge 过来的 diff 报告 同个分支产生的 merge 报告 同个分支提交的 diff 报告 具体实现原理 我们需要监控怎样的 D ...

  2. VMware15.5安装Ubuntu20.04

    一.安装前的准备 1.下载好Ubuntu20.04的镜像文件,直接从官网下载就好,激活密匙. 2.准备好VMware软件,这里就忽略安装过程了. 二.建立虚拟机以及开启正式的Ubuntu安装过程 参考 ...

  3. vivo 应用商店推荐系统探索与实践

    介绍 vivo 应用商店推荐系统如何高效支撑个性化的推荐需求. 一.前言 商店的应用数据主要来源于运营排期.CPD.游戏.算法等渠道,成立推荐项目之后也没有变化,发生变化的是由推荐系统负责和数据源进行 ...

  4. vue学习笔记 六、ref定义单个数据

    系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...

  5. vue后台管理系统,接口环境配置

    https://coding.imooc.com/lesson/397.html#mid=31487

  6. P3842-DP【黄】

    想搜索到最后一层,就必得先完成前面层的搜索任务,这构成了对状态转移的启示,即当前层的DP值应该是此前层转移过来后得到的最佳值. 但这道题看数据范围应该不能用二维数组,抱着侥幸的心理我使用了动态二维数组 ...

  7. java进阶(7)--Object类-toString()/equals()/finalize()/hashCode()

    一.object类介绍 object类这个老祖宗中的方法,所有子类通用,直接或间接继承. 学习常用方法即可 列表 prtected object clone()             //对象克隆 ...

  8. [转帖]linux设置page cache大小,Linux Page Cache调优在Kafka中的应用

    本文首发于 vivo互联网技术 微信公众号 链接: 作者:Yang Yijun 本文主要描述Linux Page Cache优化的背景.Page Cache的基本概念.列举之前针对Kafka的 IO ...

  9. [转帖]THE OSWATCHER ANALYZER USER'S GUIDE

    oswbba THE OSWATCHER ANALYZER USER'S GUIDE Carl DavisMay 7, 2019 To see how to use this tool and it' ...

  10. [转帖]InnoDB表聚集索引层高什么时候发生变化

    导读 本文略长,主要解决以下几个疑问 1.聚集索引里都存储了什么宝贝 2.什么时候索引层高会发生变化 3.预留的1/16空闲空间做什么用的 4.记录被删除后的空间能回收重复利用吗 1.背景信息 1.1 ...