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

CPP入门:日期类的构建

目录

1.日期类的成员

2.日期类的成员函数

2.1构造和析构函数

2.2检查日期合法

 2.3日期的打印

 2.4操作符重载

2.4.1小于号

2.4.2等于号

2.4.3小于等于号

 2.4.4大于号

2.4.5大于等于号

2.4.6不等号

 2.4.7加等的实现

2.4.8加的实现

2.4.9减去一个天数的减等实现

2.4.10减去一个天数的减实现

2.4.11两个日期相减的实现

 2.4.12前后置++的实现

2.4.13前后置--的实现

 2.5流插入/流提取操作符


1.日期类的成员

实现一个日期类,内部的成员自然是年、月、日

class Date
{private:int _year;int _month;int _day;
};

在日期类中,我们应当是已知每个月份有多少天的,因此我们还需要在日期内中写一个成员函数来获得当月的天数。

	//获得天数int GetMonthDay(int year,int month){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;}else{return monthDayArray[month];}}

此外,我们的日期类还应当能够实现对日期的打印、对日期类的相关计算、输入输出的重载等成员函数。

因此,我们完整的日期类应是如下:

#pragma once
#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public://流插入or输出friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);//构造Date(int year = 1900, int month = 1, int day = 1);//获取月份天数int GetMonthDay(int year, int month){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;}else{return monthDayArray[month];}}//检查日期bool CheckDate();//打印日期void Print() 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;bool operator!=(const Date& d) const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);Date operator-(int day) const;int operator-(const Date& d) const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);
private:int _year;int _month;int _day;
};
//输入流重载
ostream& operator<<(ostream& out, const Date& d);
//输出流重载
istream& operator>>(istream& in, Date& d);

2.日期类的成员函数

2.1构造和析构函数

由于日期类的成员都是内置类型,因此我们可以显式的写一个构造函数,但不用显式定义析构函数。

Date::Date(int year = 2024, int month = 7, int day = 2)
{_year = year;_month = month;_day = day;//检查日期合法性if (!CheckDate()){cout << "日期非法" << endl;}
}

由于我们的日期具有范围,因此我们需要在构造函数中检查日期是否合法。也正是因此,我们需要实现一个检查日期合法性的函数。

2.2检查日期合法

检查日期合法,首先要确保我们输入的数是正数,

//检查日期合法性
bool Date::CheckDate()
{if (_month < 1 || _month>12|| _day<1 || _day>GetMonthDay(_year, _month)||_year < 1){return false;}else{return true;}
}

 2.3日期的打印

void Date::Print() const 
{cout << _year <<'-' << _month <<'-'<<_day;
}

 2.4操作符重载

下面我们就需要重载一些对日期类计算有用的操作符

2.4.1小于号

由于我们不想要我们传入的参数会被修改,因此我们需要传递常量。(权限可以缩小)

另外,我们传引用有两个原因

  • 可以少一次构造形参的过程,可以提高性能。
  • d在函数运行结束后不会销毁,不会返回一个空引用。

因此我们的函数名为:

bool operator<(const Date& d) const

 判断一个日期是否小于另一个日期,我们需要分别判断年、月、日是否小于另一个日期。

在判断日的时候,我们可以直接使用原生的<操作符判断。

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;//if (_day < d._day)//{//	return true;//}//else//{//	return false;//}}}
}

2.4.2等于号

直接判断年月日是否相等即可 

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

2.4.3小于等于号

我们传入的第一个参数是this,因此我们解引用this即可得到第一个参数的值。 

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

 2.4.4大于号

判断是否大于就是是否小于等于取反 

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

2.4.5大于等于号

 判断是否大于等于就是对判断是否小于取反

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

2.4.6不等号

判断是否不相等就是对判等取反

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

 2.4.7加等的实现

对于加等,是给定一个日期和一个天数,计算日期加上这个天数之后的日期。

这里我们采取的思路是先将原天数加上需要加的天数。

之后我们一直减去当月的天数,并让月份加1,如果月份为13,则年份加1,月份赋为1。一直到天数没有当月天数大为止。

Date& Date::operator+=(int day)
{//日期加_day+= day;//月份加while (_day > GetMonthDay(_year, _month)){if (_day > GetMonthDay(_year, _month)){_month++;_day - GetMonthDay(_year, _month);if (_month == 13){_month == 1;_year += 1;}}}return *this;
}

2.4.8加的实现

首先,我们实现两数相加,是不能改变我们的原数的。

因此,我们的第一个形参为const修饰的变量。

