【C++】自定义一个Date类

学习了C++的一些类的默认成员函数,运算符重载等内容后,自已定义并实现了一个较为完整的Date类:
test.cpp:

class Date
{
 friend ostream& operator<<(ostream& _cout, const Date& d);
public:
     Date(int year=1900, int month=1, int day=1)//构造函数
    {
         _year = year;
         _month = month;
         _day = day;
    }
     void showDate()
     {
         cout << _year <<"-"<< _month<<"-"<< _day<< endl;
     }
     ~Date()//析构函数
     {

     }
     Date(const Date& d)//拷贝构造函数
     {
         _year = d._year;
         _month = d._month;
         _day = d._day;
     }
     Date& operator=(const Date& d)//赋值运算符重载
     {
         if (this != &d)
         {
             this->_year = d._year;
             this->_month = d._month;
             this->_day = d._day;
         }
         return *this;
     }

     int IsLeapYear(int year)//判断是否闰年
     {
         if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
         {
             return 1;
         }
         return 0;
     }
     int GetDayByYearAndMonth(int year,int month, int day)//计算当前月份所在天数
     {
         int month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
         if (IsLeapYear(year) && month == 2)
         {
             return 29;
         }
         return month_day[month];
     }
     Date operator+(int days)//计算当前日期day天之后日期
     {
         Date tmpData(*this);
         tmpData._day += days;
         while (tmpData._day > GetDayByYearAndMonth(tmpData._year, tmpData._month, tmpData._day))
         {
             tmpData._day = tmpData._day - GetDayByYearAndMonth(tmpData._year, tmpData._month, tmpData._day);
             tmpData._month += 1;
             if (tmpData._month > 12)
             {
                 tmpData._year += 1;
                 tmpData._month = 1;
             }
         }
         return tmpData;
     }
     Date& operator+=(int days)
     {
         (*this) = (*this) + days;
         return *this;
     }
     Date operator-(int days)//计算当前日期day天之前日期
     {
         Date tmpDate(*this);
         tmpDate._day -= days;
         while (tmpDate._day <= 0)
         {
             tmpDate._month -= 1;
             if (tmpDate._month <=0)
             {
                 tmpDate._year-= 1;
                 tmpDate._month = 12;
             } 
             tmpDate._day = tmpDate._day + GetDayByYearAndMonth(tmpDate._year, tmpDate._month, tmpDate._day);
         }
         return tmpDate;
     }
     Date& operator-=(int days)
     {
         (*this) = (*this) - days;
         return *this;
     }

     Date& operator++()//前置++
     {
         *this += 1;
         return *this;
     }
     Date operator++(int)//后置++
     {
         Date tmp(*this);
         *this += 1;
         return tmp;
     }
     Date& operator--()//前置--
     {
         *this -= 1;
         return *this;   
     }
     Date operator--(int)//后置--
     {
         Date tmp(*this);
         *this -= 1;
         return tmp;
     }

     bool operator==(const Date& d)//两个日期类D1 == D2
     {
         return ((_year == d._year) && (_month == d._month) && (_day == d._day));
     }
     bool operator>(const Date& d)//两个日期类D1 > D2
     {
         return ((_year > d._year) || ((_year == d._year) && (_month > d._month)) || ((_year == d._year) && (_month == d._month) && (_day > d._day)));
     }
     bool operator>=(const Date& d)//两个日期类D1 > = D2
     {
         return ((*this) == d) || ((*this)>d);
     }
     bool operator<(const Date& d)//两个日期类D1 < D2
     {
         return !((*this) >= d);
     }
     bool operator<=(const Date& d)//两个日期类D1 <= D2
     {
         return !((*this)>d);
     }
     bool operator!=(const Date& d)//两个日期类D1 ! = D2
     {
         return !((*this) == d);
     }
     int operator-(Date& d)//两个日期相差的天数
     {
         Date max = (*this);
         Date min = d;
         if (d > *this)
         {
             max = d;
             min = (*this);

         }
         int count = 0;
         while (max != min)
         {
             min++;
             count++;
         }
         return count;
     }

private:
    int _year;
    int _month;
    int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)//输出运算符重载
{
    _cout << d._year << "-" << d._month << "-" << d._day;
    return _cout;
}

以下是相关的一些测试代码:

int main()
{
    Date date1(2018, 8, 15);
    Date date2 = date1;
    date2.showDate();

    Date date3 =date1+200;
    date3.showDate();

    Date date4 = date2-300;
    date4.showDate();

    int a = date3 - date2;
    cout << a << endl;

    Date d2(2017, 10, 20);
    int i = 5;
    i = (date1 <= d2);
    cout << i << endl;
    system("pause");
    return 0;
}

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值