C++实现日期类(Date)
更新时间:2020年4月25日 17:27 点击:2009
本文实例为大家分享了C++实现日期类的具体代码,供大家参考,具体内容如下
#include<iostream> #include<stdlib.h> using namespace std; class Date { public: //构造函数 Date(int year = 1900, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { if (!IsInvalidDate(_year, _month, _day)) { _year = 1900; _month = 1; _day = 1; } } //拷贝函数 Date(const Date& d) : _year(d._year) , _month(d._month) , _day(d._day) {} //析构函数 ~Date() {} //判断是不是闰年 bool IsLeapYear(int year) { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ) { return true; } return false; } //判断是不是合法日期 bool IsInvalidDate(int year, int month, int day) { if ((year < 1) || (month < 0 || month >12) || (day < 0 || day > YearsOfMonth(year, month))) { return false; } return true; } //判断当前月份多少天 int YearsOfMonth(int year, int month) { int day; int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; day = days[month]; if (month == 2 && IsLeapYear(year)) { day += 1; } return day; } //修正日期 Date ToCorrect(Date &d) { while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0) { if(d._day <= 0) { d._day += YearsOfMonth(d._year,( d._month - 1)); if (d._month == 1) { d._month = 12; d._year--; } else { d._month--; } } else { d._day -= YearsOfMonth(d._year, d._month); if (d._month == 12) { d._year++; d._month = 1; } else { d._month++; } } } return d; } // 当前日期days天后是什么日期? Date operator+(int days) { Date tmp(*this); tmp._day += days; ToCorrect(tmp); return tmp; } // 当前日期days天前是什么日期? Date operator-(int days) { Date tmp(*this); tmp._day -= days; ToCorrect(tmp); return tmp; } // 日期比大小 bool operator>(const Date& d) { return ( _year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)); } bool operator<(const Date& d) { return (_year < d._year || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day)); } bool operator==(const Date& d) { return ((_year == d._year) && (_month == d._month) && (_day == d._day)); } bool operator!=(const Date& d) { return !(*this == d); } bool operator>=(const Date &d) { return !(*this<d); } bool operator<=(const Date &d) { return !(*this>d); } // 重载取地址符号 Date* operator&() { } // 前置++ Date& operator++() { (*this)++; return *this; } // 后置++ Date operator++(int)//通过返回值和传参区别前置和后置++ { Date tmp(*this); (*this) = (*this) + 1; return tmp; } // 前置-- Date& operator--() { (*this)--; return *this; } // 后置-- Date operator--(int) { Date tmp(*this); (*this)--; return tmp; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; int main() { Date d(2018, 9, 9); d.Display(); Date d1 = d + 50; d1.Display(); d1 =d1 - 50; d1.Display(); cout << "------"<<endl; cout << "前置++" << endl; d1.Display(); (++d1).Display(); d1.Display(); cout << "后置++" << endl; d1.Display(); (d1++).Display(); d1.Display(); cout << "------" << endl; system("pause"); return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求。该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自...2015-10-30
MySQL中SELECT+UPDATE处理并发更新问题解决方案分享
问题背景: 假设MySQL数据库有一张会员表vip_member(InnoDB表),结构如下: 当一个会员想续买会员(只能续买1个月、3个月或6个月)时,必须满足以下业务要求: •如果end_at早于当前时间,则设置start_at为当前时间,end_at为当前时...2014-05-31- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求。该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自...2015-10-30
解决Mybatis中mapper.xml文件update,delete及insert返回值问题
这篇文章主要介绍了解决Mybatis中mapper.xml文件update,delete及insert返回值问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-23- 这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 这篇文章主要介绍了Mybatis执行update失败的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-01
- 虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
- 这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25