日期类的实现(c++版)

上篇博客简单的介绍了以下c++的几个默认成员函数,现在我们为了更好的理解这几个成员函数,实现一个简单的日期类。 日期类所用到的指知识点:

1、构造函数
2、拷贝构造函数
3、赋值运算符的重载
4、函数的复用 实现的功能:
1)、在当前基础下加一天,减一天后的日期
2)、在当前基础下加多天,减多天后的日期
3)、当前日期与要计算的日期相差多少天

代码实现:

Date.h*

#pragma once
#include<stdlib.h>
#include<assert.h>
#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        if (month<1||month>12||day<0||day>GetMonthDay(_year,_month))
        {
           assert(false);
        }
    }


        Date& operator=(const Date& d);

        bool IsInvalid();
        bool isLeapYear(int year);
        int GetMonthDay(int year, int month);
        void Show();
        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);//大于
        // d1 < d2 
        bool operator<(const Date& d);//小于
        // d1 + 10 
        Date operator+(int day);
        Date& operator+=(int day);
        Date operator-(int day);
        Date& operator-=(int day);
       int operator-(const Date& d);

        //++d1 
       Date& operator++();// 前置 
        //d1++ 
       Date operator++(int); // 后置 
       Date& operator--();//前置
       Date operator--(int);//后置
private:
    int _year;
    int _month;
    int _day;
};

Date.c

#include"Date.h"

 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 ((d._year<_year)
         ||(d._year==_year&&d._month<_month)
         || (d._year==_year&&d._month==_month&&d._day<_day))
     {
             return false;
     }
     return true;
 }
 //d1!=d2
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 || *this == d);
}
bool Date::operator>(const Date& d)//大于调用小于等于的复用
{
    return !(*this <= d);
}
bool Date:: isLeapYear(int year)//判断是否为闰年
{
    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
    {
        return true;
    }
    return false;
}
int Date::GetMonthDay(int year, int month)//获得这一天的天数
{

    int monthdays[13] = { 0, 31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31 };
    if (month==2&&isLeapYear(year))
    { 
        monthdays[2]= 29;
    }
    int day = monthdays[month];
    return day;

}
void Date::Show()//输出函数
{
    cout<< _year << "-"<< _month << "-" << _day<<endl;
}
Date Date:: operator+(int day)//日期加
{
    if (day<0)
    {
        return *this-(-day);
    }
    Date ret (*this);
    ret._day += day;
    while (ret._day>GetMonthDay(ret._year,ret._month))
    {
        ret._day -= GetMonthDay(ret._year, ret._month);
        ret._month++;
        if (ret._month==13)
        {
            ret._year++;
            ret._month = 1;
        }
    }
    return ret;
}
Date& Date:: operator+=(int day)//日期的加等
{
    *this = *this + day;
    return *this ;
}
Date Date::operator-(int day)//日期的减
{
    if (day<0)
    {
        return *this + (-day);
    }
    Date ret(*this);
    ret._day -= day;
    while (ret._day<=0)
    {
            ret._month--;   
        if (ret._month==0)
        {
            ret._year--;
            ret._month = 12;
        }
        ret._day += GetMonthDay(ret._year, ret._month);
    }
    return ret;

}
Date& Date:: operator-=(int day)//日期的减等
{
    return (*this -= day);
}
Date& Date::operator=(const Date& d)//赋值运算符的重载
{
    if ((*this)!= d)
    {
        this->_year = d._year;
        this->_month = d._month;
        this->_day = d._day;
    }
    return (*this);
}
int Date::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 (min<=max)
    {
        min++;
        day++;
    }
    return day*flag;


}
Date& Date:: operator++()//日期的自增(前置)
{
    *this = *this + 1;
    return *this;
}
Date Date:: operator++(int)//日期的自加(后置)
{
    Date tmp(*this);
    *this=*this+1;
    return tmp;
}
Date& Date::operator--()//日期的自减(前置)
{
    *this = *this-1;
    return *this;
}
Date Date::operator--(int)//日期的自减(后置)
{
    Date tmp(*this);
    *this = *this - 1;
    return(tmp);
}

test.cpp

#include"Date.h"
int main()
{
    Date d1(2018,5,3);
    //d1.Show();
    /*Date d2(2018,4,5);
    Date d3(2018 ,3,2);
    d2.Show();
    (d1 + 100).Show();
    (d1 +(- 4)).Show();
    (d1 - 100).Show();
    (d1).Show();
    --d1;
    d1.Show();
    (++d1).Show();
    (d1++).Show();
    d1=d2=d3;*/
    Date d2(2018, 3, 5);
    d2.Show();
    cout << (d1 - d2) << endl;
    system("pause");
    return 0;
}

可以去这个在线日期计算器进行验证;
http://beijing-time.org/riqi.htm

猜你喜欢

转载自blog.csdn.net/adzn1/article/details/79760510
今日推荐