【C++从0到王者】第四站:日期类的实现

简介: 【C++从0到王者】第四站:日期类的实现


一、日期类的函数声明

在有了前面的类和对象的基础上,我们已经可以实现一个简单的日期类了,这个类它包括了日期的构造函数、日期的拷贝、日期的打印、日期的运算、包括两个日期的比较、相减、日期加减天数等运算。下面是日期类的函数声明

#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 = 1, int month = 1, int day = 1);
  void Print()
  {
    cout << _year << '-' << _month << '-' << _day << endl;
  }
  bool operator<(const Date& x);
  bool operator==(const Date& x);
  bool operator<=(const Date& x);
  bool operator>(const Date& x);
  bool operator>=(const Date& x);
  bool operator!=(const Date& x);
  int GetMonthDay(int year, int month);
  Date& operator+=(int day);
  Date& operator-=(int day);
  Date operator+(int day);
  Date operator-(int day);
  Date& operator++();
  Date operator++(int);
  Date& operator--();
  Date operator--(int);
  int operator-(const Date& d);
private:
  int _year;
  int _month;
  int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

二、日期类的实现

1.日期类的构造函数

如下代码所示,是日期类的构造函数,这里是函数的实现,由于是分文件,所以不能在这里写缺省参数,应该将缺省参数放在函数声明中。同时我们为了方便最好写一个获取某月的天数的函数,其中的天数的数组我们可以将他放到静态区。因为我们会频繁的调用它。

int Date::GetMonthDay(int year, int month)
{
  if (month <= 12 && month >= 1)
  {
    const static int ArrDay[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;
    }
    return ArrDay[month];
  }
  cout << "非法日期" << endl;
  return -1;
}
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;
    assert(false);
  }
}

2.日期类的析构与拷贝构造函数

这两个函数事实上是不需要我们去写的,因为我们的成员变量都是内置类型所以不需要写,使用编译器自己生成的就可以了

3.日期类的比较之d1<d2

这个运算符重载的功能是为了比较两个日期的大小,如果d1小于d2那么返回真,否则返回假。其中x是形参,他是d2的别名。且功能中并未涉及到修改d2,所以最好加上const,权限缩小。

d1<d2其实就会被编译器认为是d1.operator<(d2)

bool Date::operator<(const Date& x)
{
  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;
  }
  return false;
}

4.日期类的比较之d1==d2

这个运算符重载的功能是比较d1是否等于d2,如果相等则返回真。

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

5.日期类的其他比较

一般来说,只要我们写出一个小于和等于,那么其他的比较函数都可以用这两个函数复用给实现出来。

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

6.日期类的+=和-=

首先我们需要判断的是day是否小于0,如果小于0的话,那么其实就可以相互复用。如果是大于0的话,就需要注意算法。先将day给加上去,然后如果day大于当月天数,那么就可以进位了。-=也是类似的思路。这里我们可以返回引用,因为我们返回的是原本的日期类,出了作用域,这个日期类还在

Date& Date::operator+=(int day)
{
  if (day < 0)
  {
    return *this -= -day;
  }
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year, _month);
    _month++;
    if (_month > 12)
    {
      _month -= 12;
      _year++;
    }
  }
  return *this;
}
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);
  }
  return *this;
}

7.日期类加减具体天数

事实上在这里我们可以直接复用+=和-=,但是由于加并不会改变原本的类,所以我们先使用拷贝构造一个,然后再返回即可,这里需要注意,我们必须得返回值而不是引用,因为出了作用域tmp就不在了

Date Date::operator+(int day)
{
  Date tmp = *this;
  return tmp += day;
}
Date Date::operator-(int day)
{
  Date tmp = *this;
  return tmp -= day;
}

8.+复用+=还是+=复用+好

这里我们可能会注意到一个问题,是自己写一个+=,然后写+的时候直接复用+=好,还是自己先写一个+,然后写+=的时候直接复用+好。

其实是先写+=再让+复用+=效率要高一点,因为这样的话,首先直接使用+这个我们是不可避免的肯定会调用两次拷贝构造函数。而先写+=却不需要调用拷贝构造,如果后写+=的话,调用+的时候就会调用两次拷贝构造了。

9.前置++和后置++

这个从思路上来说比较容易实现,但是需要注意的是,如何区分前置++和后置++,再c++中,我们如果不带任何形参的话默认是前置++,如果带了一个形参的话,那就认为他是后置++。这里的实现直接复用+=即可

Date& Date::operator++()
{
  *this += 1;
  return *this;
}
Date Date::operator++(int)
{
  Date tmp = *this;
  *this += 1;
  return tmp;
}

10.前置–和后置–

这里的实现和前置++和后置++的思路基本一致

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

11.两个日期相减

这里仍然是运算符重载,但是和日期减某天这两个函数名相同,但参数类型不同,于是又构成了函数重载。这里我们采用循环计数的方法思路上比较容易实现。

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

12.流插入

这里需要注意的一点是,这个函数不可以写成成员函数。因为我们的Date对象默认占用第一个操作数。我们必须要更改操作数的位置,所以只能定义再全局中。但是这样我们就不可以使用私有的成员了,我们就有两种方案来处理,一种是在写一些获取每个成员值的函数,另外一种就是使用友元函数,你是我的朋友,所以可以来访问我的成员变量

//流插入不能写成成员函数
//因为Date对象默认占用第一个参数,就是做了左操作数
//写出来就是下面的样子,不符合我们的使用习惯
//d1<<cout;
ostream& operator<<(ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  return out;
}

