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

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

class Date
{

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

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

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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/mmwwxx123/article/details/81706789