运算符重载(个人学习笔记黑马学习)

1、加号运算符重载

#include <iostream>
using namespace std;
#include <string>//加号运算符重载
class Person {
public://1、成员函数重载+号//Person operator+(Person& p) {//	Person temp;//	temp.m_A = this->m_A + p.m_A;//	temp.m_B = this->m_B + p.m_B;//	return temp;//}int m_A;int m_B;
};//2、全局函数重载+号 
Person operator+(Person &p1,Person &p2) {Person temp;temp.m_A= p1.m_A + p2.m_A;temp.m_B = p1.m_B + p2.m_B;return temp;
}//函数重载的版本
Person operator+(Person& p1, int num) {Person temp;temp.m_A = p1.m_A + num;temp.m_B = p1.m_B + num;return temp;
}void test01() {Person p1;p1.m_A = 10;p1.m_B = 10;Person p2;p2.m_A = 10;p2.m_B = 10;//成员函数重载本质调用//Person p3 = p1.operator+(p2);//全局函数重载本质调用//Person p3 = operator+(p1, p2);//运算符重载 也可以发生函数重载Person p4 = p1 + 100;// Person + intcout << "p4.m_A = " << p4.m_A << endl;cout << "p4.m_B = " << p4.m_B << endl;Person p3 = p1 + p2;cout << "p3.m_A = " << p3.m_A << endl;cout << "p3.m_B = " << p3.m_B << endl;
}int main() {test01();system("pause");return 0;
}


2、左移运算符重载

#include <iostream>
using namespace std;
#include <string>//左移运算符class Person {friend ostream& operator<<(ostream& cout, Person& p);
//public:public:Person(int a, int b) {m_A = a;m_B = b;}
private://利用成员函数重载 左移运算符 p,operator<<(cout) 简化版本p<<cout//不会利用成员函数重载<<运算符 因为无法实现 cout在左侧/*void operator<<(cout) {}*/int m_A;int m_B;};
//只能利用全局函数重载左移运算符
ostream& operator<<(ostream& cout, Person& p) {//本质:operator<<(cout,p)  简化cout<<pcout << "m_A = " << p.m_A << " m_B = " << p.m_B ;return cout;}void test01() {/*Person p;p.m_A = 10;p.m_B = 10;*/Person p(10,10);cout << p << endl;;
}int main() {test01();system("pause");return 0;
}

3、递增运算符重载

#include <iostream>
using namespace std;
#include <string>//递增运算符重载//自定义整型
class MyInteger {friend ostream& operator<<(ostream& cout, MyInteger myint);public:MyInteger() {m_Num = 0;}//重载前置++运算符  返回引用为了一直对一个数据进行递增操作MyInteger& operator++() {//先进行++运算  在将自身做返回m_Num++;return *this;}//重载后置++运算符//void operator++(int) int代表占位参数,可以用于区分前置和后置MyInteger operator++(int) {//先 记录当前结果MyInteger temp = *this;//后递增m_Num++;//最后将记录结果做返回return temp;}private:int m_Num;
};//重载<<运算符
ostream& operator<<(ostream& cout, MyInteger myint) {cout << myint.m_Num;return cout;
}void test01() {MyInteger myint;//cout << ++myint << endl;cout << ++(++myint) << endl;cout << myint << endl;
}void test02() {MyInteger myint;cout << myint++ << endl;cout << myint << endl;
}int main() {//test01();test02();system("pause");return 0;
}

4、赋值运算符重载

#include <iostream>
using namespace std;
#include <string>//赋值运算符重载
class Person {
public:Person(int age) {m_Age=new int(age);}~Person() {if (m_Age != NULL) {delete m_Age;m_Age = NULL;}}//重载 赋值运算符Person& operator=(Person& p) {//编译器提供浅拷贝//m_Age = p.m_Age;//应该先判断是否有属性在堆区,如果有先释放干净,融合再深拷贝if (m_Age != NULL) {delete m_Age;m_Age = NULL;}//深拷贝m_Age = new int(*p.m_Age);return *this;}int* m_Age;
};void test01() {Person p1(18);Person p2(20);Person p3(30);p3 = p2 = p1;//赋值操作cout << "p1的年龄为:" << *p1.m_Age << endl;cout << "p2的年龄为:" << *p2.m_Age << endl;cout << "p3的年龄为:" << *p3.m_Age << endl;}int main() {test01();system("pause");return 0;
}


5、关系运算符重载

