【CPP】手把手教会日期类,日期类实现思路,详细思路

简介: 【CPP】手把手教会日期类,日期类实现思路,详细思路

今天简单来说一下日期类的实现,算是一个小的练习吧。

1.日期类是什么?

日期是表示时间的一种计量数字。比如我们常说公历农历。

在这里我分享的这个日期类是大大进行简化的,比如时间精度只精确到天,有效范围是0年0月0日之后…事实上,日期是非常复杂的,比如精度要精确到秒,还有大量特殊的日期,是大概1600年之后每年才比较正常,之前由于历史原因,有各种人为添加日子的情况…再加上润年的考虑…总之就是很麻烦。

2.日期类框架的构建

我们是用CPP语言来写一个简单的日期类的。那我们就按照CPP的思想先描述后组织进行依次实现。

我们用一个类来描述对应的日期,把日期看作一个对象来进行实现。

// Date.h
class Date
{
private:
  int _day;
  int _month;
  int _year;
}

好的,我们就简单的写了一个类来描述日期这一对象,然后我们再进行组织完善。

3.构造函数重写

虽说自定义类型编译器会默认给我们生成一个默认构造函数,但是显然不能满足我们的需求。

在重写构造函数的时候,我们需要考虑构造函数构造出来的日期对象是否合法的一个问题。

// Date.h
class Date
{
private:
  int _day;
  int _month;
  int _year;
public:
  Date(int year = 1, int month = 1, int day = 1);
}

这里实现一个全缺省的构造函数,不用写多个重载构造了,这样写比较方便。

然后我们在Date.cpp的文件里实现我们的重写的构造函数:先不要被劝退,从下往上看很简单,只是稍微长一点而已。下面是Date.cpp内容

// Date.cpp
bool CheckLeapYear(int year, int month)
{
  if ((!(year % 4) && (year % 100)) || !(year % 400)) return true;
  else return false;
}
int GetMonthDays(int year, int month)
{
  assert(month >= 1 && month <= 12);
  static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  if (month == 2 && CheckLeapYear(year, month)) return Months[month] + 1;
  else return Months[month];
}
bool Date::CheckInvalid()
{
  if (_year < 0 || _month > 12 || _month < 1 || _day < 0 || _day > GetMonthDays(_year, _month)) return false;
  else return true;
}
Date::Date(int year, int month, int day)
  :_year(year),_month(month),_day(day)
{
  if (!CheckInvalid()) std::cout << "error reprint" << std::endl;
}

然后我又考虑到,我们每次创建一个日期对象,都需要调用构造函数,那么每次都要调用CheckInvalid()和GetMonthDays()函数,也就是说这俩函数比较短小且被频繁调用,我干脆直接把他实现成内联函数得了。

然后就可以实现为下面这个样子:

// Date.cpp
#include"Date.h"
bool Date::CheckInvalid()
{
  if (_year < 0 || _month > 12 || _month < 1 || _day < 0 || _day > GetMonthDays(_year, _month)) return false;
  else return true;
}
Date::Date(int year, int month, int day)
  :_year(year),_month(month),_day(day)
{
  if (!CheckInvalid()) std::cout << "error reprint" << std::endl;
}
// Date.h
class Date
{
private:
  int _day;
  int _month;
  int _year;
public:
  Date(int year = 1, int month = 1, int day = 1); //简单的缺省构造函数 Date() --> CheckInvalid() --> CheckLeapYear --> GetMonthDays
  bool CheckInvalid();
  inline bool CheckLeapYear(int year, int month)
  {
    if ((!(year % 4) && (year % 100)) || !(year % 400)) return true;
    else return false;
  }
  inline int GetMonthDays(int year, int month)
  {
    assert(month >= 1 && month <= 12);
    static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && CheckLeapYear(year, month)) return Months[month] + 1;
    else return Months[month];
  }
}

做好了上面这些,日期类的构造函数算是重写完了。

我们再写一个打印日期类的函数,方便我们以后测试结果。

4.打印函数

不知道大家写代码什么习惯,因为我现在是初学者,所以写一些代码都是比较基础的,所以很多时候直接开始写一个打印函数比较容易观察现象,定位错误。

