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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import