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

相关文章

mysql递归查询语法WITH RECURSIVE的使用

《mysql递归查询语法WITHRECURSIVE的使用》本文主要介绍了mysql递归查询语法WITHRECURSIVE的使用,WITHRECURSIVE用于执行递归查询,特别适合处理层级结构或递归... 目录基本语法结构:关键部分解析:递归查询的工作流程:示例:员工与经理的层级关系解释:示例:树形结构的数

Redis中RedisSearch使用及应用场景

《Redis中RedisSearch使用及应用场景》RedisSearch是一个强大的全文搜索和索引模块,可以为Redis添加高效的搜索功能,下面就来介绍一下RedisSearch使用及应用场景,感兴... 目录1. RedisSearch的基本概念2. RedisSearch的核心功能(1) 创建索引(2

Redis中HyperLogLog的使用小结

《Redis中HyperLogLog的使用小结》Redis的HyperLogLog是一种概率性数据结构,用于统计唯一元素的数量(基数),本文主要介绍了Redis中HyperLogLog的使用小结,感兴... 目录 一、HyperlogLog 是什么?️ 二、使用方法1. 添加数据2. 查询基数China编程3.

Linux系统调试之ltrace工具使用与调试过程

《Linux系统调试之ltrace工具使用与调试过程》:本文主要介绍Linux系统调试之ltrace工具使用与调试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、ltrace 定义与作用二、ltrace 工作原理1. 劫持进程的 PLT/GOT 表2. 重定

Python实现剪贴板历史管理器

《Python实现剪贴板历史管理器》在日常工作和编程中,剪贴板是我们使用最频繁的功能之一,本文将介绍如何使用Python和PyQt5开发一个功能强大的剪贴板历史管理器,感兴趣的可以了解下... 目录一、概述:为什么需要剪贴板历史管理二、功能特性全解析2.1 核心功能2.2 增强功能三、效果展示3.1 主界面

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

springboot实现配置文件关键信息加解密

《springboot实现配置文件关键信息加解密》在项目配置文件中常常会配置如数据库连接信息,redis连接信息等,连接密码明文配置在配置文件中会很不安全,所以本文就来聊聊如何使用springboot... 目录前言方案实践1、第一种方案2、第二种方案前言在项目配置文件中常常会配置如数据库连接信息、Red

Python+Tkinter实现Windows Hosts文件编辑管理工具

《Python+Tkinter实现WindowsHosts文件编辑管理工具》在日常开发和网络调试或科学上网场景中,Hosts文件修改是每个开发者都绕不开的必修课,本文将完整解析一个基于Python... 目录一、前言:为什么我们需要专业的Hosts管理工具二、工具核心功能全景图2.1 基础功能模块2.2 进