【C++基础】实现日期类

news/2024/10/17 13:28:43/

在这里插入图片描述

​👻内容专栏: C/C++编程
🐨本文概括: C++实现日期类。
🐼本文作者: 阿四啊
🐸发布时间:2023.9.7

对于类的成员函数的声明和定义,我们在类和对象上讲到过,需要进行声明和定义分离。

一些需要使用的接口函数声明,我们放入到Date.h文件中
#include <iostream>
using namespace std;class Date
{public://构造函数Date(int year = 1, int month = 1, int day = 1);//拷贝构造函数Date(const Date& d);//析构函数//~Date(); //日期类可以不写//打印日期void Print()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 operator!=(const Date& d) const;//赋值运算符重载Date& operator=(const Date& d);//日期+= 天数Date& operator+=(int day);//日期 + 天数Date operator+(int day) const;//日期 -= 天数Date& operator-=(int day);//日期 - 天数Date operator-(int day) const;//获取当月天数int GetMonthDay(int year, int month) const;//前置++Date& operator++();//后置++Date operator++(int);//前置--Date& operator--();//后置--Date operator--(int);//日期 - 日期 返回天数int operator-(const Date& d) const;private:int _year;int _month;int _day;
};

🤗🤗下面,我们对一些日期接口函数的实现:

实现于Date.cpp文件中

一、构造函数、拷贝构造以及日期的打印

#include "date.h"
//构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;//检查日期是否合法if (month < 1 || month > 12 || day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;//exit(-1);}
}
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}
//Date::~Date()
//{
//	cout << "~Date()" << endl;
//}
void Date::Print() const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

二、赋值运算符重载函数

//赋值运算符重载
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}

三、运算符重载

比较运算符重载

我们写一个operator< 运算符重载函数和一个 operator== 运算符重载函数即可,其他直接复用就行。

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 _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) || (*this == d);
}
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}

日期 ± 天数、日期 - 日期

获取当前月份的天数

首先我们需要写一个获取当月的天数GetMonthDay()函数,以便于后面用日期 ± 天数运算。

//获取当月天数
int Date::GetMonthDay(int year, int month) const
{static int MonthDayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//判断是否为闰年(先判断是否为2月)if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0))){return 29;}return MonthDayArray[month];
}

日期 += 天数 与 日期 + 天数

//日期+= 天数
Date& Date::operator+=(int day)
{if (day < 0){return *this -= -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 tmp(*this);tmp += day;return tmp;
}
日期+ 天数
//Date Date::operator+(int day)
//{
//	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;
//}
//
日期 += 天数
//Date& Date::operator+=(int day)
//{
//	*this = *this + day;
//	return *this;
//}

在这里插入图片描述

日期 -= 天数 与 日期 - 天数

我们知道了先写operator+=,再写operator+直接复用即可这种方法更优,所以我们日期减去天数也是实现operator-=,再实现operator-

//日期 -= 天数
Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}//日期 - 天数
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}

自增 和 自减 重载

C++规定:前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载,后置++重载时多增加了一个int类型的参数,与前置++构成函数重载,以区分前置++

//前置++
Date& Date::operator++()
{*this += 1;//返回++之后的值return *this;
}
//后置++
Date Date::operator++(int)
{Date tmp(*this);//返回++之前的值*this += 1;return tmp;
}//前置--
Date& Date::operator--()
{*this -= 1;return *this;
}
//后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}

日期 - 日期

// d1 - d2
int Date::operator-(const Date& d) const
{//默认认为左边日期大Date max = *this;Date min = d;int flag = 1;//为1返回正数,-1返回负数int count = 0;//统计天数//不成立则交换if (*this < d){max = d;min = *this;flag = -1;}//while(min < max)while (min != max){min++;count++;}return count * flag;
}

四、全局函数实现流插入流提取

date.h 流插入流提取重载函数的声明
/*不能重载成成员函数,否则会导致参数不匹配,因为this指针永远占据第一个位置,无法进行流插入提取操作。*/
//流插入
ostream& operator<<(ostream& out, const Date& d);
//流提取
istream& operator>>(istream& in, Date& d);

