C++类和对象(5)——运算符重载(以日期类为例)

news2024/9/25 5:23:09

运算符重载的作用

假设我们此时实现了日期类的运算符重载,我们就可以

实现如图的很多功能,完成日期计算器的底层代码。

运算符重载关键字

运算符重载的关键字是operator。

比如你想重载‘+’运算符,那么语法格式就是

返回类型 + operator + ‘+’ +(形参),

以日期类为例,

Date operator+(int day)const;

有const关键字是因为这个重载不修改对象本身(*this)的值,如下图的d1不被改变。

Date d3 = d1 + 100;

以下运算符不能重载:

1.     ?:

2.     sizeof

3.     

4.      :: 

5.      .*

日期类的运算符重载

以下是日期类的声明,待会逐一实现运算符重载。

#pragma once
#include<iostream>
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);

	friend istream& operator>>(istream& out, Date& d);

public:

	Date(int year = 1990, int month = 1, int day = 1);

	~Date();

	int GetMonthDay(int year, int month)
	{
		static int a[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if ((month == 2) && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))
		{
			return 29;
		}
		return a[month];
	}

	Date(const Date& d);

	void print()const;

	Date& operator=(const Date& d);

	Date& operator+=(int day);

	Date operator+(int day)const;

	Date& operator-=(int day);

	Date operator-(int day)const;

	Date& operator++();

	Date operator++(int);

	Date& operator--();

	Date operator--(int);

	bool operator>(const Date& d)const;

	bool operator==(const Date& d)const;

	bool operator >= (const Date& d)const;

	bool operator < (const Date& d)const;

	bool operator <= (const Date& d)const;

	bool operator != (const Date& d)const;

	int operator-(const Date& d)const;

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out, const Date& d);

istream& operator>>(istream& out, Date& d);

构造、拷贝构造、析构函数的实现(非重载内容可跳过)

我以前写过两篇博客介绍构造、拷贝构造、析构函数,感兴趣的朋友可以看看。

http://t.csdnimg.cn/DedK1

http://t.csdnimg.cn/LWuj2

Date::Date(int year, int month, int day) :
	_year(year),
	_month(month),
	_day(day)
{}

Date::~Date() {}

Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

重载 =

Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

我们运用的场景如下:

Date d1(2000, 1, 1);
Date d2(2001, 2, 2);
d1 = d2;

当我们重载=时,将d2赋值给d1,改变了d1的值,所以重载=的返回类型为 Date& ,返回*this

重载+=

要实现日期+天数的功能,我们要先编写一个函数GetMonthDay,这个函数可以直接写在类的声明里。

int GetMonthDay(int year, int month)
{
	static int a[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
	if ((month == 2) && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))
	{
		return 29;
	}
	return a[month];
}

接着实现+=的重载

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}

通过while循环实现天数的正确叠加、月份和年份的增加。

注意返回类型是Date&,因为

Date d1(2000, 1, 1);
d1 += d1 + 100;

d1重载+=时,d1的值会被改变 !

重载+

这一步我们可以通过写过的的+=重载偷懒😎

Date Date::operator+(int day)const
{
	Date temp = *this;
//+=已经重载过了,可以直接用
	temp += day;
	return temp;
}

注意返回类型是Date,因为

Date d3 = d1 + 100;

调用的时候是d1重载+,d1的值没有被改变。

重载-=(计算这个日期前x天是几号)

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

重载-

Date Date::operator-(int day)const
{
	Date temp = *this;
	temp -= day;
	return temp;
}

重载前置++

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

这里也用+=重载偷懒了😉

重载后置++

Date Date::operator++(int)
{
	Date temp = *this;
	++*this;
	return temp;
}

注意,为了区分后置++与前置++,后置++的传参有一个int形参!

并且,后置++的返回值为Date, 前置++的返回值为Date&;

因为:

如上图所示,

d2的值与d1的原始值相等 ,

当d1++用过一次之后,d1的值才会+1.

这也就解释了为什么后置++的返回值为Date, 前置++的返回值为Date&;

后置++需要temp变量存放*this的值。

重载前置--

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

用-=偷懒。。

重载后置--

Date Date::operator--(int)
{
	Date temp = *this;
	--*this;
	return temp;
}

重载>

bool Date::operator>(const Date& d)const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}

重载==

bool Date::operator==(const Date& d)const
{
	return (_year == d._year) && (_month == d._month) && (_day == d._day);
}

重载>=

