C++使用类和对象实现日期类Date

2024-09-03 05:52

本文主要是介绍C++使用类和对象实现日期类Date,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

个人主页:C++忠实粉丝
欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 C++忠实粉丝 原创

C++使用类和对象实现日期类Date

收录于专栏【C++语法基础
本专栏旨在分享学习C++的一点学习笔记,欢迎大家在评论区交流讨论💌

目录

1.创建日期类Date

 2.日期类相关函数的实现

1.获取某年某月的天数

2.全缺省的构造函数

3.拷贝构造函数

4.检查天数是否合法

5.赋值运算符重载

6.析构函数

7.日期+=天数

8.日期+天数

9.日期-=天数

10.日期-天数

11.前置++

12.后置++

13.后置--

14.前置--

15.<运算符重载

16.==运算符重载

17.>=运算符重载

18.>运算符重载

19.<=运算符重载

20.!=运算符重载

21.日期-日期 返回天数

22.输入流 

23.输出流

3.测试代码

测试1

​编辑

 测试2

 测试3

 测试4

4.取地址运算符重载 

5.总结 

6.代码参考


1.创建日期类Date

一个基本的日期类需要有构造函数,拷贝构造函数,析构函数,相关运算符重载,以及成员变量

class Date
{
public:// 获取某年某月的天数int GetMonthDay(int year, int month);// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数// d2(d1)Date(const Date& d);// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d);//检查天数是否合法bool CheckDate() const;// 析构函数~Date();// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day);// 日期-天数Date operator-(int day);// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// >运算符重载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);// 日期-日期 返回天数int operator-(const Date& d);private:int _year;int _month;int _day;
};

注意:

我们可以将获取天数的函数写在日期类内部(这样的函数会频繁使用),这样就默认为类的内联函数,在函数调用时直接展开,来增加日期类的运行速度,将其他函数的声明和定义分开,增加我们代码的可读性!!! 

内联函数的具体内容可以参考这篇博客----C++基础入门(下)-CSDN博客 

 2.日期类相关函数的实现

1.获取某年某月的天数

因为每月的天数除了2月份在变化,其他天的天数都是不变的,我们就可以用一个静态数组来存贮,单独分析2月份的情况.

	int GetMonthDay(int year, int month) const{assert(month > 0 && month < 13);static int monthDayArray[13] = { -1, 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;}return monthDayArray[month];}

断言检查

assert(month > 0 && month < 13); 确保传入的月份在1到12之间。这是有效的,保证了函数不会处理无效的月份。
月份天数数组

static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
该数组存储每个月的天数,其中索引从1到12对应月份1到12,索引 0 的值为 - 1表示无效的日期。static 关键字确保该数组在第一次调用时初始化,之后调用不会重新初始化,节省了开销。
闰年处理

if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
检查是否为2月(即闰月),并根据闰年的规则返回 29 天。如果是闰年,2 月返回 29 天,否则返回 28 天。
返回天数

return monthDayArray[month];
对于非2月的情况,从数组中返回天数。

2.全缺省的构造函数

Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;if (!CheckDate()){cout << "非法日期:";Print();}
}

注意:

我们写构造函数时,可以加入判断日期是否合法 

3.拷贝构造函数

// 拷贝构造函数
// d2(d1)Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}

4.检查天数是否合法

bool Date::CheckDate() const
{if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month)){return false;}else{return true;}
}

5.赋值运算符重载

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

6.析构函数

注意:如果类中没有申请资源时,析构函数可以不写直接使⽤编译器⽣成的默认析构函数,如Date;如果默认⽣成的析构就可以⽤,也就不需要显⽰写析构,如MyQueue;但是有资源申请时,⼀定要⾃⼰写析构,否则会造成资源泄漏,如Stack。

析构函数具体内容可以看--C++类和对象(中)1-CSDN博客 

7.日期+=天数

// d1 += 100
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为负数,我们可以转换为-=(-day),交给-=运算符重载来实现,这样能缩减我们的代码,提高代码的运行速率! 

8.日期+天数

// d1 + 100
Date Date::operator+(int day) const
{Date tmp = *this;tmp += day;return tmp;
}

直接调用之前我们实现的+=运算符重载来实现 

9.日期-=天数

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

和+=类似,同样,如果day<0转换为+=(-day),交给+=运算符重载来实现

10.日期-天数

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

