[C++]日期类的实现

简介: [C++]日期类的实现


日期类的实现:

本篇文章带大家实现一个日期类。

我们实现声明定义分离:

这是日期类中所包含的成员函数和成员变量:(Data.h)

class Date
{
     // 友元声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
  // 构造函数
  Date(int year = 0, int month = 1, int day = 1);
  // 打印函数
  void Print() const;
    //赋值重载
    Date& operator=(const Date& d);
  // 日期+=天数
  Date& operator+=(int day);
  // 日期+天数
  Date operator+(int day) const;
  // 日期-=天数
  Date& operator-=(int day);
  // 日期-天数
  Date operator-(int day) const;
  // 函数重载
     // 运算符重载
      // ++d1 -> d1.operator++()
  // 前置++
  Date& operator++();
  // d1++ -> d1.operator++(0)
   // 加一个int参数,进行占位,跟前置++构成函数重载进行区分
   // 本质后置++调用,编译器进行特殊处理
  // 后置++
  Date operator++(int);
  // 前置--
  Date& operator--();
  // 后置--
  Date operator--(int);
  // 日期的大小关系比较
  // 总结一下:只读函数可以加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;
  // 日期-日期
  int operator-(const Date& d) const;
  // 析构,拷贝构造,赋值重载可以不写,使用默认生成的即可
private:
  int _year;
  int _month;
  int _day;
};

打印函数:

// 打印函数
void Date::Print() const
{
  cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

构造函数

进入构造函数体,首先需要检查日期的合法性,只有当日期合法时,才能进行后续的构造操作。

// 获取某年某月的天数
inline int GetMonthDay(int year, int month)
{
  // 数组存储平年每个月的天数
  static int dayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  int day = dayArray[month];
  if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
  {
    //闰年2月的天数
    day = 29;
  }
  return day;
}
// 构造函数
Date::Date(int year, int month, int day)
{
  _year = year;
    _month = month;
   _day = day;
// 检查日期是否合法
if (month < 1 || month > 12
  || day < 1 || day > GetMonthDay(year, month))
{
  cout << "非法日期" << endl;
  // exit(-1);
}
  }
}

GetMonthDay函数中的三个细节:

 1.该函数可能被多次调用,所以我们最好将其设置为内联函数。

 2.函数中存储每月天数的数组最好是用static修饰,存储在静态区,避免每次调用该函数都需要重新开辟数组。

 3.逻辑与应该先判断month == 2是否为真,因为当不是2月的时候我们不必判断是不是闰年。

注意:当函数声明和定义分开时,在声明时注明缺省参数,定义时不标出缺省参数。

void TestDate1()
{
  Date d1(2024, 3, 23);
  Date d2(2023,3,23);
  
  d1.Print();
  d2.Print();
  
  Date d3(2010, 2, 29);
  d3.Print();
  
  Date d4(2023, 13, 29);
  d4.Print();
}

检查一下:

赋值重载=

Date& Date::operator=(const Date& d)
{
  if (this != &d)
  {
    _year = d._year;
    _month = d._month;
    _day = d._day;
  }
  return *this;
}

测试一下:

//测试赋值重载
void TestDate2()
{
  Date d1(2024, 2, 22);
  // 拷贝构造,一个已经存在的对象去初始化另一个要创建对象
  Date d2(d1);
  d1.Print();
  d2.Print();
  Date d3(2023, 11, 15);
  // 赋值,两个已经存在对象进行拷贝
  //d1 = d3;  // d1.operator=(d3)
  d1 = d2 = d3;
  d1.Print();
  d2.Print();
  d3.Print();
}

测试结果:

日期 += 天数

对于+=运算符,我们先将需要加的天数加到日上面,然后判断日期是否合法,若不合法,则通过不断调整,直到日期合法为止。

调整日期的思路:

 1.若日已满,则日减去当前月的天数,月加一。

 2.若月已满,则将年加一,月置为1。

反复执行1和2,直到日期合法为止。

