date類實現操作符重載

日期類對操作符重載鞏固
data.h

#ifndef  _data_h
#define _data_h
class Date
{
private:
    int _year;
    int _month;
    int _day;
public:
    //默認成員函數
    Date(int year = 1900, int month = 1, int day = 1);//缺省構造函數
    Date(const Date& c);//拷貝構造函數
    Date& operator=(const Date& c);//賦值操作符重載
    ~Date();//析構函數
    void display();
    int  GetMonthDay(int year, int month);//獲得當月天數
    friend ostream & operator<<(ostream& out, Date& d);//重載<<
    friend istream & operator>>(istream& in, Date& d);//重載>>
    Date operator+(int day);//日期對象加上一個天數
    Date operator-(int day);//日期對象減上一個天數
    int  operator-(Date  d);//兩個日期對象相減求天數
    Date& operator++();//日期前置++
    Date operator++(int);//日期後置++
    Date AddOne(const Date& d);//日期加一天
    Date& operator+=(int day);//日期對象加等
    Date& operator-=(int day);//日期對象減等

    bool operator<(const Date& d);//比較*this對象是否比d日期對象小
    bool operator>(const Date& d);//比較*this對象是否比d日期對象大
    bool operator==(const Date& d);//比較*this對象與d日期對象是否相等
    bool operator<=(const Date& d);//比較*this對象是否比d日期對象小於等於
    bool operator>=(const Date& d);//比較*this對象是否比d日期對象大於等於
};

#endif
=========
#include <iostream>
#include "data.h"
using std::ostream;
using std::istream;
using std::endl;
using std::cout;

Date::Date(int year , int month , int day)//默認參數在頭文件中定義一次就好了,無需再定文件中再寫一次
{
    if (year<1900)
    {
        cout << "year error" << endl;
        return;
    }
    if (month <1 && month > 12)
    {
        cout << "month error" << endl;
        return;
    }
    if (day<1 || day>GetMonthDay(year, month))
    {
        cout << "day error" << endl;
        return;
    }
    //已確認日期有效
    _year = year;
    _month = month;
    _day = day;
    //cout << "構造函數" << endl;
}
Date::Date(const Date& c)
{
    _year = c._year;
    _month = c._month;
    _day = c._day;
    //cout << "拷貝構造" << endl;
}
Date& Date::operator=(const Date& c)//&表引用
{
    if (this != &c)//&表取地址
    {
        _year = c._year;
        _month = c._month;
        _day = c._day;
    }
    //cout << "賦值" << endl;
    return *this;
}
Date:: ~Date()
{
    //cout << "析構函數" << endl;
}
void Date::display()
{
    cout << _year << "-" << _month << "-" << _day << endl;
}
ostream& operator<<(ostream& out, Date& d)
{
    out << d._year << d._month << d._day;
    return out;
}
istream& operator>>(istream& in, Date& d)
{
    in >> d._year >> d._month >> d._day;
    return in;
}
int  GetMonthDay(int year, int month)
{
    int monthDay[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if ((year % 400 == 0 || year % 4 == 0 && year % 100 != 0) && month == 2)//是瑞年且2月
    {
        return monthDay[month - 1] + 1;
    }
    else
        return  monthDay[month - 1];
}
Date Date::operator+(int day)//加類域::
{
    Date tmp(*this);
    if (day < 0)
    {
        return tmp - (-day);//加一個負天等於減一個正天
    }
    tmp._day += day;
    while (tmp._day>GetMonthDay(tmp._year, tmp._month))//注意應是tmp對象和tmp._day比較
    {
        tmp._day -= GetMonthDay(tmp._year, tmp._month);
        if (tmp._month == 12)
        {
            ++tmp._year;
            tmp._month = 1;
        }
        else
        {
            ++tmp._month;
        }
    }
    return tmp;
}
Date Date:: operator-(int day)
{
    Date tmp(*this);
    if (day < 0)
    {
        return tmp + (-day);//加一個負天等於減一個正天
    }
    tmp._day -= day;
    while (tmp._day<1)//注意邊界條件,一個月最小天數爲1
    {
        if (tmp._month == 1)
        {
            --tmp._year;
            tmp._month = 12;
        }
        else
        {
            --tmp._month;
        }
        tmp._day += GetMonthDay(tmp._year, tmp._month);
    }
    return tmp;
}
int Date:: operator-(Date  d)
{
    int count = 0;
    int flag = -1;
    Date *pMax = &d;
    Date *pMin = this;
    Date tmp(*this);
    if (*this > d)
    {
        flag = 1;
        swap(pMax, pMin);
    }
    while (*pMin < *pMax)
    {
        ++(*pMin);
        ++count;
    }
    if (flag == -1)
        *this = tmp;//因爲已修改this,需改回去
    return flag*count;
}
Date& Date::operator++()
{
    *this = *this + 1;
    return *this;
}
Date Date:: operator++(int)
{
    Date tmp(*this);
    *this = *this + 1;
    return tmp;
}
Date AddOne(const Date& d)
{
    Date tmp(d);
    tmp = tmp + 1;
    return tmp;
}
Date& Date::operator+=(int day)
{
    *this = *this + day;
    return *this;
}
Date& Date::operator-=(int day)
{
    *this = *this - day;
    return *this;
}

bool Date::operator<(const Date& d)
{
    if (_year<d._year)
    {
        return true;
    }
    else if (_year == d._year)
    {
        if (_month<d._month)
        {
            return true;
        }
        else if (_month == d._month)
        {
            if (_day<d._day)
            {
                return true;
            }
        }
    }
    return false;
}
bool Date:: operator>(const Date& d)
{
    return !(*this < d);
}
bool Date::operator==(const Date& d)
{
    return _year == d._year&&_month == d._month&&_day == d._day;
}
bool Date::operator<=(const Date& d)
{
    return (*this < d) || (*this == d);
}
bool Date::operator>=(const Date& d)
{
    return !(*this < d);
}
========
Testc.c
#include <iostream>
#include "data.h"
using namespace std;

void Test1()
{
    Date a(1993, 5, 6);
    Date b = a + 1000;
    b.display();
}


int main()
{
    Test1();
    system("pause");
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章