void Date::DatePrint() const
{
  std::cout << this->_year << "/" << this->_month << "/" << this->_day << std::endl;
}

注意哈,写完了打印函数要记得把这个函数声明到Date.h中去。

5.常见的Date判断函数

我们根据生活常识可知,日期类是可以进行比较大小的,比如说2024.7.25就是比2024.7.24大呀,又或者说2024.7.25与2024.7.24不是同一天(显然这是废话)…所以我们写的Date也要支持这些比较操作。

大概有下面声明的这几个比较判断操作:

// Date.h
class Date
{
private:
  int _day;
  int _month;
  int _year;
public:
  Date(int year = 1, int month = 1, int day = 1); //简单的缺省构造函数 Date() --> CheckInvalid() --> CheckLeapYear --> GetMonthDays
  bool CheckInvalid();
  inline bool CheckLeapYear(int year, int month)
  {
    if ((!(year % 4) && (year % 100)) || !(year % 400)) return true;
    else return false;
  }
  inline int GetMonthDays(int year, int month)
  {
    assert(month >= 1 && month <= 12);
    static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && CheckLeapYear(year, month)) return Months[month] + 1;
    else return Months[month];
  }
  void DatePrint() const; // 打印Date类对象信息
  // 一些常见的判断操作
  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;

然后我们着手实现的时候,因为这里有六个判断操作,并且他们之间有特定的关系,比如说小于等价于大于等于取反,不等于等价于等于取反,这个地方就可以实现两个判断操作,剩下的一顿复用就行。

#include"Date.h"
bool Date::CheckInvalid()
{
  if (_year < 0 || _month > 12 || _month < 1 || _day < 0 || _day > GetMonthDays(_year, _month)) return false;
  else return true;
}
Date::Date(int year, int month, int day)
  :_year(year),_month(month),_day(day)
{
  if (!CheckInvalid()) std::cout << "error reprint" << std::endl;
}
void Date::DatePrint() const
{
  std::cout << this->_year << "/" << this->_month << "/" << this->_day << std::endl;
}
bool Date::operator<(const Date& d) const
{
  if (this->_year < d._year) return true;
  else if(this->_year == d._year)
  {
    if (this->_month < d._month) return true;
    else if (this->_month == d._month && this->_day < d._day) return true;
  }
  return false;
}
bool Date::operator==(const Date& d) const
{
  if (_year == d._year && _month == d._month && _day == d._day) return true;
  else return false;
}
bool Date::operator<=(const Date& d) const
{
  if (*this < d || *this == d) return true;
  else return false;
}
bool Date::operator>(const Date& d) const
{
  if (!(*this <= d)) return true;
  else return false;
}
bool Date::operator>=(const Date& d) const
{
  if (!(*this < d)) return true;
  else return false;
}
bool Date::operator!=(const Date& d) const
{
  if (!(*this == d)) return true;
  else return false;
}

上面这六个判断函数就是典型的代码复用例子,以后写代码可以多注意这种代码复用技巧,比较省力是吧。

我们写完了日期之间的比较判断,其实日期也是可以进行运算的。

6.日期类的运算

不知道大家注意到没有,日期是可以进行运算的。比如今天是2024年7月25日,那么在今天的基础上减一操作,也就是前一天是2024.7.14,当然也可以加上30天,看看三十天后是几月几号。我们也可以拿到两个日期来算他们之间差多少天…当然需要注意的是有些日期运算是没有意义的,比如一个日期+另一个日期是没有意义的,我们也就不必实现没有意义的运算。

#pragma once
#include<iostream>
#include<assert.h>
class Date
{
private:
  int _day;
  int _month;
  int _year;
public:
  Date(int year = 1, int month = 1, int day = 1); //简单的缺省构造函数 Date() --> CheckInvalid() --> CheckLeapYear --> GetMonthDays
  bool CheckInvalid();
  inline bool CheckLeapYear(int year, int month)
  {
    if ((!(year % 4) && (year % 100)) || !(year % 400)) return true;
    else return false;
  }
  inline int GetMonthDays(int year, int month)
  {
    assert(month >= 1 && month <= 12);
    static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && CheckLeapYear(year, month)) return Months[month] + 1;
    else return Months[month];
  }
  void DatePrint() const; // 打印Date类对象信息
  // 一些常见的判断操作
  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+=(const int day);
  Date operator+(const int day) const;
  Date& operator++(); // 前置++
  Date operator++(int); // 后置++
  int operator-(Date& d);