// 日期+=天数
Date& Date::operator+=(int day)
{
  if (day<0)
  {
    // 复用operator-=
    *this -= -day;
  }
  else
  {
    _day += day;
    // 日期不合法,通过不断调整,直到最后日期合法为止
    while (_day > GetMonthDay(_year, _month))
    {
      _day -= GetMonthDay(_year, _month);
      _month++;
      if (_month > 12)
      {
        _year++;
        _month = 1;
      }
    }
  }
  return *this;
}

注:当需要加的天数为负数时,转而调用-=运算符重载函数。

测试+=

//测试+=
void TestDate3()
{
  Date d1(2024, 2, 22);
  d1 += 200;
  d1.Print();
}

测试结果:

200天后是2024年9月9日,那么对不对呢?

我们用日期计算器计算一下:

发现没有问题。

日期 + 天数

+运算符的重载,我们可以复用上面已经实现的+=运算符的重载函数。但是要注意:虽然我们返回的是加了之后的值,但是对象本身的值并没有改变。就像a = b + 1,b + 1的返回值是b + 1,但是b的值并没有改变。所以我们还可以用const对该函数进行修饰,防止函数内部改变了this指针指向的对象。

// 日期+天数
Date Date::operator+(int day) const
{
  Date tmp(*this);// 拷贝构造tmp,用于返回
  // 复用operator+=
  tmp += day;
  return tmp;
}

注意:+=运算符的重载函数采用的是引用返回,因为出了函数作用域,this指针指向的对象没有被销毁。但+运算符的重载函数的返回值只能是传值返回,因为出了函数作用域,对象tmp就被销毁了,不能使用引用返回。

日期 -= 天数

对于-=运算符,我们先用日减去需要减的天数,然后判断日期是否合法,若不合法,则通过不断调整,直到日期合法为止。

调整日期的思路:

 1.若日为负数,则月减一。

 2.若月为0,则年减一,月置为12。

 3.日加上当前月的天数。

反复执行1、2和3,直到日期合法为止。

// 日期-=天数
Date& Date::operator-=(int day)
{
  if (day < 0)
  {
    // 复用operator+=
    *this += -day;
  }
  else
  {
    _day -= day;
    // 日期不合法,通过不断调整,直到最后日期合法为止
    while (_day <= 0)
    {
      _month--;
      if (_month == 0)
      {
        _year--;
        _month = 12;
      }
      _day += GetMonthDay(_year, _month);
    }
  }
  return *this;
}

注:当需要减的天数为负数时,转而调用+=运算符重载函数。

日期 - 天数

和+运算符的重载类似,我们可以复用上面已经实现的-=运算符的重载函数,而且最好用const对该函数进行修饰,防止函数内部改变了this指针指向的对象。

// 日期-天数
Date Date::operator-(int day) const
{
  Date tmp(*this);// 拷贝构造tmp,用于返回
  // 复用operator-=
  tmp -= day;
  return tmp;
}

注意:-=运算符的重载函数采用的是引用返回,但-运算符的重载函数的返回值只能是传值返回,也是由于-运算符重载函数中的tmp对象出了函数作用域被销毁了,所以不能使用引用返回。

测试-=

//测试-=
void TestDate4()
{
  Date d1(2024, 2, 22);
  d1 -= 200;
  d1.Print();
  
  Date d2(2024, 3, 22);
  d2 += -200;
  d2.Print();
  
  Date d3(2024, 9, 6);
  d3 -= -200;
  d3.Print();
}

测试结果:

前置 ++

前置++,我们可以复用+=运算符的重载函数。

// 前置++
Date& Date::operator++()
{
  // 复用operator+=
  *this += 1;
  return *this;
}

后置 ++

由于前置++和后置++的运算符均为++,为了区分它们的运算符重载,我们给后置++的运算符重载的参数加上一个int型参数,使用后置++时不需要给这个int参数传入实参,因为这里int参数的作用只是为了跟前置++构成重载。

// 后置++
Date Date::operator++(int)
{
  Date tmp(*this);// 拷贝构造tmp,用于返回
  // 复用operator+=
  *this += 1;
  return tmp;
}

