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

【C++】日期类实现

日期类实现

1、关系运算符重载

>、>=、<、<=、==、!=
如果判断a是否大于b,可以先写判断a小于b和a等于b的函数,其他的复用

主要框架(.h文件)

#pragmaonce#include<assert.h>#include<iostream>using namespace std;class Date{public:// 友元函数声明friend ostream&operator<<(ostream&out,constDate&d);friend istream&operator>>(istream&in,Date&d);intGetMonthDay(intyear,intmonth){assert(month>0&&month<13);staticintmonthDayArray[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)))return29;elsereturnmonthDayArray[month];}// 只要不改变调用对象的函数都建议加constvoidPrint()const;boolCheckDate();Date(intyear=1,intmonth=1,intday=1);Date&operator+=(intday);Date operator+(intday)const;Date&operator-=(intday);Date operator-(intday)const;// ++d1; -> d1.operatpr++();Date&operator++();//d1++; -> d1.operatpr++(1);Date operator++(int);// --d1;Date&operator--();//d1;Date operator--(int);bool operator==(constDate&d)const;bool operator!=(constDate&d)const;bool operator<(constDate&d)const;bool operator<=(constDate&d)const;bool operator>(constDate&d)const;bool operator>=(constDate&d)const;// 日期-日期 难intoperator-(constDate&d)const;/*ostream& operator<<(ostream& out) { out << _year << "年" << _month << "月" << _day << "日" << endl; return out; }*/Date*operator&(){returnthis;// return nullptr;}constDate*operator&()const{returnthis;// return nullptr;}private:int_year;int_month;int_day;};ostream&operator<<(ostream&out,constDate&d);istream&operator>>(istream&in,Date&d);

1.重载<

bool Date::operator<(constDate&d)const{if(_year<d._year){returntrue;}elseif(_year==d._year){if(_month<d._month){returntrue;}elseif(_month==d._month){return_day<d._day;}}returnfalse;}

2.重载==

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

3.重载<=

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

4.重载>

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

5.重载>=

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

6.重载!=

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

2、赋值运算符重载

1.获取某个月的天数

重载+和+=时,涉及日期进位,当日期为这个月的最后一天时,再加一天月份要进一位

intGetMonthDay(intyear,intmonth){assert(month>0&&month<13);staticintmonthDayArray[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)))return29;elsereturnmonthDayArray[month];}
  • 设置一个数组存放每个月的天数,以月份为下标
  • 把数组设置为静态,因为函数会被调用多次,设置成静态,第一次创建之后,一直到程序结束后还在,可以避免函数调用时重复的创建数组
  • 当月份为2时要判断是否是闰年

2.重载+=

用于给出一个已知日期,计算过了若干天后的日期

Date&Date::operator+=(intday)//这里必须是+= d1+100是错误的 d1没改变{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;}

3.重载+

+和+=的方法类似,可以利用+=实现重载+

Date Date::operator+(intday)const{Date tmp=*this;tmp+=day;returntmp;}

4.重载-=