实现一下:

Date& Date::operator+=(const int day)
{
  this->_day += day;
  while (_day > GetMonthDays(this->_year, this->_month))
  {
    this->_day -= GetMonthDays(this->_year, this->_month);
    this->_month++;
    if (this->_month > 12)
    {
      this->_month = 1;
      this->_year++;
    }
  }
  return *this;
}
Date Date::operator+(const int day) const
{
  Date temp = *this; // 拷贝构造
  temp += day;
  return temp;
}
Date& Date::operator++() // 前置++
{
  *this += 1;
  return *this;
}
Date Date::operator++(int) // 后置++
{
  Date temp = *this;
  *this += 1;
  return temp;
}
int Date::operator-(Date& d)
{
  int sub = 0;
  Date max, min;
  if (*this > d)
  {
    max = *this;
    min = d;
  }
  else
  {
    max = d;
    min = *this;
  }
  while (max != min)
  {
    min++;
    sub++;
  }
  return sub;
}

处理完了日期类的运算,基本最难的也就过去了,还有个比较重要的流插入和流提取去重写,基本日期类就写完了。

7.流插入、流提取

#pragma once
#include<iostream>
#include<assert.h>
class Date
{
private:
  int _day;
  int _month;
  int _year;
public:
  Date(int year = 1, int month = 1, int day = 1); //简单的缺省构造函数 Date() --> CheckInvalid() --> CheckLeapYear --> GetMonthDays
  bool CheckInvalid();
  inline bool CheckLeapYear(int year, int month)
  {
    if ((!(year % 4) && (year % 100)) || !(year % 400)) return true;
    else return false;
  }
  inline int GetMonthDays(int year, int month)
  {
    assert(month >= 1 && month <= 12);
    static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && CheckLeapYear(year, month)) return Months[month] + 1;
    else return Months[month];
  }
  void DatePrint() const; // 打印Date类对象信息
  // 一些常见的判断操作
  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+=(const int day);
  Date operator+(const int day) const;
  Date& operator++(); // 前置++
  Date operator++(int); // 后置++
  int operator-(Date& d);
  // 流插入和流提取
  friend std::ostream& operator<<(std::ostream& out, const Date& d);
  friend std::istream& operator>>(std::istream& in, Date& d);
};
std::ostream& operator<<(std::ostream& out, const Date& d);
std::istream& operator>>(std::istream& in, Date& d);
std::ostream& operator<<(std::ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << std::endl;
  return out;
}
std::istream& operator>>(std::istream& in, Date& d)
{
  while (true)
  {
    in >> d._year >> d._month >> d._day;
    if (!d.CheckInvalid())
    {
      std::cout << "error, refail" << std::endl;
      continue;
    }
    else
    {
      break;
    }
  }
  return in;
}

这个流插入和流提取用到了友元函数、并且与其他的函数不同,其他函数是声明在了Date类内,而流插入提取在类外。

这个主要是因为流插入提取如果声明在类内,第一个参数就一定是隐藏this指针,到时候我们在外面调用流插入的时候写法会很离谱。这里就不多说了。详见下图:

好了,终于说完了实现Date类的大体思路了,感觉我只是说了一下大体的实现思路,每个函数具体怎么去实现,我想代码是最具体的,当然有些地方可能有疑问,问什么这么做为什么这样处理,先思考,不明白可以评论区里问。

下面把全部代码放下面,有需要可以看看全部代码。

8.全部代码

