模拟实现日期类

来源:互联网 发布:七年级英语听力软件 编辑:程序博客网 时间:2024/06/05 16:49

日期类好久没写了,重温一下以前的知识。

写日期类需要注意的有:

1、日期减日期的计算
2、关于输出输入重载的友元函数声明
3、构造函数的条件判断
4、拷贝构造函数的自我赋值判断

实现代码如下:

#include <iostream>using namespace std;class Date{public:    //4个默认的成员函数    //构造、拷贝构造、赋值语句重载、析构函数    //构造函数    Date(int  year = 1900, int month = 1, int  day = 1)        :_year(year)        , _month(month)        , _day(day)    {        if ((year >= 1900)            && ((month >= 1) && (month <= 12))            && (day >= 1 && day <= GetMonthDay(year, month)))        {            _year = year;            _month = month;            _day = day;        }        else        {            cout << "日期非法!" << endl;            _year = 1900;            _month = 1;            _day = 1;        }    }    //拷贝构造函数    Date(const Date&d)    {        _year = d._year;        _month = d._month;        _day = d._day;    }    //赋值语句重载    //此处需要主要自我赋值    Date& operator=(const Date& d)    {        if (this != &d)        {            _year = d._year;            _month = d._month;            _day = d._day;        }        return *this;    }    //析构函数    ~Date()    {        cout << "~Date!" << endl;    }public:    //日期比较    bool operator==(const Date& d)    {        return ((_day == d._day)            && (_year == d._year)            && (_month == d._month));    }    bool operator!=(const Date& d)    {        return (!(*this == d));    }    bool operator>(const Date& d)    {        return (_year >d._year)              || (_year == d._year) && (_month > d._month)            || (_year == d._year) && (_month == d._month) && (_day > d._day);    }    bool operator<(const Date& d)    {        return (_year <d._year)            || (_year == d._year) && (_month < d._month)            || (_year == d._year) && (_month == d._month) && (_day < d._day);;    }    bool operator>=(const Date& d)    {        return (*this == d) || (*this > d);    }    bool operator<=(const Date& d)    {        return (*this == d) || (*this < d);    }public:    Date& operator++()    {        _day++;        if (_day > GetMonthDay(_year, _month))        {            _day = GetMonthDay(_year, _month) - _day;            _month++;            if (_month > 12)            {                _year++;                _month = 1;            }        }        return *this;    }    Date operator++(int)    {        Date temp(*this);        _day++;        if (_day > GetMonthDay(_year, _month))        {            _day = GetMonthDay(_year, _month) - _day;            _month++;            if (_month > 12)            {                _year++;                _month = 1;            }        }        return temp;    }    Date& operator--()    {        _day--;        if (_day < 0)        {            _month--;            if (_month < 1)            {                _year--;                _month = 12;            }            _day += GetMonthDay(_year, _month);        }        return *this;    }    Date operator--(int)    {        Date temp(*this);        _day--;        if (_day < 0)        {            _month--;            if (_month < 1)            {                _year--;                _month = 12;            }            _day += GetMonthDay(_year, _month);        }        return temp;    }    //日期+天数    Date operator+(int day)    {        Date temp(*this);        if (day < 0)            return temp - (-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;    }    //日期-天数    Date operator-(int day)    {        Date temp(*this);        if (day < 0)            return temp + (-day);        temp._day -= day;        while (temp._day <= 0)        {            if (temp._month == 1)            {                temp._year--;                temp._month = 12;            }            else                --temp._month;            temp, _day += GetMonthDay(temp._year, temp._month);        }        return  temp;    }    //日期-日期    int operator-(const Date& d)    {        Date Max(*this);        Date Min(d);        int flag = 1;        if (*this < d)        {            Max = d;            Min = *this;            flag = -1;        }        int day = 0;        while (Max != Min)        {            Max--;            day++;        }        return day*flag;    }    //注意此处的友元    friend ostream& operator<<(ostream& os, const Date& d);    //输出      friend istream& operator >> (istream& is, Date& d);         //输入      int GetMonthDay(int year, int month)    {        int a[] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };        int day;        if ((!(year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))) && month == 2)        {            day = a[month] - 1;        }        day = a[month];        return day;    }private:    int _year;//年    int _month;//月    int _day;//日};ostream& operator<<(ostream& os, const Date& d){    os << d._year << "-" << d._month << "-" << d._day << endl;    return os;}istream& operator >> (istream& is, Date& d){    cout << "输入日期:";    is >> d._year;    is >> d._month;    is >> d._day;    return is;}int main(){    Date d1(2017, 10, 25);    Date  d2(2017, 12, 1);    cout << "d1为:" << d1;    cout << "d2为:" << d2;    int ret = d2 - d1;    cout << "两个日期相减为:" << ret << endl;    Date  d3 = d2 + 1;    cout << "d3为:" << d3;    Date d4(d2);    cout << "d4为:" << d4;    Date d5 = d1 - 1;    cout << "d5为:" << d5;    Date d6 = d1++;    cout << "d6为:" << d6;    Date d7;    cin >> d7;    cout << d7;    Date d(2017, 3, 15);    d = d + 20;    cout << "d为:" << d << endl;    return 0;}
原创粉丝点击