1. 程式人生 > >C++(五)類和物件的應用(日期類實現)

C++(五)類和物件的應用(日期類實現)

可以實現日期之間的加加,減減,大小比較等
直接看程式碼

函式的宣告

#pragma once
#include <iostream>
using namespace std;

class Date{
public:
    Date(int year = 1900,int month = 1,int day = 1);//建構函式
    Date(const Date& d);//拷貝建構函式

    void DatePrint();
    int getmonthday(int year,int month);

    Date& operator=(Date d);//核心
Date& operator+=(int day);//核心 Date operator+(int day); Date& operator-=(int day);//核心 Date operator-(int day); int operator-(const Date& d); bool operator>(const Date& d);//核心 bool operator==(const Date& d);//核心 bool operator<(const Date& d);//核心
bool operator>=(const Date& d); bool operator<=(const Date& d); bool operator!=(const Date& d); Date& operator++(int);//前置 Date operator++();//後置 Date& operator--(int);//前置 Date operator--();//後置 ~Date(); private: int _year; int _month; int
_day; };

函式的實現

#include "date.h"

Date::Date(int year,int month,int day)//建構函式
{
    if(year >= 1900 && month>1 && month<13 && day>0 &&\
       day<=getmonthday(year,month) )
    {
        this->_year = year;
        this->_month = month;
        this->_day = day;
    }
}

Date::Date(const Date& d)//拷貝建構函式 
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
}

void Date::DatePrint()
{
    cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}

int Date::getmonthday(int year,int month)
{
    static int month_days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int day = month_days[month];

    if(month == 2 &&((year%4 == 0 && year%100 != 0) || year%400 == 0))
    {
        day +=1;
    }
    return day;
}

Date& Date::operator=(Date d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}

Date& Date::operator-=(int day)
{
    while(_day <= day)
    {
        if(--_month < 0)
        {
            --_year;
            _month = 12;
        }
        _day += getmonthday(_year,_month);
    }
    _day -= day;
    return *this;
}

Date Date::operator-(int day)
{
    Date tmp(*this);
    tmp -= day;
    return tmp;
}

Date& Date::operator+=(int day)
{
    _day += day;
    while(_day > getmonthday(_year,_month))
    {
        _day -= getmonthday(_year,_month);
        _month++;
        if(_month == 13)
        {
            _year++;
            _month == 1;
        }
    }
    return *this;
}

Date Date::operator+(int day)
{
    Date tmp(*this);
    tmp += day;
    return tmp;
}

int Date::operator-(const Date& d)
{
    Date min(*this);
    Date max(d);
    int flag = 1;
    if(*this > d)
    {
        min = d;
        max = *this;
        flag = -1;
    }
    int day = 0;
    while(min != max)
    {
        ++day;
        min += 1;
    }
    return flag * day;
}

Date& Date::operator++()//前置++
{
    *this += 1;
    return *this;
}

Date Date::operator++(int)//後置++
{
    Date tmp(*this);
    *this += 1;
    return tmp;
}

Date& Date::operator--()
{
    *this--;
    return *this;
}

Date Date::operator--(int)
{
    Date tmp(*this);
    *this--;
    return tmp;
}

bool Date::operator>(const Date& d) 
{
    if(_year == d._year)
    {
        if(_month == d._month)
        {
            if(_day > d._day)
            {
                return true;
            }
        }
        else if(_month > d._month)
        {
            return true;
        }
    }
    else if(_year > d._year)
    {
        return true;
    }
    return false;
}

bool Date::operator==(const Date& d)
{
    if(_year == d._year && _month == d._month && _day == d._day)
    {
        return true;
    }
    return false;
}

bool Date::operator<(const Date& d)
{
    if(_year == d._year)
    {
        if(_month == d._month)
        {
            if(_day < d._day)
            {
                return true;
            }

        }
        else if(_month < d._month)
        {
            return true;
        }
    }
    else if(_year < d._year)
    {
        return true;
    }
    return false;
}

bool Date::operator>=(const Date& d)
{
    return !(*this < d);
}

bool Date::operator<=(const Date& d)
{
    return !(*this > d);
}

bool Date::operator!=(const Date& d)
{
    return !(*this == d);
}

Date::~Date()
{
    _year = 0;
    _month = 0;
    _day = 0;
}

int main()
{
    Date d1;
    cout<<&d1<<endl;
    d1 += 3;
    cout<<&d1<<endl;
}