// Date.h
#pragma once
#include<iostream>
#include<assert.h>
class Date
{
private:
  int _day;
  int _month;
  int _year;
public:
  Date(int year = 1, int month = 1, int day = 1); //简单的缺省构造函数 Date() --> CheckInvalid() --> CheckLeapYear --> GetMonthDays
  bool CheckInvalid();
  inline bool CheckLeapYear(int year, int month)
  {
    if ((!(year % 4) && (year % 100)) || !(year % 400)) return true;
    else return false;
  }
  inline int GetMonthDays(int year, int month)
  {
    assert(month >= 1 && month <= 12);
    static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2 && CheckLeapYear(year, month)) return Months[month] + 1;
    else return Months[month];
  }
  void DatePrint() const; // 打印Date类对象信息
  // 一些常见的判断操作
  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+=(const int day);
  Date operator+(const int day) const;
  Date& operator++(); // 前置++
  Date operator++(int); // 后置++
  int operator-(Date& d);
  // 流插入和流提取
  friend std::ostream& operator<<(std::ostream& out, const Date& d);
  friend std::istream& operator>>(std::istream& in, Date& d);
};
std::ostream& operator<<(std::ostream& out, const Date& d);
std::istream& operator>>(std::istream& in, Date& d);
// Date.cpp
#include"Date.h"
bool Date::CheckInvalid()
{
  if (_year < 0 || _month > 12 || _month < 1 || _day < 0 || _day > GetMonthDays(_year, _month)) return false;
  else return true;
}
Date::Date(int year, int month, int day)
  :_year(year),_month(month),_day(day)
{
  if (!CheckInvalid()) std::cout << "error reprint" << std::endl;
}
void Date::DatePrint() const
{
  std::cout << this->_year << "/" << this->_month << "/" << this->_day << std::endl;
}
bool Date::operator<(const Date& d) const
{
  if (this->_year < d._year) return true;
  else if(this->_year == d._year)
  {
    if (this->_month < d._month) return true;
    else if (this->_month == d._month && this->_day < d._day) return true;
  }
  return false;
}
bool Date::operator==(const Date& d) const
{
  if (_year == d._year && _month == d._month && _day == d._day) return true;
  else return false;
}
bool Date::operator<=(const Date& d) const
{
  if (*this < d || *this == d) return true;
  else return false;
}
bool Date::operator>(const Date& d) const
{
  if (!(*this <= d)) return true;
  else return false;
}
bool Date::operator>=(const Date& d) const
{
  if (!(*this < d)) return true;
  else return false;
}
bool Date::operator!=(const Date& d) const
{
  if (!(*this == d)) return true;
  else return false;
}
Date& Date::operator+=(const int day)
{
  this->_day += day;
  while (_day > GetMonthDays(this->_year, this->_month))
  {
    this->_day -= GetMonthDays(this->_year, this->_month);
    this->_month++;
    if (this->_month > 12)
    {
      this->_month = 1;
      this->_year++;
    }
  }
  return *this;
}
Date Date::operator+(const int day) const
{
  Date temp = *this; // 拷贝构造
  temp += day;
  return temp;
}
Date& Date::operator++() // 前置++
{
  *this += 1;
  return *this;
}
Date Date::operator++(int) // 后置++
{
  Date temp = *this;
  *this += 1;
  return temp;
}
int Date::operator-(Date& d)
{
  int sub = 0;
  Date max, min;
  if (*this > d)
  {
    max = *this;
    min = d;
  }
  else
  {
    max = d;
    min = *this;
  }
  while (max != min)
  {
    min++;
    sub++;
  }
  return sub;
}
std::ostream& operator<<(std::ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << std::endl;
  return out;
}
std::istream& operator>>(std::istream& in, Date& d)
{
  while (true)
  {
    in >> d._year >> d._month >> d._day;
    if (!d.CheckInvalid())
    {
      std::cout << "error, refail" << std::endl;
      continue;
    }
    else
    {
      break;
    }
  }
  return in;
}

EOF

相关文章
|
算法 JavaScript 前端开发
开源项目推荐:CNC+CRC/SoftPLC/OpenCASCADE/CAD/CAM(三)
开源项目推荐:CNC+CRC/SoftPLC/OpenCASCADE/CAD/CAM
4579 1
开源项目推荐:CNC+CRC/SoftPLC/OpenCASCADE/CAD/CAM(三)
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8065 2
|
10天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18426 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
787 0
|
8天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1492 5
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2635 8