类和对象——日期类实现

简介: 类和对象——日期类实现

前言:

       我们之前已经学习过了类和对象,我们今天来实践应用一下,来增强感悟!

一、构造

       我们要实现日期类第一步便是对其经行构造,使其成为一个类,以下便是创建的类:

class Date
{
  public:
    Date(int year = 0, int month = 0, int day = 0);
  private:
  int _year;
  int _month;
  int _day;  
};

       日期类只需创建出年月日即可,以下是实例化,我们这次是声明和实现分开。

Date::Date(int year,int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
}

       这便是日期类的创建,看着这代码,你不由陷入了深思:如果我随便输入月和日该如何处理,比如:2月31日,这不就是坑吗?咱们要防患于未然,那我们能不能写一个函数,来获取月份的天数,然后我们在进行判断,符合我们就采取,不符合就进行提示。

       说干就干,那我们该如何获取月份天数呢?我们可采取这个方法:先定义一个数组,把每个月天数放进去,二月按照28天先放,然后进行闰年判断,是闰年2月返回29,否则就按照数组中的值进行返回。

int Date::GetMonthDay(int year,int month)
{
  assert(month > 0 && month < 13);
  int _month[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  //此处定义为13是为了方便,不用进行处理
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)        //闰年判断
  {
    if (month == 2)
    {
      return 29;
    }
  }
    return _month[month];
}

       那,我们日期类的创建可以做出以下的改进:

Date::Date(int year,int month, int day)
{
  if (month > 0 && month < 13
    && (day > 0 && day <= GetMonthDay(year, month)))
  {
    _year = year;
    _month = month;
    _day = day;
  }
  else
  {
    cout << "非法坐标" << endl;
  }
}

       这下你看的顺眼多了,但还是觉得缺点什么?还没实现打印函数,那就顺手实现吧!

void Date::Print()
{
  cout << _year << "/" << _month << "/" << _day;
}

       好了,接下来实现下一个函数吧!

二、运算符重载

       运算符重载在之前文章都有介绍,这里就简单说一下:我们只需任意两个即可,其余我们都可以进行复用。实现如下:

bool Date::operator==(const Date& d)
{
  return _year == d._year
    && _month == d._month
    && _day == d._day;
}
 
bool Date::operator<(const Date& d)
{
  if (_year < d._year)
  {
    return true;
  }
  else if (_year == d._year && _month < d._month)
  {
    return true;
  }
  else if (_year == d._year && _month == d._month && _day < d._day)
  {
    return true;
  }
  return false;
}
 
bool Date::operator<=(const Date& d)
{
  return *this < d || *this == d;
}
 
bool Date::operator>(const Date& d)
{
  return !(*this <= d);
}
 
bool Date::operator>=(const Date& d)
{
  return !(*this < d);
}
 
bool Date::operator!=(const Date& d)
{
  return !(*this == d);
}

       我们这里实现的是== 与 < 这两个运算符,大家可自行选择,难度不大。

三、++与--

       关于这两个运算符都有前置与后置两个用法,区别为:前置后面无参数,后置后面需加int,但这个int不参与运算。实现如下:

Date& Date::operator++()  //引用目的为:改变大小
{
  *this += 1;
  return *this;
}
 
Date Date::operator++(int)
{
  Date tmp(*this);    //创建对象目的为:不改变大小
  *this += 1;
  return tmp;
}
 
Date& Date::operator--()
{
  *this -= 1;
  return *this;
}
 
Date Date::operator--(int)
{
  Date tmp(*this);
  *this -= 1;
  return *this;
}

四、实现+与+= 操作

       4.1 实现+= 操作

               大家想一想:对于+= 这个操作,大家如何实现。我们想要实现可不可以这样搞:先啥都不管先把日期加上再说,那要是超出日期怎么办?简单,把月++然后把日减去月++前的月日期,如果还不符合,循环即可。如果月份大于十二,那就月份置一,年++。这个思路应该没啥问题。那我们考虑一点:如何接受返回值?在使用该操作符时我们明白:会改变该变量本身。所以,我们直接将其本身返回即可。实现如下:

Date& Date::operator+=(int day)
{
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year,_month);
    _month++;
    if (_month > 12)
    {
      _year++;
      _month = 1;
    }
 
  }
  return *this;
}

       4..2 实现+操作

               对于+这个操作符我们是否能够可以参考以上的思路?当然可以,不过我们思考一件事:我们使用+这个操作符,它本身是不是不改变?改变的原因是不是最后的赋值?大家想清楚这个问题,想清楚了这个问题我们来想如何解决这个问题,那既然是不改变,我们能不能创建一个临时变量叫它来帮助我们,就像实现后置++、--操作符。我们的实现如下:

Date Date::operator+(int day)
{
  Date tmp(*this);
  tmp._day += day;
  while (tmp._day > GetMonthDay(tmp._year, tmp._month))
  {
    tmp._day -= GetMonthDay(tmp._year, tmp._month);
    tmp._month++;
    if (tmp._month > 12)
    {
      tmp._month = 1;
      tmp._year++;
    }
  }
  return tmp;
}

       4.3 效率分析

               对于以上我们是不是可以用运算符重载来实现,那我们不禁思考:用谁来实现谁比较好?是+来复用+=,还是+=来复用+?这关乎与效率问题,大家选好心目中的队伍,我们来分析吧!

