3.1C++:运算符重载实现一个完善日期类

        运算符重载是为了让自定义类型可以像内置类型一样去使用运算符。

实现自定义类型之间的运算符比较,只定义小于和等于,即可实现小于等于、大于、大于等于、不等于(同理只定义大于和等于也可实现其他)

#include <iostream>
using namespace std;
class Date
{
public:
    int GetMonthDay(int year, int month)
    {
        static  int monthDay[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 monthDay[month];
    }
    //小于
    bool 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 operator==(const Date& d)
    {
        return _year == d._year && _month == d._month && _day == d._day;
    }
    //只有小于和等于自己写,其他全部复用小于和等于
    //小于等于
    bool operator<=(const Date& d)
    {
        return *this < d || *this == d; //复用小于和等于,d1 < d2 || d1 == d2;
    }
    //大于等于
    bool operator>=(const Date& d)
    {
        return !(*this < d); //不是小于
    }
    //大于
    bool operator>(const Date& d)
    {
        return !(*this < d || *this == d);  //不是小于等于
    }
    //不等于
     bool operator !=(const Date& d)
    {
        return !(*this == d);  //不是等于
    }

     //"+"   //d1+10:d1本身不发生变化;d1+=10:d1本身加10
     Date operator+(int day)
     {
         Date ret(*this);
         ret._day += day;
         while (ret._day > GetMonthDay(ret._year, ret._month))
         {
             ret._day -= GetMonthDay(ret._year, ret._month);
             ret._month++;
             if (ret._month == 13)
             { 
                 ret._year++;
                 ret._month = 1;
             }       
         }
         return ret;  //d1本身不变化
     }
     //"+="
     Date& operator+=(int day)
     {
         _day += day;
         while (_day > GetMonthDay(_year, _month))
         {
             _day -= GetMonthDay(_year, _month);
             _month++;
             if (_month == 13)
             {
                 _year++;
                 _month = 1;
             }
         }
         return *this;  //d1本身变化
     }
     //"-="     
     Date& operator-=(int day)
     {
         while (_day < day)
         {
             if (_month == 1)
             {
                 _year--;
                 _month = 12;
             }
             else
                 _month--;
             _day += GetMonthDay(_year, _month);
         }
         _day -= day;
         return *this;  
     }
     //"-"     
     Date operator-(int day)
     {
         Date ret(*this);
         while (ret._day < day)
         {
             if (ret._month == 1)
                 {
                     ret._year--;
                     ret._month = 12;
                 }
              else
                 ret._month--;
         ret._day += GetMonthDay(ret._year, ret._month);
         }
         ret._day -= day;
         return ret;  
     }
     //"++"     
     Date& operator++()
     {
         _day ++;
         while (_day > GetMonthDay(_year, _month))
         {
             _day -= GetMonthDay(_year, _month);
             _month++;
             if (_month == 13)
             {
                 _year++;
                 _month = 1;
             }
         }
         return *this;  //d1本身变化
     }
     //"--"     
     Date& operator--()
     {
         if (_day == 1)
         {
             if (_month == 1)
             {
                 _year--;
                 _month = 12;
             }
             else
                 _month--;
             _day = GetMonthDay(_year, _month);
         }
         else
            _day --;
         return *this;
     }
    //初始化
    Date(int year = 0, int month = 1, int day = 1)
    {
        if (year >= 0 && 
            month >= 1 && month <= 12 && 
            day >= 1 && day <= GetMonthDay(year, month))
        {
            _year = year;
            _month = month;
            _day = day;
        }
        else
        {
            cout << "非法日期" << endl;
        }
    }
    //打印
    void print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    Date d1(2015, 2, 1);
    Date d2(2015, 1, 1); 
    cout << (d1 == d2) << endl; //0
    cout << (d1 != d2) << endl; //1
    cout << (d1 >= d2) << endl;//1
    cout << (d1 <= d2) << endl;//0
    cout << (d1 > d2) << endl;//1
    cout << (d1 < d2) << endl;//0

    d1 - 10;
    d1 -= 10;
   Date d4 =  d1 - 10;
   d4.print();
   d1 += 30;
   d1.print();
   Date d5 = --d1;
   d5.print();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值