[C++核心编程](七):类和对象——运算符重载*

目录

四则运算符重载

左移运算符重载

递增运算符重载

赋值运算符重载

关系运算符重载

函数调用运算符重载


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

四则运算符重载

        对自定义数据类型实现四则运算(加减乘除)操作,比如:自己写成员函数,实现两个对象相加属性后返回新的对象

        注意:四则运算符重载也可以进行函数重载。

        总结:

        1.对于内置的数据类型的表达式的的运算符是不可以改变的

        2.不要滥用运算符重载

#include <iostream>

using namespace std;

class Person
{
public:
	int m_a;
	int m_b;

	Person(int a, int b) :m_a(a), m_b(b)
	{

	}
	//成员函数重载
	Person operator+(const Person &p)
	{
		Person rp(0, 0);
		rp.m_a = this->m_a + p.m_a;
		rp.m_b = this->m_b + p.m_b;
		return rp;
	}
};
//全局函数重载
static Person operator-(const Person& p1, const Person& p2)
{
	Person rp(0, 0);
	rp.m_a = p1.m_a - p2.m_a;
	rp.m_b = p1.m_b - p2.m_b;
	return rp;
}
//函数重载
static Person operator-(const Person& p1, const int value)
{
	Person rp(0, 0);
	rp.m_a = p1.m_a - value;
	rp.m_b = p1.m_b - value;
	return rp;
}

int main(void)
{
	Person p1(10, 10);
	Person p2(10, 10);
	Person p3(5, 5);
	
	Person p4 = p1 + p2; //Person p4 = p1.operator+(p2);
	cout << "p4 m_a value:" << p4.m_a << endl;
	cout << "p4 m_b value:" << p4.m_b << endl;

	Person p5 = p4 - p3; //Person p5 = operator-(p4,p3)
	cout << "p5 m_a value:" << p5.m_a << endl;
	cout << "p5 m_b value:" << p5.m_b << endl;

	Person p6 = p5 - 5; //Person p6 = operator-(p5,5)
	cout << "p6 m_a value:" << p6.m_a << endl;
	cout << "p6 m_b value:" << p6.m_b << endl;
	system("pause");

	return 0;
}

左移运算符重载

        输出自定义的数据类型,比如:直接输出一个对象就可以输出其成员属性

        总结:左移运算符配合友元可以实现输出自定义数据类型

#include <iostream>
#include <string>

using namespace std;

class Person
{
	friend static ostream& operator<<(ostream& cout, Person& p);
public:
	Person(int a, int b)
	{
		m_a = a;
		m_b = b;
	}
private:
	//通常不使用 成员函数重载 左移运算符 ,无法实现cout在左侧
	int m_a;
	int m_b;
};

static ostream& operator<<(ostream &cout, Person& p) // operator<<(cout, p)
{
	cout << "m_a=" << p.m_a << endl;
	cout << "m_b=" << p.m_b << endl;
	return cout;
}

static void test(void)
{
	Person p(10, 10);

	cout << p << endl;
}

int main(void)
{
	test();
	system("pause");
	return 0;
}

递增运算符重载

        区分前置和后置的差别!!

#include <iostream>
#include <string>

using namespace std;

class MyInteger
{
	friend static ostream& operator<<(ostream& cout, MyInteger p);
public:
	MyInteger(int b)
	{
		m_b = b;
	}
	//重载 后置,int 占位参数 int ,区分前置和后置
	MyInteger operator++(int)
	{
		MyInteger temp = *this;
		++*this;
		return temp;
	}
	//重载 前置
	MyInteger& operator++() //返回引用是为了一直对一个数据进行操作
	{
		++m_b;
		return *this;
	}
private:
	int m_b;
};

static ostream& operator<<(ostream& cout, MyInteger p) // operator<<(cout, p)
{
	cout << p.m_b;
	return cout;
}

static void test(void)
{
	MyInteger p(0);

	cout << ++p << endl;
	cout << p << endl;
}

static void test1(void)
{
	MyInteger p1(0);

	cout << p1++ << endl;
	cout << p1 << endl;
}

int main(void)
{
	test();
	test1();
	system("pause");
	return 0;
}

赋值运算符重载

         c++至少给一个类添加4个函数:前三个略

        4.赋值运算符operator=,对属性进行值拷贝(注意深浅拷贝问题)

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
	Person(int b)
	{
		m_age = new int(b);
	}
	~Person()
	{
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
	}
	Person& operator=(const Person & p) //重载赋值运算符,使用深拷贝
	{
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
		m_age = new int(*p.m_age);
		return *this;
	}

	int *m_age;
};

static void test1(void)
{
	Person p1(12);
	Person p2(19);
	Person p3(14);
	p3 = p2 = p1;

	cout << "p1年龄为:" << *p1.m_age << endl;
	cout << "p2年龄为:" << *p2.m_age << endl;
	cout << "p3年龄为:" << *p3.m_age << endl;
}

