『C++成长记』日期类的实现

🔥博客主页:小王又困了

📚系列专栏: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;
}

🎁结语: 

     本次的内容到这里就结束啦。希望大家阅读完可以有所收获,同时也感谢各位读者三连支持。文章有问题可以在评论区留言,博主一定认真认真修改,以后写出更好的文章。你们的支持就是博主最大的动力。

版权声明:本文为博主作者:小王又困了原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/2301_76207836/article/details/135360890

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2024年1月16日
下一篇 2024年1月16日

相关推荐