【C++】实现日期类相关接口

news2024/9/28 3:30:24

在这里插入图片描述

C++语法相关知识点可以通过点击以下链接进行学习一起加油!
命名空间缺省参数与函数重载C++相关特性类和对象-上篇类和对象-中篇
类和对象-下篇

本篇将介绍实现日期类中众多接口,同时这其中涉及到很多知识,务必将类和对象相关内容掌握再来实现日期类相关接口。

请添加图片描述
Alt
🌈个人主页:是店小二呀
🌈C语言笔记专栏:C语言笔记
🌈C++笔记专栏: C++笔记
🌈初阶数据结构笔记专栏: 初阶数据结构笔记
🌈Linux笔记专栏: Linux笔记

🌈喜欢的诗句:无人扶我青云志 我自踏雪至山巅 请添加图片描述

文章目录

  • 一、天数加法
  • 二、获得某年某月的天数
  • 三、比较两个日期
  • 四、日期加天数
    • 4.1 先实现operator+还是operator+=
  • 五、日期减日期
  • 六、流插入和流提取运算符重载
    • 6.1 需要判断输入进去的数据是否有误
  • 七、源代码展示
    • 7.1 Date.h
    • 7.2 Date.cpp
    • 7.3 test.cpp

如果你想让这个函数是内敛,可以在类里面定义的函数,默认是内敛,内敛不支持声明和定义分离。接下来我们将展开相关接口的实现逻辑。

一、天数加法

//前置++
Date& operator++()
	{
		_day += 1;
		return *this;
	}	
//后置++
Date operator++(int)
	{
		Date temp(*this);
		_day += 1;
		return temp;
	}
int main()
{
	Date d;
	Date d1(2022, 1, 13);
	d = d1++; // d: 2022,1,13 d1:2022,1,14
	d = ++d1; // d: 2022,1,15 d1:2022,1,15
	return 0;
}
  • 前置++:返回对象+1后结果,由于this指针指向的对象,函数结束后不会销毁,故而使用引用方式返回,提高效率。
  • 后置++;既要完成对象+1操作,又要返回旧值的对象。这里可以使用拷贝构造一份this。对于temp属于临时对象,出了函数作用域就会销毁,因此只能传值返回,不能返回引用
  • 侧面反映了this虽然不能在参数部分显式写,但是可以在函数体内使用,这样子设计是有需求的。

二、获得某年某月的天数

关于计算日期,最频繁调用就是获得某年某月的天数接口,对此可以单独使用该接口。由于表示年月日的成员对象都在日期类中封装起来,类外部不能随便访问类成员,只能在类中实现GetMonthDay函数,在通过return将获得的天数返回。

实现逻辑(涉及到很多历史发展背景,这里局限于现代的日期计算):

int Date::GetMonthDay(int year, int month) const
{
	assert(month > 0 && month < 13);//保证月数合法
	static int montharr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return 29;
	else
		return montharr[month];
}

实现上一些细节:

  • 这里由于自转和公转问题,当是闰年时,二月的天数加一
  • 还有一些细节上的问题(但是CPU跑太快,没啥影响),static int montharr属于静态变量,只能定义一次。对此频繁调用时,不用多次定义。
  • 在判断语句中,可以将位置进行调正,这里跟&&短路知识点有关,如果前面是假,不同接下去判断,整个表达式都为假

三、比较两个日期

这里需要涉及到运算符重载,这里有个小技巧,只需要实现大于等于或小于等于的接口。

函数声明

	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;

实现小于等于(剩下可取反)

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);//这里会调用的
}

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 (_year == d._year && _month == d._month && _day < d._day)
		return true;
	else
		return false;
}

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);
}

四、日期加天数

这里需要考虑持续进位原则

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);//频繁调用,不用考虑其中细节问题
			++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day) const
{
	Date temp(*this);//拷贝构造
	temp += day;
	return temp;
}
  • 当日期加完天数后,通过日期的规则需要按照进位原则,对年月日数据进行调正
  • 在实现operator+=/+,都可以间接实现operator+/+=
  • 这里operator+=使用日期加天数,提高了效率和避免传值返回中的拷贝过程
  • operator+这里不能使用引用返回,这里是创建了一个临时变量,调用完会销毁

4.1 先实现operator+还是operator+=

  • 先实现operator+=,再间接实现operator+呢?
  • 还是先实现operator+,再间接实现operator+=呢?

在这里插入图片描述

对此得出结论:推荐先实现operator+=,再间接实现operator+。