注意:后置++也是需要返回加了之前的值,只能先用对象tmp保存之前的值,然后再然对象加一,最后返回tmp对象。由于tmp对象出了该函数作用域就被销毁了,所以后置++只能使用传值返回,而前置++可以使用引用返回。

测试一下:

void TestDate5()
{
  Date d1(2023, 11, 14);
  Date ret1 = d1++;
  //Date ret1 = d1.operator++(0);
  ret1.Print();
  d1.Print();
  Date ret2 = ++d1;
  //Date ret2 = d1.operator++();
  ret2.Print();
  d1.Print();
}

测试结果:

前置 –

前置–,我们也是可以复用前面的-=运算符的重载函数。

// 前置--
Date& Date::operator--()
{
  // 复用operator-=
  *this -= 1;
  return *this;
}

后置–

后置–需要注意的事项和后置++是一样的,我这里就不过多阐述了。

// 后置--
Date Date::operator--(int)
{
  Date tmp(*this);// 拷贝构造tmp,用于返回
  // 复用operator-=
  *this -= 1;
  return tmp;
}

日期类的大小关系比较

日期类的大小关系比较需要重载的运算符看起来有6个,实际上我们只用实现两个就可以了,然后其他的通过复用这两个就可以实现。

注意:进行日期的大小比较,我们并不会改变传入对象的值,所以这6个运算符重载函数都应该被const所修饰。

>运算符的重载

>运算符的重载很简单,先判断年是否大于,再判断月是否大于,最后判断日是否大于,这其中有一者为真则函数返回true,否则返回false。

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

==运算符的重载

==运算符的重载也是很简单,年月日均相等,则为真。

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

>=运算符的重载

>=,即大于或者等于,满足其中之一即可。
bool Date::operator>=(const Date& d) const
{
  return *this > d || *this == d;
}

<运算符的重载

<,大于等于的反面即是小于。

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

<=运算符的重载

<=,大于的返回即是小于等于。

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

!=运算符的重载

!=,等于的反面即是不等于。

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

日期 - 日期

日期 - 日期,即计算传入的两个日期相差的天数。我们只需要让较小的日期的天数一直加一,直到最后和较大的日期相等即可,这个过程中较小日期所加的总天数便是这两个日期之间差值的绝对值。若是第一个日期大于第二个日期,则返回这个差值的正值,若第一个日期小于第二个日期,则返回这个差值的负值。

// 日期-日期
int Date::operator-(const Date& d) const
{
  Date max = *this;// 假设第一个日期较大
  Date min = d;// 假设第二个日期较小
  int flag = 1;// 此时结果应该为正值
  if (*this < d)
  {
    // 假设错误,更正
    max = d;
    min = *this;
    flag = -1;// 此时结果应该为负值
  }
  int n = 0;// 记录所加的总天数
  while (min != max)
  {
    min++;// 较小的日期++
    n++;// 总天数++
  }
  return n*flag;
}

测试一下:

blog.csdnimg.cn/6b4338f019944d85a35f4c08362ec4d7.png)

代码中使用flag变量标记返回值的正负,flag为1代表返回的是正值,flag为-1代表返回的是负值,最后返回总天数与flag相乘之后的值即可。

输出

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;
}


相关文章
|
1天前
|
C++
【C++基础】类class
【C++基础】类class
9 1
|
1天前
|
安全 程序员 编译器
C++程序中的基类与派生类转换
C++程序中的基类与派生类转换
8 1
|
1天前
|
C++
C++程序中的类成员函数
C++程序中的类成员函数
7 1
|
1天前
|
C++
C++程序中的类封装性与信息隐蔽
C++程序中的类封装性与信息隐蔽
8 1
|
1天前
|
C++
C++程序中的类声明与对象定义
C++程序中的类声明与对象定义
9 1
|
1天前
|
数据安全/隐私保护 C++
C++程序中的派生类
C++程序中的派生类
6 1
|
1天前
|
C++
C++程序中的派生类成员访问属性
C++程序中的派生类成员访问属性
8 1
|
1天前
|
编译器 C++
C++程序中的派生类析构函数
C++程序中的派生类析构函数
9 2
|
4天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
6天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0