int main(void)
{
	test1();
	system("pause");
	return 0;
}

关系运算符重载

        重载关系运算符,让两个自定义的数据类型进行对比操作

#include <iostream>
#include <string>

using namespace std;

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;
		}
		else 
		{
			return false;
		}
	}

	string m_name;
	int m_Age;
};

static void test1(void)
{
	Person p1("Tom", 19);
	Person p2("Mac", 19);

	if (p1 == p2)
	{
		cout << "p1 == p2" << endl;
	}
	else
	{
		cout << "p1 != p2" << endl;
	}

}

int main(void)
{
	test1();
	system("pause");
	return 0;
}

函数调用运算符重载

  • 函数调用运算符() 也可以重载
  • 由于重载后使用的方式非常像函数的调用,故称仿函数
  • 仿函数没有固定写法,非常灵活(返回值、参数均不固定)
#include <iostream>
#include <string>

using namespace std;

class Myprint
{
public:
	void operator()(string test)
	{
		cout << test << endl;
	}
};

class MyAdd
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};

static void test1(void)
{
	Myprint myprint;

	myprint("Hello World!");//仿函数

	MyAdd myadd;
	int result = myadd(1, 1);
	cout << "result = " << result << endl;

	//匿名函数对象
	cout << "result = " << MyAdd()(11, 11) << endl;
}

int main(void)
{
	test1();
	system("pause");
	return 0;
}

推荐:[C++核心编程](五):类和对象——友元(friend)

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

碰撞的小球(Colliding balls)

效果如下&#xff1a; 代码: #include <bits/stdc.h> #include <graphics.h>//必须库 #include <time.h> using namespace std; int main() {initgraph(650,400);//背景图大小circle(100,100,40);fillcircle(200,200,10);//球的数据srand(time(NULL));int …

Leetcoder Day37| 动态规划part04 背包问题

01背包理论基础 面试掌握01背包&#xff0c;完全背包和重背包就够用了。 背包问题的理论基础重中之重是01背包&#xff0c;一定要理解透&#xff01; 01 背包 有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i]&#xff0c;得到的价值是value[i] 。每件物品…

[Redis]——Redis命令手册set、list、sortedset

&#x1f333;List类型常见命令 LPUSH / RPUSH [KEY] [element] …… 向列表左侧或者右侧插入一个或多个元素 LPOP / RPOP [key] 删除左边或者右边第一个元素 LRANGE [key] start end 返回索引start到end的元素&#xff08;索引从0开始&#xff09; BLPOP / BRPOP [key] [等…

Flink 定义 Temporal Table 的两种方式:Temporal Table DDL 和 Temporal Table Function

博主历时三年精心创作的《大数据平台架构与原型实现&#xff1a;数据中台建设实战》一书现已由知名IT图书品牌电子工业出版社博文视点出版发行&#xff0c;点击《重磅推荐&#xff1a;建大数据平台太难了&#xff01;给我发个工程原型吧&#xff01;》了解图书详情&#xff0c;…

小程序环形进度条爬坑

在做微信小程序的时候&#xff0c;发现用canvas做的环形进度条&#xff0c;在带滚动条的view里面显示有闪动、显示不全的问题&#xff0c;后面改成echart-weixin的pie图实现了&#xff0c;option配置如下 // 表示进度的百分比 var progressValue 70;option {series: [{type: …

GC机制以及Golang的GC机制详解

要了解Golang的GC机制,就需要了解什么事GC,以及GC有哪几种实现方式 一.什么是GC 当一个电脑上的动态内存不再需要时&#xff0c;就应该予以释放&#xff0c;以让出内存&#xff0c;这种内存资源管理&#xff0c;称为垃圾回收&#xff08;Garbage Collection&#xff09;&#x…

黑马点评-短信登录业务

原理 模型如下 nginx nginx基于七层模型走的事HTTP协议&#xff0c;可以实现基于Lua直接绕开tomcat访问redis&#xff0c;也可以作为静态资源服务器&#xff0c;轻松扛下上万并发&#xff0c; 负载均衡到下游tomcat服务器&#xff0c;打散流量。 我们都知道一台4核8G的tomca…

RH850P1X芯片学习笔记-Generic Timer Module -ATOM

文章目录 ARU-connected Timer Output Module (ATOM)OverviewGLOBAL CHANNEL CONTROL BLOCK ATOM Channel architectureATOM Channel modesSOMP-Signal Output Mode PWMSOMP - ARUSOMC-Signal Output Mode CompareSOMC - ARUSOMC – COMPARE COMMANDSOMC – OUTPUT ACTIONATOM …

