实现代码:

int getmonthday(int year, int month )

{

         int montharr[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

         int day = montharr[month ];

         if (month == 2 && leapyear(year))

        {

                day = day + 1;

        }

         return day;

}

 bool leapyear( int year )

{

         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

        {

                 return true ;

        }

         return false ;

}






#ifndef _CALENDAR__H _

#include

#include

using namespace std;

class calendar

{

         friend bool leapyear(int year);

         friend int getmonthday(int year, int month);

public:

         void SetDate(int year, int month , int day)

        {

                _year = year;

                _month = month;

                _day = day;

        }

        calendar( int year = 1990, int month = 1, int day = 1)

        {

                 if (year >= 1990 && month <= 12 && month > 0 && day > 0 && day <= getmonthday(year, month))

                {

                        _year = year;

                        _month = month;

                        _day = day;

                }

                 else

                {

                        cout << "输入错误" << endl;

                        _year = 1990;

                        _month = 1;

                        _day = 1;

                }

        }

        calendar( calendar &d )

        {

                _year = d._year;

                _month = d._month;

                _day = d._day;

        }

         void display()

        {

                cout << _year << "-" << _month << "-" << _day << endl;

        }

private:

         int _year;

         int _month;

         int _day;

public:

         bool operator==(const calendar& d)

        {

                 return (_year == d ._year &&_month == d._month&&_day == d ._day);

        }

         bool operator!=(const calendar& d)

        {

                 return !(*this == d);

        }

         bool operator>(const calendar& d)

        {

                 return (_year > d ._year&&_month > d._month&&_day > d ._day);

        }

         bool operator>=(const calendar& d)

        {

                 return (*this > d || * this == d );

        }

         bool operator<(const calendar& d)

        {

                 return !(*this > d || * this == d );

        }

         bool operator<=(const calendar& d)

        {

                 return !(*this > d);

        }

         calendar& operator--()

        {

                * this -= 1;

                 return *this ;

        }

         calendar operator--(int )

        {

                 calendar temp(*this );

                * this -= 1;

                 return temp;

        }

         calendar& operator++()

        {

                * this += 1;

                 return *this ;

        }

         calendar operator++(int )

        {

                 calendar temp(*this );

                * this += 1;

                 return temp;

        }

         calendar operator+(int day)

        {

                 calendar temp(*this );

                 if (day <0)

                {

                         return *this - (-day);

                }

                temp._day += day;

                 while (temp._day > getmonthday(temp._year, temp._month))

                {

                        temp._day -= getmonthday(temp._year, temp._month);

                         if (temp._month == 12)

                        {

                                temp._year++;

                                temp._month = 1;

                        }

                         else

                        {

                                temp._month++;

                        }

                }

                 return temp;

        }

         calendar operator-(int day)

        {

                 calendar temp(*this );

                 if (day < 0)

                {

                         return *this + (-day);

                }

                temp._day -= day;

                 while (temp._day<=0)

                {

                         if (temp._month == 1)

                        {

                                temp._year--;

                                temp._month = 12;

                        }

                         else

                        {

                                temp._month--;

                        }

                        temp._day += getmonthday(_year, _month);

                }

                 return temp;

        }

         calendar operator-=(int day)

        {

                * this = *this - day;

                 return *this ;

        }

         calendar operator+=(int day)

        {

                * this = *this + day;

                 return *this ;

        }

         int operator-( calendar &d)

        {

                 int flag = 1;

                 calendar max = *this ;

                 calendar min = d ;

                 if (max < min)

                {

                        std::swap(max._year, min._year);

                        std::swap(max._month, min._month);

                        std::swap(max._day, min._day);

                        flag = -1;

                }

                 int count = 0;

                 while (max != min)

                {

                        ++count;

                        ++min;

                }

                 return (count*flag);

        }

};

#endif// _CALENDAR__H _