当前位置: 首页 > news >正文

日期类代码实现-C++

一、目标

        通过前面对类和对象的介绍我们可以自己通过C++代码初步实现一个简单的日期类。

实现的主要操作有:

1.日期类的构造函数

2.日期类的拷贝构造函数(在头文件中实现)

3.日期类的比较运算符重载

4.日期类的计算运算符重载

5.流插入运算符重载。

二、总体思路

        首先,我这里采用的是分文件编程的方式来实现的日期类。

分别为:

1.头文件:Date.h

        该文件的主要目的是对上述目标所要实现的所有操作进行函数的声明。同时,还要包含在

源文件Date.cpp 中定义时所需要用到的头文件函数。

2.源文件:Date.cpp

        该文件的目的主要是用于对 头文件Date.h 所声明的所有函数进行定义,从而完成各个函数所要实现的操作。

3.源文件:Test.cpp

        该文件的目的主要是用于检测 源文件Date.cpp 中定义的函数是否能正常够使用并且达到所要实现的操作。

三、代码实现及具体思路

1.头文件:Date.h

        通过上面思路的介绍,我们可以知道,我们的目的是创建一个日期类,然后在日期类中自我声明:日期类的构造函数、日期类的比较运算符重载、日期类的计算运算符重载以及流插入运算符重载。并实现拷贝构造函数。

注意:通过我们前面对类和对象的介绍可知,因为在实现日期类过程中,我们没有动态申请空间,所以我们只需使用编译器默认生成的析构函数就可以,因此,我们不需要自己再定义一个析构函数)

代码如下:

