C++实现日期类的方法详解

 更新时间:2024年01月08日 09:51:09   作者:小王又困了  
这篇文章主要给大家介绍了C++实现日期类的方法,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下

一、日期类的实现

1.1日期类功能

头文件中是我们要实现日期类功能的函数声明。这里我们要注意拷贝函数,只能在函数声明时写缺省值,防止我们在声明和定义是给的缺省值不一样。

#include <iostream>
#include <assert.h>
using namespace std;
 
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1);
 
    void Print();
    int GetMonthDay(int year, int month);
 
    bool operator==(const Date& y);
    bool operator!=(const Date& y);
    bool operator>(const Date& y);
    bool operator<(const Date& y);
    bool operator>=(const Date& y);
    bool operator<=(const Date& y);
 
    int operator-(const Date& d);
    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);
private:
    int _year;
    int _month;
    int _day;
};

1.2拷贝日期

有时输入的日期可能是非法的,例如:月份大于12,日期大于31,还有闰2月天数等。所以我们要对输入的日期进行判断,因为每个月的天数不同,所以要用到GetMonthDay函数。

Date::Date(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
 
    if (_year < 1 || 
        _month < 1 || _month>12 || 
        _day < 1 || _day > GetMonthDay(_year, _month))
	{
        //assert(false);
        Print();
        cout << "日期非法" << endl;
	}
}
 
int Date::GetMonthDay(int year, int month)
{
    assert(year >= 1 && month >= 1 && month <= 12);
 
    int monthArray[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 monthArray[month];
}

小Tips:我们先判断是否是二月,在判断是否是闰年可以提高效率。 

1.3重载关系运算符

关系运算符有:<、>、==、<=、>=、!=,我们在写实现这些功能的代码时,会发现它们逻辑相同,会复制粘贴,但这样的代码看着十分冗余,我们可以通过复用来实现,让代码更简单。

重载==

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

重载!=

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

重载<

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;
    }
    else
    {
        return false;
    }
}

重载<=

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

重载>

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

重载>=

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

1.4重载+、+=

我们想知道50天之后的日期,就可以通过重载+、+=来实现。在加天数的时候,由于每个月的天数不一样,所以进位就不同,我们要得到每个月份的天数。

获得月份的天数

int Date::GetMonthDay(int year, int month)
{
    assert(year >= 1 && month >= 1 && month <= 12);
 
    int monthArray[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 monthArray[month];
}

小Tips:把month == 2放在前面判断可以提高效率,如果不是二月就不需要判断是否是闰年,如果是二月在判断。

重载+=

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 == 13)
        {
            _year++;
            _month = 1;
        }
    }
    return *this;
}

重载+

Date Date::operator+(int day)
{
    if(x < 0)
    {
        return *this - (-day);
    }
 
    Date tmp(*this);
    tmp._day = _day + x;
    while (tmp._day > GetDay(tmp._year, tmp._month))
    {
        tmp._day = tmp._day - GetDay(tmp._year, tmp._month);
        tmp._month++;
        if (tmp._month == 13)
        {
            tmp._year++;
            tmp._month = 1;
        }
    }
    return tmp;
}

小Tips:重载+是原来的日期不会改变,所以我们拷贝构造了一个和*this相同的对象,我们对tmp对象修改,不会改变*this。重载+=是在原来的日期上直接修改,所以我们直接对*this指向的日期进行修改,然后返回就可以。

为什么重载+=用引用返回?

在重载+=中,*this就是d1,它的作用域是函数结束后才销毁,由于传值返回会拷贝一份返回值,所以为了减少返回时的拷贝,所以使用引用返回。在重载+中,tmp出了operator+函数就被销毁,所以只能使用传值返回。

++=之间的复用

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

1.5重载 -、-=

有时我们想知道以前的日期,日期-天数可以知道多少天以前的日期,日期-日期可以知道两个日期直接隔了多少天。两个operator-函数构成了函数重载。

重载-=

Date& operator-=(int x)
{
    //天数天数小于0,复用+=
    if (x < 0)
    {
        return *this += -x;
    }
    _day -= x;
    while (_day <= 0)
    {
        _month--;
        if (_month == 0)
        {
            _month = 12;
            _year--;
	    }
        _day += GetDay(_year, _month);
    }
 
    return *this;
}

重载日期-天数

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

重载日期-日期

日期-日期,计算的结果是两个日期之间的天数,所以返回值是int,要知道两个日期之间相隔的天数,可以设置一个计数器,让小日期一直加到大日期,就可以知道两个日期之间相隔的天数。

int 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)
    {
        --max;
        ++n;
    }
 
    return n * flag;
}

1.6重载++、--

重载前置++

前置++要返回++之后的值

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

重载前置++

后置++要返回++之前的值

Date Date::operator++(int)//编译器会把有int的视为后置++
{
    Date tmp(*this);
    *this += 1;//复用+=
    return tmp;
}

小Tips:这两个operator++函数构成了函数重载,那调用的时候怎么区分前置++和后置++呢?后置重载的时候多增加一个int类型的参数,使用后置++时,调用运算符重载函数时不用传递参数,编译器自动传递。

重载前置 -- 

前置--要返回--之后的值

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

重载后置 -- 

后置--要返回--之前的值

Date operator--(int)
{
    Date tmp(*this);
    *this -= 1;//复用了-=
    return tmp;
}

结语: 

到此这篇关于C++实现日期类的方法详解的文章就介绍到这了,更多相关C++实现日期类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++输入输出重定向方法示例

    C++输入输出重定向方法示例

    这篇文章主要给大家介绍了关于C++输入输出重定向的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • 探讨register关键字在c语言和c++中的差异

    探讨register关键字在c语言和c++中的差异

    建议不要用register关键字定义全局变量,因为全局变量的生命周期是从执行程序开始,一直到程序结束才会终止,而register变量可能会存放在cpu的寄存器中,如果在程序的整个生命周期内都占用着寄存器的话,这是个相当不好的举措
    2013-10-10
  • 基于C++实现一个简单的音乐系统

    基于C++实现一个简单的音乐系统

    C++中的Beep 函数是一个发出嗡鸣声的函数,本文将利用这个函数实现制作一个简单的声音系统。文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-12-12
  • C++设计模式之迭代器模式

    C++设计模式之迭代器模式

    这篇文章主要介绍了C++设计模式之迭代器模式,本文讲解了什么是迭代器模式、迭代器模式的代码实例等内容,需要的朋友可以参考下
    2014-10-10
  • C++中的对象指针总结

    C++中的对象指针总结

    以下是对C++中的对象指针进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-10-10
  • C语言代码实现简单三子棋游戏

    C语言代码实现简单三子棋游戏

    这篇文章主要为大家详细介绍了C语言代码实现简单三子棋游戏,文中安装步骤介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 使用C++中string实现任意长度的正小数、整数之间加减法方法实例

    使用C++中string实现任意长度的正小数、整数之间加减法方法实例

    这篇文章主要介绍了利用C++中string函数实现任意长度的正小数、整数之间加减法方法实例,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • c++ 内联函数和普通函数的区别

    c++ 内联函数和普通函数的区别

    内联函数是c++为了提高程序的运行速度做的改进,那么内联函数和普通函数的区别是什么,本文就来详细的介绍一下,感兴趣的朋友可以了解一下
    2021-05-05
  • C++利用链表模板类实现简易队列

    C++利用链表模板类实现简易队列

    这篇文章主要为大家详细介绍了C++利用链表模板类实现一个简易队列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • C++实现算法两个数字相加详解

    C++实现算法两个数字相加详解

    这篇文章主要介绍了C++实现算法两个数字相加详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07

最新评论