如果重载成成员函数,那么成员函数的第一个参数永远是隐藏的this指针,成员函数中只能利用out << _year << _month << _day的顺序,但是在调用时, (d1为日期类对象)d1 << cout,只能这么写,虽然可以,但是很别扭,不符合使用习惯和价值。所以我们需要实现成全局函数才可以。
在这里插入图片描述
但是写成全局函数会访问类的成员变量,我们可以利用友元或者将成员变量封装成成员函数解决。

date.cpp 流插入流提取重载函数的实现
/*不能重载成成员函数,否则会导致参数匹配,因为this指针永远占据第一个位置,无法进行流插入提取操作。*/
//涉及访问私有成员变量可以利用友元,或者将成员变量封装成Get成员函数
//流插入
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;
}

http://www.ppmy.cn/news/1103854.html

相关文章

Golang RabbitMQ实现的延时队列

文章目录 前言一、延时队列与应用场景二、RabbitMQ如何实现延时队列实现延时队列的基本要素整体的实现原理如下 三、Go语言实战生产者消费者 前言 之前做秒杀商城项目的时候使用到了延时队列来解决订单超时问题&#xff0c;本博客就总结一下Golang是如何利用RabbitMQ实现的延时…

2.12 PE结构:实现PE字节注入

本章笔者将介绍一种通过Metasploit生成ShellCode并将其注入到特定PE文件内的Shell注入技术。该技术能够劫持原始PE文件的入口地址&#xff0c;在PE程序运行之前执行ShellCode反弹&#xff0c;执行后挂入后台并继续运行原始程序&#xff0c;实现了一种隐蔽的Shell访问。而我把这…

51单片机的智能台灯控制系统仿真( proteus仿真+程序+原理图+报告+讲解视频)

51单片机的红外光敏检测智能台灯控制系统仿真 1.主要功能&#xff1a;2.仿真3. 程序代码4. 原理图5. 设计报告6. 设计资料内容清单&&下载链接 51单片机的红外光敏检测智能台灯控制系统仿真( proteus仿真程序原理图报告讲解视频&#xff09; 仿真图proteus7.8及以上 程…

AI系统论文阅读:SmartMoE

提出稀疏架构是为了打破具有密集架构的DNN模型中模型大小和计算成本之间的连贯关系的——最著名的MoE。 MoE模型将传统训练模型中的layer换成了多个expert sub-networks&#xff0c;对每个输入&#xff0c;都有一层special gating network 来将其分配到最适合它的expert中&…

celery的用法--bind=True

通过将 bindTrue 设置为 app.task 装饰器的参数&#xff0c;Celery 会自动将任务实例绑定到第一个参数&#xff08;通常命名为 self&#xff09;&#xff0c;使得你可以在任务函数内部访问任务实例的属性和方法。 在 Celery 的任务函数中&#xff0c;self 参数代表任务实例本身…

Mysql树形表的两种查询方案(递归与自连接)

你有没有遇到过这样一种情况&#xff1a; 一张表就实现了一对多的关系&#xff0c;并且表中每一行数据都存在“爷爷-父亲-儿子-…”的联系&#xff0c;这也就是所谓的树形结构 对于这样的表很显然想要通过查询来实现价值绝对是不能只靠select * from table 来实现的&#xff0…

Java——》synchronized的使用

推荐链接&#xff1a; 总结——》【Java】 总结——》【Mysql】 总结——》【Redis】 总结——》【Kafka】 总结——》【Spring】 总结——》【SpringBoot】 总结——》【MyBatis、MyBatis-Plus】 总结——》【Linux】 总结——》【MongoD…

1分钟了解音频、语音数据和自然语言处理的关系

机器学习在日常场景中的应用 音频、语音数据和自然语言处理这三者正在不断促进人工智能技术的发展&#xff0c;人机交互也逐渐渗透进生活的每个角落。在各行各业包括零售业、银行、食品配送服务商&#xff09;的多样互动中&#xff0c;我们都能通过与某种形式的AI&#xff08;…