C++实现一个日期类

函数实现部分:

#include "Date.h"
void Date::display()
{
    cout << _year << "-" << _month << "-" << _day << endl;
}


int GetMonthDay(int year, int month)
{
    static int MonthDay[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (month == 2)
    {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        {
            return MonthDay[month] - 1;
        }
    }
         return MonthDay[month];
}



bool Date::IsLegal()
{
    if ((_year >= 0) && (_month > 0) && (_month
        < 13) && (_day>0) && (_day <= GetMonthDay(_year, _month)))
    {
        return true;
    }
    else
    {
        return false;
    }
}


Date Date:: operator+(int day)   //不改变初始日期的值
{
    if (day < 0)
    {
        return *this - (-day);
    }
    Date tmp = *this;
    tmp._day += day;
    while (tmp._day > GetMonthDay(tmp._year, tmp._month))
    {
        tmp._day = tmp._day - GetMonthDay(tmp._year, tmp._month);
        if (tmp._month == 12)
        {
            tmp._month = 1;
            tmp._year += 1;
        }
        else
        {
            tmp._month += 1;
        }
    }
    return  tmp;
}

Date & Date::operator+=(int day)
{
    *this = *this + day;    //代码的复用
    return *this;            
}

Date Date::operator-(int day)  //减去多少天,不改变初始日期的值
{
    if (day < 0)  //d1-(-100)
    {
        return *this + (-day);
    }
    Date tmp = *this;
    tmp._day = tmp._day - day;
    while (tmp._day <= 0)
    {
        if (tmp._month == 1)
        {
            tmp._month = 12;
            tmp._year -= 1;
        }
        else
        {
            tmp._month -= 1;
        }
        tmp._day = tmp._day + GetMonthDay(tmp._year, tmp._month);
    }
    return tmp;
}

bool Date:: operator==(const Date&d) //比较日期大小
{
    if ((_year != d._year) || (_month != d._month) || (_day != d._day))
    {
        return  false;
    }
    return  true;
}

bool Date:: operator!=(const Date&d) //比较日期大小
{
    return !((*this).operator==(d));
}

Date & Date :: operator-=(int day)  //减去多少天,改变初始日期的值
{
    *this = *this - day;
    return *this;
}

bool Date::operator> (const Date&d)  //比较日期大小
{
    if ((this->_year>d._year) 
        || ((this->_month>d._month)&&(this->_year==d._year))
        ||((this->_day>d._day)&&(this->_month==d._month)&&(this->_year==d._year)))          
    {
        return true;
    }
    return false;
}

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

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

int  Date::operator-(const Date&d)   //两个日期相减相差多少天
{
    Date FutureDay;
    Date PastDay;
    int i = 0;
    if (*this > d)
    {
         FutureDay = *this;
         PastDay = d;
    }
    else
    {
         FutureDay = d;
         PastDay = *this;
    }
    while (FutureDay != PastDay)
    {
        PastDay += 1;
        i++;
    }
    return i;
}

头文件声明部分:

#define _CRT_SECURE_NO_WARNINGS 
#ifndef  __DATE_CLASS__
#define __DATE_CLASS__

#include <iostream>
#include <windows.h>
#include <assert.h>
using namespace std;
class Date
{
public:
    Date(int year=1995,int month=5 ,int day=6)
    :_year(year)
    , _month(month)
    , _day(day)
    {
        assert(IsLegal());
    }

    void display();  //打印日期

    Date operator+(int day);  //加上多少天,不改变初始日期的值

    Date & operator+=(int day);  //加上多少天,改变初始日期的值

    Date operator-(int day);  //减去多少天,不改变初始日期的值

    Date & operator-=(int day);  //减去多少天,改变初始日期的值

    bool operator>(const Date&d);  //比较日期大小

    bool operator==(const Date&d);  //比较日期大小

    bool operator!=(const Date&d);  //比较日期大小

    Date operator++(int);     //date++;

    Date & operator++();       //++date;

    int operator-(const Date&d);   //两个日期相减相差多少天

    bool IsLegal();    //判断日期是否合法
private:
    int _year;
    int _month;
    int _day;
};

测试用例:

#include "Date.h"
int main()
{
    Date d(2017,3,8);
    Date d1 = d + 100;
    d.display();
    d1.display();
    Date d2(2017, 3, 8);
    d2 += 100;
    d2.display();
    Date d3 = d2 - (-100);
    d3.display();
    Date d4 = d3 + (-100);
    d4.display();
    int k=d3 > d4;
    cout << k << endl;
    Date d5 = d4;
    (d5++).display();
    (++d5).display();
    int a = d3 - d4;
    cout << a << endl;
    system("pause");
    return 0;
}

运行结果:
这里写图片描述

总结:
在实现日期类的时候要注意代码的复用+简洁,以及什么时候能够返回一个值的引用来使函数的效率更高(不能返回指向栈内存的引用和指针),这些都是需要注意的,以及函数的重载的理解(函数名可以相同,参数的类型不同或者参数的个数不同(参数列表不同),返回值可同可不同)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值