bool Date::operator >= (const Date& d)const
{
	return (*this > d) || (*this == d);
}

重载<

bool Date::operator < (const Date& d)const
{
	return !(*this >= d);
}

重载<=

bool Date::operator <= (const Date& d)const
{
	return !(*this > d);
}

重载!=

bool Date::operator != (const Date& d)const
{
	return !(*this == d);
}

重载<<

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << '-' << d._month << '-' << d._day << endl;
	return out;
}

这不是Date类的成员函数,而是全局函数。

为了能访问私有成员_year,_month,_day,

我们要把这个函数变成友元函数,如下:

重载>>

//这里的形参Date& d的前面不能添加const关键字,因为d的值待会要改变
istream& operator>>(istream& in, Date& d)
{
	cout << "" << endl;
	in >> d._year >> d._month >> d._day;
	return in;
}

重载-(计算两个时间之间差几天)

int Date::operator-(const Date& d)const
{
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
	}
	int ret = 0;
	while (min != max)
	{
		++min;
		++ret;
	}
	return ret;
}

我的另一篇博客讲了详细的实现思路http://t.csdnimg.cn/gk7cK

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

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

相关文章

Linux驱动开发基础(sr04超声波模块)

所学来自百问网 目录 1. SR04 超声波简介 2. 硬件设计 3. 软件设计 4. 示例代码 4.1 驱动代码 4.1.1 轮询模式 4.1.2 中断模式 4.3 应用程序 4.4 Makefile 4.5 实验效果 1. SR04 超声波简介 超声波测距模块是利用超声波来测距。模块先发送超声波&#xff0c;然后接…

大数据技术概述

4v特点 volume&#xff08;体量大&#xff09; velocity&#xff08;处理速度快&#xff09; variety&#xff08;数据类型多&#xff09; value&#xff08;价值密度低&#xff09; 核心设计理念 并行化 规模经济 虚拟化 分布式系统满足需求 系统架构 大数据处理流程 采集…

找论文的方法:如何找到本领域研究方向所需要的论文进行泛读和精读?

1、参考其他研究者给出的该领域的reading lists&#xff1a; 例如&#xff0c;在异配图神经网络领域&#xff1a; Awesome Resource on Graph Neural Networks With Heterophily&#xff1a;https://github.com/alexfanjn/Graph-Neural-Networks-With-Heterophily 在图对抗攻…

快速掌握GPTEngineer:用AI创建网页应用的实用教程

今天来聊聊一个非常有趣的工具——GPTEngineer。这是一个基于AI的网页开发平台&#xff0c;特别适合那些不熟悉编程但又想快速创建网页应用的人。如果你想用简单的文本描述来生成一个网站或者应用&#xff0c;GPTEngineer可能就是你需要的。我们一步步看看如何使用它。 1. 了解…

Guava Cache实现原理及最佳实践

本文内容包括Guava Cache的使用、核心机制的讲解、核心源代码的分析以及最佳实践的说明。 概要 Guava Cache是一款非常优秀本地缓存&#xff0c;使用起来非常灵活&#xff0c;功能也十分强大。Guava Cache说简单点就是一个支持LRU的ConcurrentHashMap&#xff0c;并提供了基于…

Java面试宝典-java基础08

Java面试宝典-java基础08 71、BIO、NIO、AIO有哪些应用场景72、简述一下BIO的编程流程73、NIO的三大核心部分是什么&#xff1f;74、NIO中buffer的四大属性是什么&#xff1f;75、对比一下BIO和NIO&#xff1f;76、FileChannel是做什么的&#xff1f;77、简述一下Selector选择器…

51单片机-矩阵键盘(基于LC602)

时间&#xff1a;2024.8.30 作者&#xff1a;Whappy 目的&#xff1a;手撕51&#xff08;第二遍&#xff09; 代码&#xff1a; main.c #include <REGX52.H> #include "LCD1602.h" #include "Delay.h" #include "MatrixKey.h"unsigned…

【Canvas与艺术】录王昌龄诗《从军行之四》

【成图】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>出塞青海长云暗雪山</title><style type"text/css&quo…

opencv实战项目十六:kmeans图像颜色聚类:

文章目录 前言K-means介绍效果 前言 在数字化时代&#xff0c;图像处理技术已成为计算机视觉领域的重要组成部分。其中&#xff0c;图像颜色聚类作为一项关键技术在众多应用场景中发挥着重要作用&#xff0c;如图像分割、物体识别、色彩调整等。K-means算法作为一种经典的聚类…

