九.运算符重载

1.Code :

#include<iostream>
using namespace std;//运算符重载的意义:
//对已有的 运算符 重新进行定义,赋予其另一项功能,以适应不同的数据类型
// operator : 操作人员//   一. 加号运算符重载
//作用:  实现两个自定义数据类型相加的运算
//总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
//总结2:不要滥用运算符重载class Person
{public:Person() {};//默认构造函数Person(int a, int b){this->pa = a;this->pb = b;}/*Person operator + (const Person& P)//复制函数 //成员函数实现 + 号运算符重载{cout << " operator + (const Person& P) " << endl;Person t;//若无定义默认构造函数,则此处 Errort.pa = this->pa + P.pa;t.pb = this->pb + P.pb;return t;}*/
public:int pa = 2;int pb = 2;
};//全局函数实现 + 号运算符重载
Person operator + (const Person& a, const Person& b)
{cout << " operator + (const Person& a, const Person& b) " << endl;Person c(0,0);c.pa = a.pa + b.pa;c.pb = a.pb + b.pb;return c;
}//运算符重载 可以发生函数重载
Person operator + (const Person& P2, int val)
{cout << " operator + (const Person& P2, int val) " << endl;Person te;te.pa = P2.pa + val;te.pb = P2.pb - val;return te;
}void test()
{Person P1;Person P2;Person P3 = P1 + P2;//相当于 P2.operator + (P1,P2)cout << "pa = " << P3.pa << "    pb = " << P3.pb << endl;Person P4 = P3 + 10;//相当于 operator + (P3,10)cout << "pa = " << P4.pa << "    pb = " << P4.pb << endl;Person P5 = P3 + P4;//相当于 P5.operator + (P3,P4)//Person P5;//P5.operator + (P3);cout << "pa = " << P5.pa << "    pb = " << P5.pb << endl;}//    二. 左移运算符重载
//作用: 可以输出自定义数据类型
//总结:重载左移运算符配合友元可以实现输出自定义数据类型
class Person1
{friend ostream& operator<<(ostream& out, Person1& P);//友元public:Person1(int a, int b){this->pa = a;this->pb = b;}//利用成员函数重载 左移运算符 P.operator << (out) 简化版 P << out// 由上边可知 不能利用成员函数重载 << 运算符,因为无法实现 cout 在左侧/*void operator << (ostream out){}*/
private:int pa = 1;int pb = 1;
};//只能 利用全局函数实现左移重载
//ostream 对象只能有一个
ostream& operator << (ostream& s, Person1& P)
//本质: operator <<(s,P) 简化 s << P
{s << "P.pa = " << P.pa << endl<< "P.pb = " << P.pb << endl;return s;
}void test1()
{Person1 P1(10,20);cout << P1 << "Good Boy" << endl;//链式编程}//   三.递增运算符重载
//作用: 通过重载递增运算符,实现自己的整型数据
//总结: 前置递增返回引用,后置递增返回值class Person2
{friend ostream& operator<<(ostream& out,Person2 P1);
public:Person2(){cout << "构造函数" << endl;a = 5;}//前置++Person2& operator++(){//先++a++;//再返回return *this;//返回对象本身}//后置++//Person2 operator++(int) int代表占位参数,用以区分前/后置递增Person2 operator++(int){//先返回Person2 p1 = *this;//记录当前本身的值,然后让本身的值加一,//但是返回的是以前的值,达到 先返回后++ 的效果a++; return p1;}
private:int a = 0;
};//重载左移 << 运算符
ostream& operator<<(ostream& out, Person2 myint)
{out << myint.a;return out;
}void test2()
{// 前置++  先++ 再返回Person2 P21;cout << ++P21 << endl;//原值为5,++ 后 为6cout << P21 << endl;// 为6// 后置++  先返回 再++Person2 P22;cout << P22++ << endl;//原值为5 返回未加之前的值 5cout << P22 << endl;//返回 加 1 后的值 为6
}//   四. 赋值(=) 运算符重载
//前言: C++编译器至少给一个类添加 4 个函数:
//                                      1.默认构造函数 (无参,函数体为空)
//                              2.默认拷贝函数 ,对属性进行值拷贝
//                      3.默认析构函数 (无参,无函数体)
//              4.赋值运算符 operator = ,对属性进行值拷贝
//        若类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
//
//为什么要进行赋值运算符的重载?
//答:防止指向堆区的值出现深浅拷贝的问题.
class Person3
{public:Person3(int q)//构造函数{a = new int(q);//将 q 值存入到开辟的堆区,并将地址返回给 a}Person3& operator=(Person3& P)//每当进行赋值的时候 运行赋值运算符 重载{//p1 = p2;cout << " 赋值运算符 重载 " << endl;if (a != NULL)//释放指向 赋值类(p2) 堆区的 被赋值类(p1) 元素a{delete a;a = NULL;}//编译器 默认 提供了浅拷贝代码://a = P.a;//需提供深拷贝 解决浅拷贝的问题//将被赋值类(p1)的元素a开辟新空间a = new int(*P.a);return *this;}~Person3(){cout << " 析构函数 " << endl;if (a != NULL){delete a;a = NULL;//防止野指针}}
public:int *a;
};void test3()
{Person3 p1(18);Person3 p2(20);Person3 p3(22);cout << "p1 年龄为:" << *p1.a << endl;cout << "p2 年龄为:" << *p2.a << endl;cout << "p3 年龄为:" << *p3.a << endl;p1 = p2;//将p2的数据赋值给p1cout << "p1 年龄为:" << *p1.a << endl;p1 = p2 = p3;cout << "p1 年龄为:" << *p1.a << endl;cout << "p2 年龄为:" << *p2.a << endl;cout << "p3 年龄为:" << *p3.a << endl;
}//   五.关系(== < >)运算符重载
//作用: 重载关系运算符,可以让两个自定义类型对象进行对比操作class Person4
{public:Person4(string name, int age){this->m_name = name;this->m_age = age;}bool operator==(Person4& P){if (this->m_name == P.m_name && this->m_age == P.m_age){return true;}else return false;}bool operator<(Person4& P){if (this->m_age < P.m_age) { return true; }else return false;}bool operator!=(Person4& P){if (this->m_name != P.m_name && this->m_age != P.m_age){return true;}else return false;}string m_name;int m_age;
};void test4()
{Person4 P1("沉鱼", 12);Person4 P2("沉鱼", 12);Person4 P3("秀虾", 23);cout << "P1(沉鱼, 12) P2(沉鱼, 12) if (P1 == P2) = " << endl;if (P1 == P2){cout << " 相等 " << endl;}else cout << " 不相等 " << endl;cout << "P1(沉鱼, 12) P3(沉鱼, 23) if (P1 != P3) = " << endl;if (P1 != P3){cout << " 不相等 " << endl;}else cout << " 相等 " << endl;cout << "P1(沉鱼, 12) P3(沉鱼, 23) if (P1 < P3) = " << endl;if (P1 < P3){cout << " 小于 " << endl;}else cout << " 大于或等于 " << endl;
}//   六.函数调用()运算符重载
//由于重载之后使用方式非常像函数调用,因此成为仿函数
//仿函数没有固定写法,非常灵活class Person5
{public:void operator()(string T){cout << T << endl;}
};class Person6
{public:int operator()(int a,int b){return (a + b) * 2;}
};void test5()
{//重载的()操作符 也称为仿函数Person5 p1;Person6 p2;p1(" 仿函数 ");cout << "p2(1,2) = " << p2(1, 2) << endl;//匿名对象调用cout << "Person6()(4,5) = " << Person6()(4,5) << endl;}int main()
{cout << "运算符重载" << endl;cout << endl << "一. 加号运算符重载" << endl;test();cout << endl << "二. 左移运算符重载" << endl;test1();cout << endl << "三. 递增运算符重载 " << endl;test2();cout << endl << "四. 赋值 运算符重载 " << endl;test3();cout << endl << "五. 关系运算符重载" << endl;test4();cout << endl << "六.函数调用()运算符重载" << endl;test5();system("pause");return 0;}

2.运行结果 :

(C++学习笔记七)运算符重载相关推荐

  1. c++学习笔记之运算符重载

    1.重载运算符 void operator+(....)        //重载"+"运算符 (1)如果是成员函数形式,那么必须是非static (2)使用运算符必须重载,除了&q ...

  2. Typescript 学习笔记七:泛型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  3. 吴恩达《机器学习》学习笔记七——逻辑回归(二分类)代码

    吴恩达<机器学习>学习笔记七--逻辑回归(二分类)代码 一.无正则项的逻辑回归 1.问题描述 2.导入模块 3.准备数据 4.假设函数 5.代价函数 6.梯度下降 7.拟合参数 8.用训练 ...

  4. websocket 获取连接id_Swoole学习笔记七:搭建WebSocket长连接 之 使用 USER_ID 作为身份凭证...

    Swoole学习笔记七:搭建WebSocket长连接 之 使用 USER_ID 作为身份凭证 2年前 阅读 3678 评论 0 喜欢 0 ### 0.前言 前面基本的WebSocket操作,我们基本都 ...

  5. ROS学习笔记七:使用rqt_console和roslaunch

    ROS学习笔记七:使用rqt_console和roslaunch 本节主要介绍在调试时使用的rqt_console和rqt_logger_level,以及一次性打开多个节点的工具roslaunch. ...

  6. 【K210】K210学习笔记七——使用K210拍摄照片并在MaixHub上进行训练

    [K210]K210学习笔记七--使用K210拍摄照片并在MaixHub上进行训练 前言 K210准备工作 K210如何拍摄照片 准备工作 拍摄相关代码定义 用K210拍摄到的照片在MaixHub平台 ...

  7. Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...

  8. window的dos命令学习笔记 七

    文章目录 一.dos历史学习笔记(后期整合到这里,我想能学到这里的应该不多了,嘿嘿,加油) 二.执行状态返回值(`%errorlevel%`,和shell中`$?`相似): 三.视窗 1.color ...

  9. ESP32学习笔记(七) 复位和时钟

    ESP32学习笔记(七) 复位和时钟 目录: ESP32学习笔记(一) 芯片型号介绍 ESP32学习笔记(二) 开发环境搭建 VSCode+platformio ESP32学习笔记(三) 硬件资源介绍 ...

最新文章

  1. 非常详细的sift算法原理解析
  2. oracle 10g磁盘管理,Oracle 10g UNDO表空间过大导致磁盘空间不足的解决
  3. 大系统观:第2章 系统论概述
  4. XML Schema 基本结构
  5. OSError: [Errno 22] Invalid argument:**
  6. 2017和2019对比图刷屏,真实扎心!苹果中国女老板也跟风晒图...
  7. 【更新】火星人敏捷开发手册 2011-12-31
  8. java什么叫元素_java-什么是HTTP标头元素?
  9. 如何实现甘特图独立安装
  10. 从苏宁电器到卡巴斯基第28篇:难忘的三年硕士时光 VI
  11. 如何在Win7自安装驱动
  12. Linux60个小时速成
  13. Google广告数据分析与优化总结
  14. 服务器搬迁需要注意的几个地方
  15. linux下的网络管理命令,【linux】常见的网络管理命令
  16. 高盛为什么认为中国AI领域将超越美国?
  17. Freeswitch呼入及呼出录音问题
  18. java导出excel表格,文件名称汉字话
  19. XML学习-方立勋视频学习
  20. 二维空间最近点对问题 python

热门文章

  1. 【正点原子MP157连载】 第六章 TF-A 使用-摘自【正点原子】【正点原子】STM32MP1嵌入式Linux驱动开发指南V1.7
  2. Android在线源码网站推荐
  3. python和vb的区别 程序语句_python与VB的区别?
  4. 雷·克兹维尔:人工智能正造福全人类
  5. 官媒痛批“精神鸦片”,曾拿百万年终奖的腾讯游戏员工要失业了吗?
  6. 测试工程师必会能力之缺陷分析入门
  7. 我爱赚钱吧:学生都可以做的兼职工作
  8. rabbitmq的安装和配置
  9. 解密一个量化对冲基金开发人员的工作内容
  10. Velocity是什么?