日期类运算符的重载

前言

在C++中我们我们内置类型想要进行计算可以用C++自带的+、-、*、/进行运算,对于我们的自定义类型该怎么办呢?我们可以通过运算符重载的方式来给运算符定义一套新的规则,今天我们就来以日期类来来介绍一下类型的重载。

1.运算符重载的特性

(1)当运算符被用于类类型的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。类类型的对象使用运算符时,必须调用对应运算符重载,如果没有重载运算符就会报错。

(2)运算符重载是具有特殊的名字的函数,他的名字由operator和运算符共同构成,它有返回类型,参数表及函数体。

(3)重载运算符的参数和该运算符作用的运算符对象数量一样多。

(4)若一个重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的this指针,因此运算符对象作为成员函数时参数笔运算对象少一个。

(5)运算符重载以后,其优先级,结核性与对应内置类型运算符保持一致。

(6)不通过连接语法中没有的符号创建新的操作符。

(7).* 、 :: 、 sizeof 、 ?: 、 . 以上五种运算符不能重载。

(8)一个类需要重载哪些运算符要看哪些运算符重载后有意义。

(9)重载操作至少有一个类类型的参数,不能通过运算符重载改变内置类型对象的含义。

(10)重载++,有前置++和后置++(前置无参eg:Date operator++() , 后置有参eg:Date operator++(int i))。

(11)<<流插入 和 >>流提取,重载成全局函数,解决挣抢位置的问题。

2.日期类实现的接口

bool operator<(const Date& y1) const;
bool operator==(const Date& y1) const;
bool operator>(const Date& y1) const;
bool operator>=(const Date& y1) const;
bool operator<=(const Date& y1) const;
bool operator!=(const Date& y1) const;
​
​
Date operator+(const int day) const;
Date operator-(const int day) const;
Date& operator+=(const int day);
Date& operator-=(const int day);
​
//前置
Date operator++();
Date operator--();
//后置
Date operator++(int i);
Date operator--(int i);
​
int operator-(const Date& d1)const;
​
std::ostream& operator<<(std::ostream& out, const Date& d);
​
std::istream& operator>>(std::istream& in, Date& d);

3.日期类接口功能的实现

(1)重载<

先比较两个年,如果年相等就比较月,如果月也相等就比较日。

bool Date::operator<(const Date& y1) const
{
    
    if (_year < y1._year)
    {
        return true;
    }
    else if (_year == y1._year && _month < y1._month)
    {
        return true;
    }
    else if ((_year == y1._year && _month == y1._month) && _day < y1._day)
    {
        return true;
    }
    return false;
}

(2)重载==

判断年月是是否都相等

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

(3)重载>

不小于且不等于就是大于

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

(4)重载>=

不小于就是大于等于

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

(5)重载<=

不大于就是小于等于

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

(6)求天数

通过一个数组来存储每个月的天数,然后判断一下是否为二月和闰年,闰年的二月有29天

int Date::Day(int _year, int _month)
{
    static int a[13] = { -1,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 a[_month];
}

(7)重载!=

不等于

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

(8)重载+

额外创建一个变量,然后对新创建的变量加等要加的天数。

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

(9)重载-

额外创建一个变量,然后对新创建的变量减等要减的天数。

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

(10)重载+=

先把要加的天数加到日上,然后根据每个月的天数减下去直到日小于当月天数为止。

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

(11)重载-=

先把要减的天数减去到日上,然后根据每个月的天数加上去直到日大于0。

Date& Date::operator-=(int day)
{
    if (day < 0)
    {
        *this += -day;
    }
    _day -= day;
    while (_day <= 0)
    {
        _month--;
        if (_month == 0)
        {
            _year--;
            _month = 12;
        }
        _day += Day(_year, _month);
    }
    return *this;
}

(12)重载前置++

直接给传入的日期加等1

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

(13)重载前置--

直接给传入的日期减等1

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

(14)重载后置++

先创建一个变量用来存储出入的日期,然后给传入的日期加等1,返回创建的新变量。

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

(15)重载后置--

先创建一个变量用来存储出入的日期,然后给传入的日期减等1,返回创建的新变量。

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

(16)重载-

先比较传入的两个日期判断天数符号,然后创建两个变量用来存储,传入的日期,将小日期循环加1直到两个日期相等。

int Date::operator-(const Date& d1)
{
    int falg = 1;
    if (*this < d1)
    {
        falg *= -1;
    }
    Date tmp = *this;
    Date tmp2 = d1;
    if (tmp > tmp2)
    {
        tmp = d1;
        tmp2 = *this;
    }
    int day = 0;
    while (tmp != tmp2)
    {
        tmp += 1;
        day++;
    }
    return day*falg;
}

(17)重载<<

std::ostream& operator<<(std::ostream& out, const Date& d)
{
    std::cout << d._year << "年" << d._month << "月" << d._day << "日" << std::endl;
    return out;
}

(18)重载>>

std::istream& operator>>(std::istream& in, Date& d)
{
    std::cin >> d._year >> d._month >> d._day;
    return in;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值