#include <iostream>
using namespace std;
#include <string>//重载关系运算符class Person {
public:Person(string name, int age) {m_Name = name;m_Age = age;}//重载关系运算符==bool operator== (Person& p) {if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {return true;}return false;}//重载关系运算符!=bool operator!= (Person& p) {if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {return false;}return true;}string m_Name;int m_Age;
};void test01() {Person p1("Tom", 18);Person p2("Tom", 18);if (p1 == p2) {cout << "p1和p2是相等的" << endl;}else {cout << "p1和p2是不相等的" << endl;}if (p1 != p2) {cout << "p1和p2是不相等的" << endl;}else {cout << "p1和p2是相等的" << endl;}}int main() {test01();system("pause");return 0;
}


6、函数调用运算符重载

#include <iostream>
using namespace std;
#include <string>//函数调用运算符重载//打印输出类
class MyPrint {
public://重载函数调用运算符void operator()(string test){cout << test << endl;}
};void test01() {MyPrint myPrint;myPrint("hello world");//由于使用起来非常类似于函数调用 因此称为仿函数}//仿函数非常灵活,没有固定的写法
//加法类class MyAdd {
public:int operator()(int num1, int num2) {return num1 + num2;}
};void test02() {MyAdd myadd;int ret=myadd(100, 100);cout << "ret = " << ret << endl;//匿名函数对象cout << MyAdd()(100, 100) << endl;
}int main() {//test01();test02();system("pause");return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/74512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【算法】快速排序 详解

快速排序 详解 快速排序1. 挖坑法2. 左右指针法 &#xff08;Hoare 法&#xff09;3. 前后指针法4. 快排非递归 代码优化 排序&#xff1a; 排序&#xff0c;就是使一串记录&#xff0c;按照其中的某个或某些关键字的大小&#xff0c;递增或递减的排列起来的操作。 稳定性&…

docker介绍和安装

docker安装 下载Docker依赖组件 yum -y install yum-utils device-mapper-persistent-data lvm2 设置下载Docker的镜像源为阿里云 yum-config-manager --add-repo http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo 安装Docker服务 yum -y install docker-ce 安…

OpenCV项目实战(1)— 如何去截取视频中的帧

前言&#xff1a;Hello大家好&#xff0c;我是小哥谈。针对一段视频&#xff0c;如何去截取视频中的帧呢&#xff1f;本节课就给大家介绍两种方式&#xff0c;一种方式是按一定间隔来截取视频帧&#xff0c;另一种方式是截取视频的所有帧。希望大家学习之后能够有所收获&#x…

Web学习笔记-React(路由)

笔记内容转载自 AcWing 的 Web 应用课讲义&#xff0c;课程链接&#xff1a;AcWing Web 应用课。 CONTENTS 1. Web分类2. Route组件3. URL中传递参数4. Search Params传递参数5. 重定向6. 嵌套路由 本节内容是如何将页面和 URL 一一对应起来。 1. Web分类 Web 页面可以分为两…

微信小程序 wx:if使用

在微信小程序中&#xff0c;可以使用wx:if指令来控制某个元素是否需要被渲染到页面上。根据条件表达式的结果&#xff0c;wx:if指令决定元素是否显示。 下面是使用wx:if的基本示例&#xff1a; <view><view wx:if"{{condition}}"><!-- 条件为真时显…

计算机网络概述

目录 一、计算机网络的作用及互联网概述 1.1计算机网络在信息时代中的作用 1.2基本概念 1.3互联网基础架构发展三个阶段 1.4互联网的标准化工作 二、互联网的组成 2.1互联网组成 2.2互联网的边缘部分 2.3互联网的核心部分 三、计算机网络的类别 3.1计算机网络的定义:…

yapi以及gitlab的容器化部署

yapi部署&#xff1a; https://blog.csdn.net/Chimengmeng/article/details/132074922 gitlab部署 使用docker-compose.yml version: 3 services: web: image: twang2218/gitlab-ce-zh:10.5 restart: always hostname: 192.168.xx.xx environm…

企业架构LNMP学习笔记19

Nginx 第三方模块的使用&#xff1a; Nginx官方没有的功能&#xff0c;开源开发人员定制开发了一些功能&#xff0c;把代码公布出来&#xff0c;可以通过编译加载第三方模块的方式&#xff0c;使用新功能。 NGINX 3rd Party Modules | NGINX shell > tar xvf ngx-fancyinde…

IDEA中的MySQL数据库所需驱动包的下载和导入方法

文章目录 下载驱动导入方法 下载驱动 MySQL数据库驱动文件下载方法&#xff1a; 最新版的MySQL版本的驱动获取方法&#xff0c;这个超链接是下载介绍的博客 除最新版以外的MySQL版本的驱动获取方法&#xff0c;选择Platform Independent&#xff0c;选择第二个zip压缩包虾藻…

剑指 Offer 12. 矩阵中的路径

剑指 Offer 12. 矩阵中的路径 推荐写法 把判断条件都写在dfs函数开头&#xff08;对节点进行处理&#xff0c;尽量不要对边进行处理&#xff09; 写法一 class Solution {boolean[][] vis;public boolean exist(char[][] board, String word) {int m board.length, n boa…

浅谈redis未授权漏洞

redis未授权漏洞 利用条件 版本比较高的redis需要修改redis的配置文件&#xff0c;将bind前面#注释符去掉&#xff0c;将protected-mode 后面改为no 写入webshell config get dir #查看redis数据库路径 config set dir web路径# #修改靶机Redis数据库路径 config set dbfilen…

QT QTabWidget 控件 使用详解

本文详细的介绍了QTabWidget控件的各种操作&#xff0c;例如&#xff1a;新建界面、设置页面名字、设置提示信息、设置页面激活、设置标题栏位置、设置页面关闭按钮、设置页面关闭按钮、获取页面下标、获取页面总数、清空所有页面、删除某个页面、设置拖拽移动、设置显示页面、…

【Spring容器的启动流程】

Spring容器的启动流程主要分为以下几个步骤&#xff1a; 加载配置文件&#xff1a;Spring容器会从指定的配置文件中读取配置信息&#xff0c;包括bean的定义、依赖关系、AOP切面等。 创建容器&#xff1a;Spring容器启动后会创建一个容器实例&#xff0c;容器负责管理bean的生…

MyBatis-Plus排除不必要的字段

查询学生信息排除年龄列表 &#x1f4da;&#x1f50d; 使用MyBatis-Plus排除某些字段。如果你想要进行查询&#xff0c;但又不需要包含某些字段&#xff0c;那么这个功能将非常适合你。&#x1f50d;&#x1f393;&#x1f4dd; 1. 学生信息查询-排除年龄列表 在使用 MyBat…

【计算机网络】HTTP(下)

本文承接上文的代码进行改造&#xff0c;上文链接&#xff1a;HTTP上 文章目录 1. 实现网站跳转实现 自己的网站跳转 2. 请求方法(get) && 响应方法(post)GET方法POST方法GET与POST的应用场景 3. HTTP状态码在自己设计的代码中发现4043开头的状态码(重定向状态码)永久…

一种基于注意机制的快速、鲁棒的混合气体识别和浓度检测算法,配备了具有双损失函数的递归神经网络

A fast and robust mixture gases identification and concentration detection algorithm based on attention mechanism equipped recurrent neural network with double loss function 摘要 提出一个由注意力机制组成的电子鼻系统。首先采用端到端的编码器译码器&#xff…

容器编排学习(二)镜像制作和私有仓库介绍

一 Dockerfile 1 概述 commit的局限 很容易制作简单的镜像&#xff0c;但碰到复杂的情况就十分不方便例如碰到下面的情况需要设置默认的启动命令需要设置环境变量需要指定镜像开放某些特定的端口 Dockerfile就是解决这些问题的方法 Dockerfile是一种更强大的镜像制作方式…

VMware虚拟机+Centos7 配置静态,动态IP

本章目录 一、查看网关&#xff1a; 编辑–>虚拟网络编辑器二、点击NAT设置三、记住网关IP待会要用四、配置静态ip地址1、进入存放修改IP地址的目录2、修改ip地址的文件3、编辑文件4、文件&#xff08;编辑好后退出&#xff09; 五、重启网络六、测试1、linux上查看IP地址的…

爬虫爬取mp3文件例子

相信训练模型时数据集的获取也是一个很头疼的事情&#xff0c;搞cv领域的可以扛着摄像头架起三脚架拍摄获取&#xff08;以前干过&#xff09;&#xff0c;但是如果是nlp领域的呢&#xff0c;特别是chatgpt等大模型出来后对这类文本等数据的需求更大&#xff0c;如果没有现成的…

【AIGC专题】Stable Diffusion 从入门到企业级实战0403

一、前言 本章是《Stable Diffusion 从入门到企业级实战》系列的第四部分能力进阶篇《Stable Diffusion ControlNet v1.1 图像精准控制》第03节&#xff0c; 利用Stable Diffusion ControlNet Canny模型精准控制图像生成。本部分内容&#xff0c;位于整个Stable Diffusion生态…