五、实现 - 与-= 操作符

       有了 + 与 += 操作符的实现,我们对于该操作符实现可以说是很简单了,所以,大家参考以下代码即可:

Date& Date::operator-=(int day)
{
  _day -= day;
  while (_day <= 0)
  {
    _month--;
    if (_month == 0)
    {
      _year--;
      _month = 12;
    }
    _day += GetMonthDay(_year, _month);
  }
  return *this;
}
 
Date Date::operator-(int day)
{
  Date tmp(*this);
  tmp -= day;
  return tmp;
}

       咱们现在以是实现完毕,这时我们想一个问题:如果有人故意捣乱传的值为负数怎么办?很简单,+= 移交给 -= ,反之,亦然。咱们以-= 为例,只需加入以下代码即可:

Date& Date::operator-=(int day)
{
  if (day < 0)          //进行判断,符合条件进行复用即可
  {
    *this += day;
    return *this;
  }
  _day -= day;
  while (_day <= 0)
  {
    _month--;
    if (_month == 0)
    {
      _year--;
      _month = 12;
    }
    _day += GetMonthDay(_year, _month);
  }
  return *this;
}

六、日期减日期

       我们明白,日期加日期没啥用,但是日期减去一个日期就有用了不少,我们不禁想明白这个该如何实现:直接减去,我们啥都要考虑,闰年不润年先放一边,主要是你月份相减所得的天数是哪几个月的?这是个很复杂的问题。现提供如下思路:我们先把大的选出来,如果小的减去大的最后乘以-1即可。然后,我们一直使小的++最后等于大的即可得出。代码如下:

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

七、流插入和流提取

       对于流插入和提取我们要实现的目的是:Date为自定义类型,对于自定义类型我们只能自己实现,不能依靠别人。我们要实现插入与提取最好放在全局,不放入类中,因为this指针会替代第一个问题,放入类中会反过来使用,不符合习惯。实现如下:

ostream& operator<<(ostream& out, const Date& d)
{
  out << d._year << "/" << d._month << "/" << d._day << endl;
  return out;
}
 
istream& operator>>(istream& in, Date& d)
{
  in >> d._year >> d._month >> d._day;
  return in;
} 

八、完整代码

       Date.h

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
 
class Date 
{
 
  friend ostream& operator<<(ostream& out, const Date& d);
  friend istream& operator>>(istream& in, Date& d);
 
public:
  Date(int year = 0, int month = 0, int day = 0);
  void Print();
  int GetMonthDay(int year, int month);
 
  bool operator==(const Date& d);
  bool operator!=(const Date& d);
  bool operator< (const Date& d);
  bool operator<=(const Date& d);
  bool operator> (const Date& d);
  bool operator>=(const Date& d);
 
  Date& operator+=(int day);
  Date operator+(int day);
 
  Date& operator-=(int day);
 
  // d1 - 100
  Date operator-(int day);
 
  // d1 - d2;
  int operator-(const Date& d);
 
  // ++d1
  Date& operator++();
 
  // d1++
  Date operator++(int);
 
  Date& operator--();
 
  Date operator--(int);
 
private:
  int _year;
  int _month;
  int _day;
};

       Date.cpp

#include"Data.h"
 
int Date::GetMonthDay(int year,int month)
{
  assert(month > 0 && month < 13);
  int _month[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  //此处定义为13是为了方便,不用进行处理
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)        //闰年判断
  {
    if (month == 2)
    {
      return 29;
    }
  }
    return _month[month];
}
 
Date::Date(int year,int month, int day)
{
  if (month > 0 && month < 13
    && (day > 0 && day <= GetMonthDay(year, month)))
  {
    _year = year;
    _month = month;
    _day = day;
  }
  else
  {
    cout << "非法坐标" << endl;
  }
}
 
Date& Date::operator+=(int day)
{
  if (day < 0)
  {
    *this -= day;
    return *this;
  }
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year,_month);
    _month++;
    if (_month > 12)
    {
      _year++;
      _month = 1;
    }
 
  }
  return *this;
}
 
Date Date::operator+(int day)
{
  Date tmp(*this);
  tmp += day;
  return tmp;
}
 
//Date Date::operator+(int day)
//{
//  Date tmp(*this);
//  tmp._day += day;
//  while (tmp._day > GetMonthDay(tmp._year, tmp._month))
//  {
//    tmp._day -= GetMonthDay(tmp._year, tmp._month);
//    tmp._month++;
//    if (tmp._month > 12)
//    {
//      tmp._month = 1;
//      tmp._year++;
//    }
//  }
//  return tmp;
//}
//
//Date& Date::operator+=(int day)
//{
//  *this = *this + day;
//  return *this;
//}
 