理由:

  • 将代码贴出来,方便进行对比。这里不能交叉对比,需要采用横向对比。
  • 两者实现operator+是等价,但是实现operator+=,左边需要复用+operator,相比之下多次拷贝构造。
  • 这不详细介绍关于operator-和operator-=。大体逻辑跟operator+和operator+有异曲同工之处(具体在Date.cpp看看)

五、日期减日期

需要使用持续借位原则,如果天数为0,需要得到上月的天数

第一种方式:不断++直到等于大的那个年份,这里需要日期加日期接口的技术支撑

int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (*this < d)
	{
		int flag = -1;//判断两个天数相差
		max = d;
		min = *this;
	}

	int n = 0;
	while (min != max)
	{
		++min;//这里会调用operato++()
		++n;//operator++()
	}

	return n * flag;
}
  • 优点:方便实现
  • 缺点:效率低

第二个方法:通过该变量记录两个年份修饰到1月1日,也就是都修饰到1月1日之间还差多少天,再计算两个年之间有多少个年,如果是平年+365,闰年+366

int Date::operator-(const Date& d) const
{
	//不知道哪个操作数大,先假设
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)//假设错了就认错
	{
		Date max = d;
		Date min = *this;
		int flag = -1;//用来标记
	}
	int count =0;
	//大的减到1月1日  count++
	while (!(max._day == 1 && max._month == 1))
	{
		--max;
		++count;
	}
	//小的减到1月1日  count--
	while (!(min._day == 1 && min._month == 1))
	{
		--min;
		--count;
	}
	//都减到1月1日了  算差多少年
	while (min._year != max._year)
	{
		if (is_leapyear(min._year))
			count += 366;
		else
			count += 365;
		++min._year;
	}
	return flag * count;
}

六、流插入和流提取运算符重载

out和cin的本质是输入和输出流对象,对于<<和>>用于重载的运算符,从图可以得,cout属于ostream类,cin属于istream类,可以自动识别类型

对于我们可以在日期类中,实现<<和>>重载打印日期和提取日

int main()
{
	Date d1(2024, 3, 10);
    //void operator<<(ostream& out);
    //cout<<d1;
    
	d1 << cout;//->d1.operator<<(cout)->operator<<(const Date *d1,cout);
	return 0;
}
  • 如果使用运算符重载,隐含的this指针占用第一个参数的位置,Date必须是左操作数,d1<<cout是不符合我们的习惯的
  • 对此我们可以在类外实现该运算符重载函数,就可以自己设计参数部分的位置
  • 但是又引出了另一个问题:类外不能访问类中的私有成员,如果将私有权限放开,就缺乏安全性,对此C++中有友元,接下来我们会涉及到,这里就使用下,虽然这个全局函数不在类中,但是可以访问私有成员函数
//友元,告诉该类这两个全局函数是我们的朋友,允许使用私有成员(在类中)
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "" << d._month << "" << d._day << "" << endl;
	return out;
}
 
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

如果我们需要连续打印cout<<d1<<d2的话,这里就不合适的。因为这里的结合性是从左往右,cout<<d1会返回一个临时变量,那么这里运算符重载函数需要通过引用返回了。C++存在私有的,printf不支持自定义打印,cout本质实现所用类型的打印。

- **用引用做返回值,应对连续流插入和流提取
- 流提取不是不能对Date进行const修饰,需要通过键盘读取数据存储在成员变量

6.1 需要判断输入进去的数据是否有误

在这里插入图片描述

七、源代码展示

7.1 Date.h

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	bool operator<(const Date& d);
	bool operator<=(const Date& d);
	bool operator>(const Date& d);
	bool operator>=(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);

	// d1 + 100
	Date& operator+=(int day);
	Date operator+(int day);
	// d1 - 100
	Date operator-(int day);
	Date& operator-=(int day);

	// ++d1
	Date& operator++();
	// 特殊处理:解决语法逻辑不自洽,自相矛盾的问题
	// d1++
	// 为了跟前置++区分,强行增加一个int形参,够成重载区分
	Date operator++(int);

	Date operator--(int);
	Date& operator--();

	// d1 - d2
	int operator-(const Date& d);

	// 本质就是inline
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		// 365   自转  公转  365 5+h
		// 366
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}

		return monthDays[month];
	}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

7.2 Date.cpp

#include"Date.h"

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

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

	return false;
}
// d1 <= d2
bool Date::operator<=(const Date& d)
{
	return *this < d || *this == d;
}

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

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

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

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

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

	return *this;
}

Date Date::operator+(int day)
{
	//Date tmp(*this);
	Date tmp = *this; // 
	tmp += day;

	return tmp;
}

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

Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;

	return tmp;
}

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

// ++d ->d.operator++()
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