我们需要注意的是cout的类型是ostream,而且他不可以加上const,因为我们是在cout里面进行写入的。我们可以直接传别名过去使得效率更高。流插入运算符在库函数里事实上也是经历了大量的重载,才使得我们的cout可以自动识别类型。最后为了可以实现连续流插入,我们需要返回out

13.流提取

这个运算符与cout类似,我们也是必须使用友元函数进行声明。注意的是,这里的in和d都不可以加上const,因为他们都会被修改。最后为了连续的流提取,我们需要返回in

istream& operator>>(istream& in, Date& d)
{
  int year, month, day;
  in >> year >> month >> day;
  if (month > 0 && month < 13
    && day>0 && day <= d.GetMonthDay(year, month))
  {
    d._year = year;
    d._month = month;
    d._day = day;
  }
  else
  {
    cout << "非法日期" << endl;
    assert(false);
  }
  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 = 1, int month = 1, int day = 1);
  void Print()
  {
    cout << _year << '-' << _month << '-' << _day << endl;
  }
  bool operator<(const Date& x);
  bool operator==(const Date& x);
  bool operator<=(const Date& x);
  bool operator>(const Date& x);
  bool operator>=(const Date& x);
  bool operator!=(const Date& x);
  int GetMonthDay(int year, int month);
  Date& operator+=(int day);
  Date& operator-=(int day);
  Date operator+(int day);
  Date operator-(int day);
  Date& operator++();
  Date operator++(int);
  Date& operator--();
  Date operator--(int);
  int operator-(const Date& d);
private:
  int _year;
  int _month;
  int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
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;
    assert(false);
  }
}
bool Date::operator<(const Date& x)
{
  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;
  }
  return false;
}
bool Date::operator==(const Date& x)
{
  return (_year == x._year) &&
       (_month == x._month) &&
       (_day == x._day);
}
bool Date::operator<=(const Date& x)
{
  return (*this < x) || (*this == x);
}
bool Date::operator>(const Date& x)
{
  return !(*this <= x);
}
bool Date::operator>=(const Date& x)
{
  return !(*this < x);
}
bool Date::operator!=(const Date& x)
{
  return !(*this == x);
}
int Date::GetMonthDay(int year, int month)
{
  if (month <= 12 && month >= 1)
  {
    const static int ArrDay[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;
    }
    return ArrDay[month];
  }
  cout << "非法日期" << endl;
  return -1;
}
Date& Date::operator+=(int day)
{
  if (day < 0)
  {
    return *this -= -day;
  }
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year, _month);
    _month++;
    if (_month > 12)
    {
      _month -= 12;
      _year++;
    }
  }
  return *this;
}
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);
  }
  return *this;
}
Date Date::operator+(int day)
{
  Date tmp = *this;
  return tmp += day;
}
Date Date::operator-(int day)
{
  Date tmp = *this;
  return tmp -= day;
}
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 tmp;
}
int Date::operator-(const Date& d)
{
  Date max = *this;
  Date min = d;
  int flag = 1;
  if (max < min)
  {
    max = d;
    min = *this;
    flag = -1;
  }
  int n = 0;
  while (max != min)
  {
    min++;
    n++;
  }
  return flag * n;
}
//流插入不能写成成员函数
//因为Date对象默认占用第一个参数,就是做了左操作数
//写出来就是下面的样子,不符合我们的使用习惯
//d1<<cout;
ostream& operator<<(ostream& out, const Date& d)
{
  out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  return out;
}
istream& operator>>(istream& in, Date& d)
{
  int year, month, day;
  in >> year >> month >> day;
  if (month > 0 && month < 13
    && day>0 && day <= d.GetMonthDay(year, month))
  {
    d._year = year;
    d._month = month;
    d._day = day;
  }
  else
  {
    cout << "非法日期" << endl;
    assert(false);
  }
  return in;
}

Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
void TestDate1()
{
  Date d1;
  Date d2(2023, 5, 13);
  d1.Print();
  d2.Print();
  cout << (d1 < d2) << endl;
  cout << (d1 == d2) << endl;
  cout << (d1 > d2) << endl;
  cout << (d1 <= d2) << endl;
  d2 += 200;
  d2.Print();
  Date d3 = d2;
  d3 -= 200;
  d3.Print();
  d1 = d3 + 200;
  d1.Print();
}
void TestDate2()
{
  Date d1(2023, 5, 13);
  d1 -= -100;
  d1.Print();
  d1 += -100;
  d1.Print();
}
void TestDate3()
{
  Date d1(2023, 5, 13);
  Date ret1 = d1--;
  ret1.Print();
  Date ret2 = --d1;
  ret2.Print();
}
void TestDate4()
{
  Date d1(2023, 5, 13);
  Date d2(2049, 6, 13);
  cout << (d2 - d1) << endl;
  cout << d1 << d2;
  cin >> d2 >> d1;
  cout << d2 << d1;
}
void TestDate5()
{
  //Date d1(2022, 13, 1);
  Date d1;
  cin >> d1;
}
int main()
{
  TestDate5();
  return 0;
}

好了,本期内容就到这里了

如果对你有帮助的话不要忘记点赞加收藏哦

相关文章
|
5天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
19 0
|
5天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1
|
3天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
4天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
5天前
|
C++ Linux
|
5天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
9 1
|
5天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
16 0
|
5天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
5天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)