Date&Date::operator-=(intday){if(day<0){return*this+=-day;}_day-=day;while(_day<=0){--_month;if(_month==0){_month=12;_year--;}// 借上⼀个⽉的天数_day+=GetMonthDay(_year,_month);}return*this;//第二种写法//*this=*this-day;//return *this;//拷贝次数多}

5.重载日期-天数

利用-=重载-

Date Date::operator-(intday)const{Date tmp=*this;tmp-=day;returntmp;}

6.重载日期-日期

intDate::operator-(constDate&d)const{Date max=*this;//存放大日期Date min=d;//存放小日期intflag=1;if(*this<d){max=d;min=*this;flag=-1;}//初始化计数器intn=0;while(min!=max){++min;++n;}returnn*flag;}
  • flag为标记符号,默认*this日期≥d日期,结果为正
  • 调用Date类重载的<运算符,判断当前日期(this)是否小于d,若成立,说明d是大日期,*this是小日期,执行大括号内逻辑
  • 把大日期d赋值给max,把小日期*this赋值给min,falg更新符号为-1,最终结果取负,退出if语句
  • 初始化一个计数器,统计从min到max之间的天数差
  • 最终返回值:

若*this≥d:结果为正,flag=1,返回n

若*this<d:结果为负,flag=-1,返回-n

3、重载++、–

1.重载前置++

Date&Date::operator++(){*this+=1;//先把自己加一天return*this;//加完之后的样子}
  • 返回++之后的值

2.重载后置++

Date Date::operator++(int){Datetmp(*this);//保存自己当前的样子*this+=1;//把自己加一天returntmp;//返回保存下来的自己}
  • 返回++之前的值

3.重载前置–

Date&Date::operator--(){*this-=1;return*this;}
  • 返回–之后的值

4.重载后置–

Date Date::operator--(int){Date tmp=*this;*this-=1;returntmp;}
  • 返回–之前的值

4、重载<<、>>

对于自定义类型,编译器不知道如何打印,所以通过<<打印日期类对象

cin是istream类实例化对象,cout是ostream类实例化对象

将<<重载成全局函数

ostream&operator<<(ostream&out,constDate&d){out<<d._year<<"年"<<d._month<<"⽉"<<d._day<<"⽇"<<endl;returnout;}

注意:因为该运算符重载函数写在全局,默认函数内部无法访问日期类的私有成员变量,所以把该函数设置成友元函数

friend ostream&operator<<(ostream&out,constDate&d);

将>>重载成全局函数

istream&operator>>(istream&in,Date&d){cout<<"请依次输入年⽉日:";in>>d._year>>d._month>>d._day;if(!d.CheckDate()){cout<<"⽇期非法"<<endl;}returnin;}

注意:两个参数都不能加const修饰

friend istream&operator>>(istream&in,Date&d);

如果文章中有错误或不足,欢迎大家指正交流。

http://www.jsqmd.com/news/1135505/

相关文章:

  • 内网渗透核心技术解析:从信息收集到横向移动的实战攻防
  • 构建面向多租户环境的LLM API资源聚合平台安全架构
  • 你的电脑为什么越来越慢?82款硬件检测工具一站式解决所有性能问题
  • 3步搞定PHP代码自动化重构:Rector让你告别手动升级的烦恼 [特殊字符]
  • LunaTV终极容器化部署指南:5分钟构建个人影视中心
  • Halcon 标定板像素当量实战:单图计算XY方向精度,误差控制在0.01mm内
  • 使用Metasploit生成Windows后门:从原理到实战控制
  • Minecraft基岩版启动器终极指南:如何免费管理多个游戏版本
  • STM32与IS31FL3731 LED驱动芯片的硬件设计与优化
  • OpenCV 4.8 与 PIL.Image 图像转换:BGR/RGB 通道陷阱与 3 种正确转换方法
  • 如何在3小时内构建企业级知识图谱:R2R完全指南
  • 5分钟搭建终极AI知识库:Open Notebook开源智能笔记完全指南
  • 终极品牌图标解决方案:Simple Icons让开发者效率提升10倍的完整指南
  • Python国密SM4加密实战:pysm4库深度解析与最佳实践
  • 场效应晶体管结构工作原理输出特性传输特性
  • intellij-erlang高级技巧:10个让你事半功倍的使用方法
  • SRC漏洞挖掘——4.安全配置错误漏洞实战详解
  • 5分钟掌握:如何用Better BibTeX插件彻底解决Zotero文献管理难题
  • 工作中Git的使用,基于GitLab,保姆级教程!
  • 软件测试学习笔记
  • iOS开发 SwiftUI 14:ScrollView 滚动视图
  • 实战p5.js:3步构建惊艳的可视化项目与创意编程应用
  • 如何为洛雪音乐配置完美音源:新手3分钟快速上手指南
  • EhViewer开源漫画浏览应用:从零开始的完整使用指南
  • linux的dd命令详解
  • 别被坑了!2026实测靠谱的AI写作辅助平台|省心版
  • 【RHCA+】基本正则表达式
  • 2026年暑期 AI 简历工具推荐:毕业生求职必备
  • 十七.读写区块索引(上)
  • OpenCore Legacy Patcher:一场针对苹果硬件生态的技术革命与架构重构