// d++ ->d.operator++(0)
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

// d1 - d2
int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		int flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;
}

7.3 test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

int main()
{
	Date d1(2024, 1, 29);
	Date d2 = d1 + 20;
	d2.Print();
	d1.Print();

	d2 -= 20;
	d2.Print();

	d1 += 30000;
	d1.Print();

	++d1;
	d1.operator++();
	d1.Print();

	d1++;
	d1.operator++(10);
	d1.Print();

	/*bool ret = false;
	if (ret)
	{
		d1.Print();
	}*/

	Date d4(2024, 1, 29);
	Date d5(2024, 8, 1);
	cout << d5 - d4 << endl;

	return 0;
}

以上就是本篇文章的所有内容,在此感谢大家的观看!这里是店小二呀C++笔记,希望对你在学习C++语言旅途中有所帮助!
请添加图片描述

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

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

相关文章

【数据结构】详细介绍栈和队列,解析栈和队列每一处细节

目录 一. 栈 1. 栈的概念 2. 栈的实现 2.1 栈的结构 2.2 初始化栈 2.3 入栈 2.4 出栈 2.5 获取栈顶元素 2.6 获取栈中有效个数 2.7 判断栈是否为空 2.8 销毁栈 二. 队列 1. 队列的概念 2. 队列的实现 2.1 队列的结构 2.2 队列初始化 2.3 销毁队列 2.4 入…

聊聊适配器模式

目录 适配器模式概念 主要实现方式 主要组成 UML用例图 代码示例 生活场景 应用场景 适配器模式概念 适配器模式属于结构型设计模式&#xff0c;它的主要目的是将一个类的接口转换成客户端所期望的另一种接口形式&#xff0c;使得原本接口不兼容的类可以一起工作。 主…

【New SQL】 -- CockroachDB license change

1、CockroachDB 发布了修改开源协议的 releases 北京时间 2024-08-16 &#xff0c;CockroachDB 发布了修改开源协议的 releases。 原文链接&#xff1a;Licensing FAQs Evolving our self-hosted offering and license model CockroachDB License change (again) | Product T…

Kali Linux 定制化魔改 添加常见60渗透工具

项目地址&#xff1a;https://github.com/CuriousLearnerDev/kali-linux-kde-beautify 系统版本&#xff1a;kali linux 2024.1 固件类型&#xff1a;BIOS 用户: zss 密码: ss 完整版 系统压缩大小&#xff1a;18.8 GB 解出来&#xff1a;36.00GB 左右 系统版 系统压缩大小&…

《Cloud Native Data Center Networking》(云原生数据中心网络设计)读书笔记 -- 04路由协议的选择

本章要回答的问题&#xff1a; 路由是如何工作的?有哪些类型的路由协议?Clos 拓扑中的路由协议是如何工作的?什么是无编号接口&#xff0c;以及为什么无编号接口如此重要?如何确定最适合自己需求的路由协议? 路由概述 用最简单的话来说&#xff0c;路由是使用数据包的目…

DESeq2差异基因分析和批次效应移除

差异基因鉴定 基因表达标准化 不同样品的测序量会有差异&#xff0c;最简单的标准化方式是计算counts per million (CPM)&#xff0c;即原始reads count除以总reads数乘以1,000,000。 这种计算方式的缺点是容易受到极高表达且在不同样品中存在差异表达的基因的影响&#xff…

FunClip,音视频识别,自动化剪辑,文本校对,智能纠错,导出SRT

今天给大家介绍一个自动化剪辑项目——FunClip,该项目是由阿里开源的&#xff0c;可以识别音频、视频中的文字&#xff0c;一键剪辑和添加字幕。 FunClip是一款高效的自动化视频剪辑工具&#xff0c;它利用语音识别技术对视频内容进行分析。用户可以在识别结果中选择所需的文本…

重生奇迹MU 梦幻骑士 真正生而高贵的职业

作为重生奇迹MU梦幻骑士中真正生而高贵的职业&#xff0c;圣骑士是玩家们最为追捧的职业之一。在游戏内&#xff0c;圣骑士拥有着强大的防御和治疗能力&#xff0c;成为团队中不可或缺的存在。如果你正准备选择一个强大的职业&#xff0c;那么不妨考虑成为一名圣骑士&#xff0…

选择文件摆渡系统要遵守的“三要”和“三不要”原则

文件摆渡系统不仅可以实现企业网络隔离后的数据摆渡需求&#xff0c;同时也可以视作企业数据安全及网络安全建设的重要组成部分。文件摆渡系统的选择也很关键&#xff0c;在企业进行筛选时&#xff0c;应该遵守“三要”和“三不要”原则。 “三要”之一&#xff1a;要安全 文件…