智慧城市中的公共服务创新:让城市生活更便捷

目录 一、引言 二、智慧城市公共服务创新的实践 1、智慧交通系统 2、智慧医疗服务 3、智慧教育系统 4、智慧能源管理 三、智慧城市公共服务创新的挑战 四、智慧城市公共服务创新的前景 五、结论 一、引言 随着信息技术的迅猛发展&#xff0c;智慧城市已成为现代城市发…

failed to connect to ‘127.0.0.1:58526‘: Connection refused

WSA使用体验 链接&#xff1a; 知乎-穿越时间一步到位&#xff0c;教你完美安装Windows 11 Android 安卓子系统 CPU不满足要求 明明是12700H&#xff0c;满足要求&#xff0c;但是应用商店说不满足&#xff0c;在设置&#xff08;注意不是控制面板的区域&#xff09;把地区改…

第二天 Kubernetes落地实践之旅

第二天 Kubernetes落地实践之旅 本章学习kubernetes的架构及工作流程&#xff0c;重点介绍如何使用Workload管理业务应用的生命周期&#xff0c;实现服务不中断的滚动更新&#xff0c;通过服务发现和集群内负载均衡来实现集群内部的服务间访问&#xff0c;并通过ingress实现外…

RabbitMQ队列

RabbitMQ队列 1、死信的概念 ​ 先从概念解释上搞清楚这个定义&#xff0c;死信&#xff0c;顾名思义就是无法被消费的消息&#xff0c;字面意思可以这样理解&#xff0c;一般来说,producer将消息投递到broker或者直接到queue里了&#xff0c;consumer 从 queue取出消息进行消…

浅析虚函数的vptr和虚函数表

浅析虚函数的vptr和虚函数表 文章目录 浅析虚函数的vptr和虚函数表前言1. 基础理论2. 实现与内部结构 前言 ​ 为了实现虚函数&#xff0c;C使用一种称为虚拟表的特殊形式的后期绑定。该虚拟表是用于解决在动态/后期绑定方式的函数调用函数的查找表。虚拟表有时会使用其他名称…

【STM32+HAL】七针OLED(SSD1306)配置(SPI版)

一、前言 关于四针OLED的I2C版配置方式&#xff0c;请转至【STM32HAL】OLED显示初始化配置 二、实现功能&#xff1a; 用SPI通信方式初始化OLED显示&#xff08;相较于I2C速度更快&#xff09; 三、方法一&#xff1a;硬件SPI通信 1、打开SPI通信&#xff08;仅传输&#xf…

互联网加竞赛 车位识别车道线检测 - python opencv

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习 机器视觉 车位识别车道线检测 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f947;学长这里给一个题目综合评分(每项满分5分) …

苍穹外卖Day05——总结5

前期文章 文章标题地址苍穹外卖Day01——总结1https://lushimeng.blog.csdn.net/article/details/135466359苍穹外卖Day01——解决总结1中存在的问题https://lushimeng.blog.csdn.net/article/details/135473412苍穹外卖Day02——总结2https://lushimeng.blog.csdn.net/articl…

STM32-SPI通信协议

串行外设接口SPI&#xff08;Serial Peripheral Interface&#xff09;是由Motorola公司开发的一种通用数据总线。 在某些芯片上&#xff0c;SPI接口可以配置为支持SPI协议或者支持I2S音频协议。 SPI接口默认工作在SPI方式&#xff0c;可以通过软件把功能从SPI模式切换…

【计算机网络】HTTPS 协议原理

https 一、HTTPS 是什么二、加密1. 加密概念2. 加密的原因3. 常见的加密方式&#xff08;1&#xff09;对称加密&#xff08;2&#xff09;非对称加密 三、数据摘要(数据指纹)四、HTTPS 的工作原理探究1. 只使用对称加密2. 只使用非对称加密3. 双方都使用非对称加密4. 非对称加…

java 实现图片新增水印(动态计算水印背景 + 水印文字),附带文字乱码解决方案

文章目录 概要实现流程代码如下小结 概要 图片增加水印背景以及水印文字&#xff0c;根据文字内容是否换行&#xff0c;以及文字行高大小自适应计算背景大小 结果图如下&#xff1a; 实现流程 定义图片来源&#xff0c;以及读取字体来源(防止中文乱码)计算文字所需高度 与…

云上攻防-云原生篇KubernetesK8s安全APIKubelet未授权访问容器执行

知识点 1、云原生-K8s安全-名词架构&各攻击点 2、云原生-K8s安全-Kubelet未授权访问 3、云原生-K8s安全-API Server未授权访问 章节点&#xff1a; 云场景攻防&#xff1a;公有云&#xff0c;私有云&#xff0c;混合云&#xff0c;虚拟化集群&#xff0c;云桌面等 云厂商…
最新文章