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

🎁结语:

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

相关文章
|
2天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
42 30
|
16天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
16天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
16天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。
|
30天前
|
存储 算法 编译器
c++--类(上)
c++--类(上)
|
1月前
|
编译器 C++
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
|
1月前
|
编译器 C++
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
|
1月前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
1月前
|
编译器 C++
【C++】类和对象(中)
【C++】类和对象(中)