C++(五)类和对象的应用(日期类实现)

5 篇文章 0 订阅

可以实现日期之间的加加,减减,大小比较等
直接看代码

函数的声明

#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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值