直接调用-=来实现 

11.前置++

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

前缀自增 ++d1: 直接修改对象,返回自增后的对象的引用。

12.后置++

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

后缀自增 d1++: 创建一个对象副本保存原始状态,修改当前对象,返回原始对象副本。 

13.后置--

// 后置--
Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}

14.前置--

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

15.<运算符重载

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

16.==运算符重载

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

17.>=运算符重载

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

18.>运算符重载

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

19.<=运算符重载

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

20.!=运算符重载

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

21.日期-日期 返回天数

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

注意:

这里使用了flag来判断相差的天数的正负 

因为我们这里已经将前置++重载完毕,后面的日期转换我们这里不需要考虑!!!

22.输入流 

istream& operator>>(istream& in, Date& d)
{while (1){cout << "请依次输入年月日:>";in >> d._year >> d._month >> d._day;if (!d.CheckDate()){cout << "输入日期非法:";d.Print();cout << "请重新输入!!!" << endl;}else{break;}}return in;
}

23.输出流

ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}

3.测试代码

测试1

void TestDate1()
{Date d1(2024, 7, 12);Date d2 = d1 + 100;//Date d3(d1 + 100);d1.Print();d2.Print();//d1 += 100;//d1.Print();d1 += 30000;d1.Print();
}

测试我们的构造函数,拷贝构造函数,打印函数,以及相关运算符重载 

 测试2

void TestDate2()
{/*Date d1(2024, 7, 13);d1 -= 30000;d1.Print();*/Date d1(2024, 7, 13);Date ret1 = d1++;ret1.Print();d1.Print();Date d2(2024, 7, 13);Date ret2 = ++d2;ret2.Print();d2.Print();
}

测试前后置++是否正常 

 

 测试3

void TestDate4()
{Date d1(2034, 10, 1);Date d2(2024, 6, 31);cout << d1 - d2 << endl;
}

测试两个日期相减 

 测试4

void TestDate5()
{Date d1, d2;cin >> d1 >> d2;cout << d1 << d2;cout << d1 - d2 << endl;
}

 测试输入输出流

 

4.取地址运算符重载 

正常情况下, 我们不需要取地址运算符重载.

取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,⼀般这两个函数编译器⾃动⽣成的就可以够我们⽤了,不需要去显⽰实现。除⾮⼀些很特殊的场景,⽐如我们不想让别⼈取到当前类对象的地址,就可以⾃⼰实现⼀份,胡乱返回⼀个地址,让对方程序崩溃!!!

const Date* operator&() const
{//return this;return nullptr;//return (Date*)0x2673FE30;
}

测试:

void TestDate6()
{const Date d1(2024, 7, 13);d1.Print();Date d2(2024, 7, 13);d2.Print();cout << &d1 << endl;cout << &d2 << endl;
}

5.总结 

日期类可以很好的帮助我们梳理C++中类和对象的一些知识,并将知识转换成实际的应用,很有实践意义!!!

后面我也会持续更新C++中STL中的string,感兴趣的宝子们点赞关注不迷路哦!!!

大家也可以关注我的C++专栏[C++语法学习]后续的文章将会持续更新到里面,欢迎 点赞👍 收藏✨ 留言✉ 加关注💓

6.代码参考

#include<iostream>
#include<assert.h>using namespace std;class Date
{// 友元friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:bool CheckDate() const;Date(int year = 1900, int month = 1, int day = 1);void Print() const;// 内联函数int GetMonthDay(int year, int month) const{assert(month > 0 && month < 13);static int monthDayArray[13] = { -1, 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;}return monthDayArray[month];}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+(int day) const;Date& operator+=(int day);Date operator-(int day) const;Date& operator-=(int day);// d1++;// d1.operator++(0);Date operator++(int);// ++d1;// d1.operator++();Date& operator++();// d1--;// d1.operator--(0);Date operator--(int);// --d1;// d1.operator--();Date& operator--();// d1 - d2int operator-(const Date& d) const;//void operator<<(ostream& out);//Date* operator&()//{//	//return this;//	//return nullptr;//	//return (Date*)0x2673FF40;//}const Date* operator&() const{//return this;return nullptr;//return (Date*)0x2673FE30;}// 拷贝构造函数// d2(d1)Date(const Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

这篇关于C++使用类和对象实现日期类Date的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132209

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方