MySQL对事务的支持

5.MySQL对事务的支持情况&#xff1a; 5.1. 查看存储引擎对事务的支持情况 &#xff1a; 1.SHOW ENGINES 命令来查看当前 MySQL 支持的存储引擎都有哪些&#xff0c;以及这些存储引擎是否支持事务2.下图可以能看出在 MySQL 中&#xff0c;只有InnoDB类型的存储引擎是支持事务…

轻松搞定 Java7 新特性,示例丰富

Java 7 是 Java 语言的一个主要版本&#xff0c;于 2011 年 7 月 28 日正式发布。 由 Sun Microsystems 开发并由 Oracle 公司发布的一个重要版本&#xff0c;它带来了许多新功能特性&#xff0c;增强了编程语言的能力和性能。 以下是一些 Java 7 的主要新功能特性&#xff1…

基于SSM的体育馆预约管理系统---附源码84196

摘 要 体育馆作为一个重要的运动场所&#xff0c;需要进行预约管理以保证资源的合理利用和场馆秩序的维护。传统的人工预约管理方式存在效率低、容易出错等问题&#xff0c;因此&#xff0c;在互联网高速发展的当下&#xff0c;需要设计和实现一个基于SSM的体育馆预约管理系统&…

关于LLC知识9

1、上图有多条增益曲线&#xff0c;是每种不同的输出负载都对应一条增益曲线&#xff0c;在f1时候每个曲线都汇聚一点&#xff0c;说明只要输出电压频率f1时&#xff0c;不论输出负载是多少&#xff0c;增益是一样的&#xff0c;不受负载影响G1&#xff08;Lr与Cr发生谐振&…

[单master节点k8s部署]21.EFK日志收集平台介绍

大型系统是一个分布式部署的架构&#xff0c;不同的服务模块部署在不同的服务器上&#xff0c;问题出现时&#xff0c;大部分情 况需要根据问题暴露的关键信息&#xff0c;定位到具体的服务器和服务模块&#xff0c;构建一套集中式日志系统&#xff0c;可以提高 定位问题的效率…

构建自己的语音助手

在我最近发布关于如何构建自己的 RAG 并在本地运行它的帖子之后。今天&#xff0c;我们更进一步&#xff0c;不仅实现了大型语言模型的对话能力&#xff0c;还增加了听力和口语能力。这个想法很简单&#xff1a;我们将创建一个语音助手&#xff0c;让人想起标志性钢铁侠电影中的…

基于asp.net的办公协同管理系统源码分享

今天给大家分享一个asp.net开发的webform框架的办公协同管理系统源码SQLserver数据库 1.主要功能 这个项目是帮助一个学生指导的毕业设计&#xff0c;包含用户登陆、用户管理、车辆 管理、文件管理、个人中心、后台管理、文件上传、人事管理、系统日志等 等模块。2.开发工具及…

【Django-vue-admin学习笔记】页面自动计算日期差额的方法

在许多应用场景中,尤其是在管理系统中,经常需要对日期进行动态计算和展示,以帮助用户了解关键日期的即时状态。例如,在学生宿舍管理系统中,显示学生的退宿倒计时可以帮助管理人员有效监控即将到期的宿舍安排,并及时进行必要的调整。这样的功能不仅提高了管理效率,也增加…

H5接入企微JS-SDK,使用wx.previewFile进行文件预览

最近上项目&#xff0c;需求是做一个附件预览并且可以进行保存到手机、用其他应用打开的需求的需求&#xff0c;用企微的JS-SDK的wx.previewFile就可以满足以上的需求了 详细的可以参考&#xff1a;企业微信官方文档 前端 1、在项目的index.html中添加&#xff1a;jweixin-1.2…

两种用MATLAB绘制色块的方法

绘制色块首先可以想到用填充像素的方式 % 定义图像的尺寸 imageSize 500;% 创建一个 imageSize x imageSize x 3 的矩阵&#xff0c;每个像素都是绿色 % RGB颜色模型中绿色的值为 [0, 1, 0] greenImage zeros(imageSize, imageSize, 3); greenImage(:, :, 2) ones(imageSiz…

Kubeadm快速安装 Kubernetes集群

Kubernetes的基础概念 Kubernetes&#xff08;通常简称为K8s&#xff09;是一个开源的容器编排系统&#xff0c;用于自动化部署、扩展和管理容器化应用程序。Kubernetes 提供了强大的抽象能力&#xff0c;使得开发者能够专注于应用程序的逻辑&#xff0c;而无需担心底层容器的…