//加
//c=a+b;
Date Date::operator+(int day) const
{Date tmp = *this;tmp += day;return tmp;
}

这里我们采取的思路是创建一个临时变量来保存*this,然后返回tmp加等day的结果即可。

这里需要注意的是,由于这个函数运行结束之后,tmp会先被销毁掉,再进行返回,因此我们如果返回值为引用的话,则会出错。

2.4.9减去一个天数的减等实现

//减去一个天数
//减等
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);}
}

与实现加等类似的是,这里我们也是类似的步骤,通过一个循环来不断的更新年月日。 

2.4.10减去一个天数的减实现

//减去一个天数
Date Date::operator-(int day)const
{Date tmp = *this;tmp -= day;return tmp;
}

这里和上面的加等相似。 

2.4.11两个日期相减的实现

首先,我们要判断出哪个日期大

之后,我们让小的日期不断加1,直到他们相同。

加了多少次1,两个日期就相隔多少天。 

//两个日期相减
Date Date::operator-(const Date& d)const
{//假设法判断谁大Date max = *this;Date min = d;if (min > max){max = d;min = *this;}//小的日期不断加一天,直到二者相等//设置一个计数器,计数器的值就是两个日期的差值int n = 0;while (min!+ max){min++;n++;}return n;
}

 2.4.12前后置++的实现

由于我们重载++操作符时都是这么写的:

Date::operator++()//前
Date::operator++(int)//后

这样便无法判断到底是调用前置++还是后置++了。

因此,我们规定调用后置++时,形参写一个int。

Date::operator++()//前
Date::operator++(int)//后

前置++非常容易实现,这里不再赘述。 

//前后置++--
Date& Date::operator++()//前
{*this += 1;return *this;
}
Date  Date::operator++(int)//后
{Date tmp = *this;*this + 1;return tmp;
}

后置++是先使用后++的。因此我们创建一个临时变量来保存*this,并在返回tmp前对*this+1。

2.4.13前后置--的实现

Date& Date::operator--()//前
{*this -= 1;return *this;
}
Date Date::operator--(int)//后
{Date tmp = *this;*this - 1;return tmp;
}

 2.5流插入/流提取操作符

 观察下面两行代码,我们发现这两个操作符的第二个操作数才是this。

但是成员函数默认第一个操作数为this,这就产生了问题。

因此我们不能够将这两个函数声明为成员函数。

cout << n << endl;
cin << n ;

我们需要将这两个函数声明在类外,之后通过友元在类内访问即可。

//类内
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
//定义
ostream& operator <<(ostream& out, const Date& d)
{cout << d._year << '-' << d._month << '-' << d._day;return out;
}
istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;if (!d.CheckDate()){cout << "日期非法" << endl;}return in;
}

相关文章:

  • [AIGC] HashMap的扩容与缩容:动态调整容量以提高性能
  • 【JavaEE精炼宝库】多线程进阶(2)synchronized原理、JUC类——深度理解多线程编程
  • 【Qt+opencv】图片与视频的操作
  • 13018.CUDA工程配置GDB调试
  • 探索未来远程调试新纪元——《串口网口远程调试软件》:无缝连接,高效调试
  • 森林防火气象站:守护森林安全的科技利器
  • 【FFmpeg】avcodec_find_encoder和avcodec_find_decoder
  • 《mysql篇》--查询(进阶)
  • TCP: 传输控制协议
  • 双非本 985 硕,我马上要入职上海AI实验室大模型算法岗
  • 嵌入式实验---实验五 串口数据接收实验
  • Webpack: Loader开发 (1)
  • 基于正点原子FreeRTOS学习笔记——时间片调度实验
  • pdfmake不能设置表格边框颜色?
  • UnityShader SDF有向距离场简单实现
  • JS 中的深拷贝与浅拷贝
  • 《剑指offer》分解让复杂问题更简单
  • 08.Android之View事件问题
  • Date型的使用
  • gcc介绍及安装
  • JAVA 学习IO流
  • React-flux杂记
  • SAP云平台里Global Account和Sub Account的关系
  • Solarized Scheme
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 关于springcloud Gateway中的限流
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 线上 python http server profile 实践
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 正则与JS中的正则
  • 【干货分享】dos命令大全
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • ​人工智能书单(数学基础篇)
  • # include “ “ 和 # include < >两者的区别
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (js)循环条件满足时终止循环
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (第二周)效能测试
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (十三)Flask之特殊装饰器详解
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (正则)提取页面里的img标签
  • .aanva
  • .net mvc 获取url中controller和action
  • .net refrector
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .NET开发者必备的11款免费工具
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递