1. 程式人生 > >【C++】自定義一個Date類

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

這裡寫圖片描述