#pragma once#include <iostream>
#include <assert.h>using namespace std;
class Date
{public:Date(int year = 1, int month = 1, int day = 1);void Print()const{cout << _year << '-' << _month << '-' << _day << endl;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//日期类的比较运算符的重载bool operator<(const Date& x) const;bool operator==(const Date& x) const;bool operator<=(const Date& x) const;bool operator>(const Date& x) const;bool operator>=(const Date& x) const;bool operator!=(const Date& x) const;//日期类的计算运算符的重载int Get_MonthDay(int year,int month);		//获取该月份的天数Date& operator+=(int day);Date operator+(int day)const;Date& operator-=(int day);Date operator-(int day)const;Date& operator++();		//前置++Date operator++(int);		//后置++Date& operator--();			//前置--Date operator--(int);		//后置--int operator-(const Date& x) const;// 流插入不能写成成员函数?// 因为Date对象默认占用第一个参数,就是做了左操作数// 写出来就一定是下面这样子,不符合使用习惯//d1 << cout; // d1.operator<<(cout); //void operator<<(ostream& out);// 友元函数声明friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& x);
istream& operator>>(istream& in, Date& x);

2.源文件:Date.cpp

        对于实现下面函数,我们需要首先定义一个获取当前月份天数的函数

代码实现:

//因为平年闰年的2月天数不一样所以我们需定义一个获取月份的函数来解决这一问题
int Date::Get_MonthDay(int year, int month)		//获取该月份的天数
{static int daysArr[13] = { 0,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;}else{return daysArr[month];}
}

1.日期类的构造函数

 思路:

        我们在通过日期类构造函数初始化所定义的日期时,我们需要判断我们所定义的日期是否合法,若不合法,则需要返回并提示

代码实现:

Date::Date(int year, int month, int day)
{//判断所初始化的日期是否合法if (month > 0 && month < 13&&day>=1&&day<= Get_MonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "非法日期" << endl;assert(false);}
}

2.日期类的比较运算符重载

思路:

        对于比较类的运算符重载来说,因为比较运算符具有互斥性,所以我们只需定义出 < 运算符重载和 == 运算符重载,然后其他运算符复用上面所定定义的两个运算符即可。并且在比较的同时,我们不会改变参数的值,所以我们可以在函数尾部加const用于修饰内部的this指针,这样的话,const修饰的类型我们也可以通过比较运算符来比较。

代码实现:

bool Date::operator<(const Date& x) const
{if (_year < x._year){return true;}else if (_year == x._year && _month < x._month){return true;}else if (_year == x._year && _month == x._month && _day < x._day){return true;}else{return false;}
}bool Date::operator==(const Date& x) const
{if (_year == x._year && _month == x._month && _day == x._day){return true;}else{return false;}
}bool Date::operator<=(const Date& x) const
{//复用上面定义的重载运算符:< , ==return *this < x || *this == x;
}bool Date::operator>(const Date& x) const
{//复用上面定义的重载运算符:<=return !(*this <= x);
}bool Date::operator>=(const Date& x) const
{//复用上面定义的重载运算符:> , ==return *this > x || *this == x;
}bool Date::operator!=(const Date& x) const
{//复用上面定义的重载运算符:==return !(*this == x);
}

3.日期类的计算运算符重载

思路:

        对于日期类的计算运算符重载的定义,我们可以先定义 += 运算符重载和 -= 运算符重载,然后其他运算符重载的定义我们可以复用这两个运算符重载,从而实现各个运算符所要实现的目的。

代码实现:

Date& Date::operator+=(int day)
{if (day < 0){return *this -= (-day);}_day = _day + day;while (_day > Get_MonthDay(_year, _month)){_day = _day-Get_MonthDay(_year, _month);++_month;if (_month == 13){_month = 1;++_year;}}return *this;
}Date Date::operator+(int day)const
{if (day < 0){return *this - (-day);}//复用上面定义的重载运算符:+=Date tem(*this);tem+= day;return tem;
}Date& Date::operator-=(int day)
{if (day < 0){return *this += (-day);}_day = _day - day;while (_day < 1){--_month;if (_month <1 ){_month = 12;--_year;}_day = _day + Get_MonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)const
{if (day < 0){return *this + (-day);}//复用上面定义的重载运算符:-=Date tem(*this);tem -= day;return tem;
}Date& Date::operator++()
{//复用上面定义的重载运算符:+=*this += 1;return *this;
}Date Date::operator++(int)
{//复用上面定义的重载运算符:+Date tem = *this;*this += 1;return tem;
}Date& Date::operator--()
{//复用上面定义的重载运算符:+=*this -= 1;return *this;
}Date Date::operator--(int)
{//复用上面定义的重载运算符:+Date tem = *this;*this -= 1;return tem;
}

        实现两个日期之间相减求天数时,我们可以先判断哪个日期大,从而确定出所求的天数是正数还是负数,即用flage的正负来实现。接着我们定义一个n来统计天数,然后我们通过while循环,++最小的日期,并且++天数直到最小日期和最大日期相等的时候结束,这时候我们返回n*flage的值即是所求天数。

int Date::operator-(const Date& x) const
{Date max = *this;Date min = x;int flage = 1;if (*this < x){max = x;min = *this;flage = -1;}int n = 0;while (min != max){++min;++n;}return n * flage;
}

4.流插入运算符重载。

思路:

        对于日期类使用系统中的流插入(只能插入内置类型)时并不能实现所期望的操作,因为日期类是自定义类型,所以我们就需要自己定义一个流插入来实现这个操作

	// 流插入不能写成成员函数?// 因为Date对象默认占用第一个参数,就是做了左操作数// 写出来就一定是下面这样子,不符合使用习惯//d1 << cout; // d1.operator<<(cout); //void operator<<(ostream& out);

因此,这里我们通过友元函数,在全局中定义流插入的运算符重载

代码实现:

ostream& operator<<(ostream& out, const Date& x)
{out << x._year << "年" << x._month << "月" << x._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& x)
{int year, month, day;in >> year >> month >> day;if (month > 0 && month < 13&& day > 0 && day <= x.Get_MonthDay(year, month)){x._year = year;x._month = month;x._day = day;}else{cout << "非法日期" << endl;assert(false);}return in;
}

3.源文件:Test.cpp

Test1:

用于检测日期类的比较运算符的重载

void Test1()		//用于检测日期类的比较运算符的重载
{Date s1(2005, 2, 16);Date s2(2024, 8, 12);cout << "bool operator<(const Date& x) const:" << (s1 < s2) << endl;cout << "bool operator==(const Date& x) const:" << (s1 == s2) << endl;cout << "bool operator<=(const Date& x) const:" << (s1 <= s2) << endl;cout << "bool operator>(const Date& x) const:" << (s1 > s2) << endl;cout << "bool operator>=(const Date& x) const:" << (s1 >= s2) << endl;cout << "bool operator!=(const Date& x) const:" << (s1 != s2) << endl;
}

Test2:

用于检测日期类的计算运算符的重载:+= , +

void Test2()		//用于检测日期类的计算运算符的重载:+= , +
{Date s1(2005, 2, 16);s1.Print();s1 += 10000;s1.Print();Date s2(2005, 2, 16);s2.Print();Date s3=s2+10000;s3.Print();
}

Test3:

用于检测日期类的计算运算符的重载:-= , -

void Test3()		//用于检测日期类的计算运算符的重载:-= , -
{Date s1(2005, 2, 16);s1.Print();s1 -= 1000;s1.Print();Date s2(2005, 2, 16);s2.Print();Date s3 = s2 - 1000;s3.Print();
}

Test4:

用于检测日期类的计算运算符的重载:--

void Test4()		//用于检测日期类的计算运算符的重载:--
{Date s2(2005, 2, 16);Date s3(2005, 2, 16);Date s4;s4=s2--;s4.Print();s4 = --s3;s4.Print();
}

Test5:

用于检测日期类的计算运算符的重载:++

void Test5()		//用于检测日期类的计算运算符的重载:++
{Date s2(2005, 2, 16);Date s3(2005, 2, 16);Date s4;s4 = s2++;s4.Print();s4 = ++s3;s4.Print();
}

Test6:

用于检测日期类之间的计算运算符的重载:-

void Test6()		//用于检测日期类之间的计算运算符的重载:-
{Date s1(2005, 2, 16);Date s2(2024, 8, 13);cout << "int operator-(const Date& x) const:" << (s1 - s2) << endl;
}

Test7:

用于检测日期类之间的计算运算符的重载:<< , >>

void Test7()		//用于检测日期类之间的计算运算符的重载:<< , >>
{Date s1;cin >> s1;cout << s1 << endl;cout << s1 + 100 << endl;
}

四、结语:

         上述内容,即是我个人对C++日期类的个人见解及代码实现。若有大佬发现哪里有问题可以私信或评论指教一下。非常感谢各位uu们的点赞,关注,收藏,还望各位多多关照,让我们一起进步吧!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • windows关闭英语美式键盘
  • conda配置国内源
  • Python | Leetcode Python题解之第334题递增的三元子序列
  • c语言中比较特殊的输入函数
  • 基于Mediepipe的手势识别系统 | OpenCV | Mediapipe | C++ | QT | Python | C# | Unity
  • Doris与StarRocks
  • linux网络配置脚本
  • 《机器学习by周志华》学习笔记-决策树-04多变量决策树
  • 【重学c++primer】第五章第二节 深入浅出:左值和右值
  • LabVIEW VI 多语言动态加载与运行的实现
  • Cesium天空盒子(Skybox)制作(js代码)和显示
  • C语言中的函数sscanf()用法
  • Golang基于DTM的分布式事务TCC实战
  • Golang | Leetcode Golang题解之第343题整数拆分
  • 16.2 TensorFlow 与 Keras 基础
  • hexo+github搭建个人博客
  • [数据结构]链表的实现在PHP中
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • IndexedDB
  • Mysql5.6主从复制
  • PHP 7 修改了什么呢 -- 2
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • React-flux杂记
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Vue UI框架库开发介绍
  • 猴子数据域名防封接口降低小说被封的风险
  • 一个项目push到多个远程Git仓库
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • ​Redis 实现计数器和限速器的
  • # Redis 入门到精通(九)-- 主从复制(1)
  • #宝哥教你#查看jquery绑定的事件函数
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (39)STM32——FLASH闪存
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (代码示例)使用setTimeout来延迟加载JS脚本文件
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (四)汇编语言——简单程序
  • (四)进入MySQL 【事务】
  • (限时免费)震惊!流落人间的haproxy宝典被找到了!一切玄妙尽在此处!
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (转)Windows2003安全设置/维护
  • ****Linux下Mysql的安装和配置
  • .apk文件,IIS不支持下载解决
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .NET 动态调用WebService + WSE + UsernameToken
  • .net 获取url的方法
  • .NET 中创建支持集合初始化器的类型
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET大文件上传知识整理
  • .net开发时的诡异问题,button的onclick事件无效
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题