Date& Date::operator++()  //引用目的为:改变大小
{
  *this += 1;
  return *this;
}
 
Date Date::operator++(int)
{
  Date tmp(*this);    //创建对象目的为:不改变大小
  *this += 1;
  return tmp;
}
 
Date& Date::operator--()
{
  *this -= 1;
  return *this;
}
 
Date Date::operator--(int)
{
  Date tmp(*this);
  *this -= 1;
  return *this;
}
 
Date& Date::operator-=(int day)
{
  if (day < 0)          //进行判断,符合条件进行复用即可
  {
    *this += day;
    return *this;
  }
  _day -= day;
  while (_day <= 0)
  {
    _month--;
    if (_month == 0)
    {
      _year--;
      _month = 12;
    }
    _day += GetMonthDay(_year, _month);
  }
  return *this;
}
 
Date Date::operator-(int day)
{
  Date tmp(*this);
  tmp -= day;
  return tmp;
}
 
int Date::operator-(const Date& d)
{
  Date max = *this;
  Date min = d;
  int flag = 1;
  if (*this < d)
  {
    max = d;
    min = *this;
    flag = -1;
  }
  int n = 0;
  while (max != min)
  {
    min++;
    n++;
  }
  return n * flag;
}
 
void Date::Print()
{
  cout << _year << "/" << _month << "/" << _day << endl;
}
 
bool Date::operator==(const Date& d)
{
  return _year == d._year
    && _month == d._month
    && _day == d._day;
}
 
bool Date::operator<(const Date& d)
{
  if (_year < d._year)
  {
    return true;
  }
  else if (_year == d._year && _month < d._month)
  {
    return true;
  }
  else if (_year == d._year && _month == d._month && _day < d._day)
  {
    return true;
  }
  return false;
}
 
bool Date::operator<=(const Date& d)
{
  return *this < d || *this == d;
}
 
bool Date::operator>(const Date& d)
{
  return !(*this <= d);
}
 
bool Date::operator>=(const Date& d)
{
  return !(*this < d);
}
 
bool Date::operator!=(const Date& d)
{
  return !(*this == d);
}
 
ostream& operator<<(ostream& out, const Date& d)
{
  out << d._year << "/" << d._month << "/" << d._day << endl;
  return out;
}
 
istream& operator>>(istream& in, Date& d)
{
  in >> d._year >> d._month >> d._day;
  return in;
} 

完!


相关文章
|
小程序 iOS开发
|
7月前
|
网络协议 安全 Devops
Infoblox DDI (NIOS) 9.0 - DNS、DHCP 和 IPAM (DDI) 核心网络服务管理
Infoblox DDI (NIOS) 9.0 - DNS、DHCP 和 IPAM (DDI) 核心网络服务管理
257 4
|
9月前
|
消息中间件 运维 监控
智能运维,由你定义:SAE自定义日志与监控解决方案
SAE(Serverless应用引擎)是阿里云推出的全托管PaaS平台,致力于简化微服务应用开发与管理。为满足用户对可观测性和运维能力的更高需求,SAE引入Sidecar容器技术,实现日志采集、监控指标收集等功能扩展,且无需修改主应用代码。通过共享资源模式和独立资源模式,SAE平衡了资源灵活性与隔离性。同时,提供全链路运维能力,确保应用稳定性。未来,SAE将持续优化,支持更多场景,助力用户高效用云。
|
11月前
|
运维 Cloud Native 开发工具
智能运维:云原生大规模集群GitOps实践
智能运维:云原生大规模集群GitOps实践,由阿里云运维专家钟炯恩分享。内容涵盖云原生运维挑战、管理实践、GitOps实践及智能运维体系。通过OAM模型和GitOps优化方案,解决大规模集群的发布效率与稳定性问题,推动智能运维工程演进。适用于云原生环境下的高效运维管理。
423 8
|
11月前
|
弹性计算 监控 网络协议
自动化AutoTalk第十二期-使用Terraform高效实现云自动化
《自动化AutoTalk第十二期》聚焦使用Terraform高效实现云自动化。内容涵盖IaC(基础设施即代码)概述、Terraform简介与核心组件、实现云自动化步骤及最佳实践。通过Terraform的统一编排语言HCL和对资源生命周期管理,结合CI/CD流程,实现云资源的自服务管理。强调了Terraform在环境准备、业务集成、生产配置及持续监控中的应用,并分享了结合GitLab/GitHub进行代码托管和流程标准化的最佳实践。
328 9
JavaMap工具类(MapUtils)
JavaMap工具类(MapUtils)
|
Python
python 海龟画图tutle螺旋线
python 海龟画图tutle螺旋线
634 0
|
SQL JSON Kubernetes
KubeVela 项目和能力简介 | 学习笔记
快速学习 KubeVela 项目和能力简介
KubeVela 项目和能力简介 | 学习笔记
|
存储 运维 数据中心
Terraform的自动化管理
Terraform的自动化管理
208 0
|
存储 Java 测试技术
后台管理系统引入OSS实现图片上传功能
后台管理系统引入OSS实现图片上传功能
1248 0