Java性能优化传奇之旅--Java万亿级性能优化之电商平台高峰时段性能大作战:策略与趋势洞察

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

Redis基础知识学习(入门篇)

文章目录 五大数据结构一. String: 字符串二. Hash: 散列概念性质 三. List: 列表四. Set: 集合特点 五. Sorted Set: 有序集合 五大数据结构 一. String: 字符串 数据结构中&#xff0c;字符串要单独用一种存储结构来存储&#xff0c;称为串存储结构。这里的串指的就是字符串…

性能分析之使用 Jvisualvm dump 分析示例

一、前言 在 JMeter 入门系列中相信大家对工具使用已经没问题&#xff0c;今天开起性能测试进阶系列之 jvisualvm 工具简单学习&#xff0c;目标是通过演示 Jvisualvm 工具定位代码&#xff0c;帮助性能测试工程师直接定位代码位置&#xff0c;协助开发解决性能问题&#xff1…

Flink CDC MySQL数据同步到Doris表同步配置生成工具类

工具类 生成的配置 要同步表为&#xff1a; customer_user.tb_business_user_info express.route_push_service 请提前自行到doris中建好目标数据库&#xff0c;如果没有会报错 同步的配置文件如下&#xff1a;&#xff08;将配置内容保存为xxx.yaml文件到flink cdc提交任务&…

昇腾 Ascend 概念澄清 Host、Device、AI core、AI CPU、DVPP、AIPP、AscendCL、AscendC

昇腾 Ascend 概念澄清 Host、Device、AI core、AI CPU、DVPP、AIPP、AscendCL、AscendC flyfish Ascend C开发算子&#xff0c;偏低。 AscendCL开发应用&#xff0c;偏高。 AI core、AI CPU、DVPP都属于计算资源。 Ascend C开发的算子运行在AI Core上。 AIPP用于在AI Core上完…

TimeWheel算法介绍及在应用上的探索

作者&#xff1a;来自 vivo 互联网服务器团队- Li Fan 本文从追溯时间轮算法的出现&#xff0c;介绍了时间轮算法未出现前&#xff0c;基于队列的定时任务实现&#xff0c;以及基于队列的定时任务实现所存在的缺陷。接着我们介绍了时间轮算法的算法思想及其数据结构&#xff0c…

手撕数据结构与算法——拓扑排序

拓扑排序是图论中的一个重要概念&#xff0c;它在许多领域如任务调度、课程规划等都有广泛的应用。在这篇文章中&#xff0c;我们将探讨拓扑排序的基本概念、算法实现以及在C/C中的实现方法。 拓扑排序简介 拓扑排序是针对有向无环图&#xff08;DAG&#xff09;的一种排序算法…

二叉树(数据结构)

1.两种特殊的二叉树 1. 满二叉树 : 一棵二叉树&#xff0c;如果 每层的结点数都达到最大值&#xff0c;则这棵二叉树就是满二叉树 。也就是说&#xff0c; 如果一棵 二叉树的层数为 K &#xff0c;且结点总数是2^k-1 &#xff0c;则它就是满二叉树 。 2. 完全二叉树 : 完…

为你的LLM应用增加记忆能力

1. 记忆系统的重要性 我们都知道&#xff0c;大模型本身是无状态、无记忆的。默认情况下&#xff0c;我们向大模型发起的每次提问&#xff0c;在其内部都会被视为一次全新的调用。尽管诸如 ChatGPT 等聊天应用内置了部分记忆功能&#xff0c;可以记录用户最近几轮的聊天信息&a…

ChatTTS容器构建教程

一、模型介绍 ChatTTS 是专门为对话场景设计的文本转语音模型&#xff0c;例如 LLM 助手对话任务。它支持英文和中文两种语言。最大的模型使用了 10 万小时以上的中英文数据进行训练。在 HuggingFace 中开源的版本为 4 万小时训练且未 SFT 的版本。 ChatTTS WebUI如下&#x…

【单片机原理及应用】实验:LED循环控制

目录 一、实验目的 二、实验内容 三、实验步骤 四、记录与处理 五、思考 六、成果文件提取链接 一、实验目的 熟悉Proteus x8原理图与C51程序的联合仿真调试方法&#xff0c;掌握C51延时函数和循环控制的方法 二、实验内容 【参照图表】 &#xff08;1&#xff09;创建一…