C++——Date功能实现

简介

因为Date各个功能的实现,都需要用到 类的构造函数,拷贝构造函数以及赋值重载,所以完成Date的各个功能是对 类的构造函数,拷贝构造函数以及赋值重载一个非常好的练习

一,Date.h

在声明部分,需要注意的不多,主要是 缺省参数的缺省值要在声明部分写,以及public和private的权限问题。
#include <iostream>
using namespace std;

class Date
{
public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month)
    {
        static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
       31 };
        int day = days[month];
        if (month == 2
            && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
        {
            day += 1;
        }
        return day;
    }

    // 全缺省的构造函数
    Date(int year = 2023, int month = 2, int day = 28);
    // 拷贝构造函数
    // d2(d1)
    Date(const Date& d);
    // 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
    Date& 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++();
    // 后置++
    Date operator++(int);
    // 后置--
    Date operator--(int);
    // 前置--
    Date& operator--();
    // 日期+=天数
    Date& operator+=(int day);
    // 日期+天数
    Date operator+(int day);
    // 日期-天数
    Date operator-(int day);
    // 日期-=天数
    Date& operator-=(int day);
    // 日期-日期 返回天数
    int operator-(const Date& d);
    void print();
    
private:
    int _year;
    int _month;
    int _day;
};

二,Date.cpp

功能实现的方面,每个功能所需要注意的点都要详细的标注,其中有不少功能可以进行 复用,这使我们的代码简洁许多,以及关于 引用返回和引用传参,只要是函数销毁后对象仍存在我们都建议引用返回,这样少了一个拷贝的过程,可以有效提高效率,以及引用传参也是相同的道理
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
//构造函数
Date::Date(int year, int month, int day)
{
    //判断日期是否合法
    if ((month > 0 && month < 13) && (day > 0 && day < GetMonthDay(year, month)))
    {
        _year = year;
        _month = month;
        _day = day;
    }
    else
    {
        perror("日期不合法");
        exit(-1);
    }
    
}
//拷贝构造函数
Date::Date(const Date& d)//用const限定是因为,d是用来拷贝的,防止出现改变d的情况
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
}
//=
Date& Date::operator=(const Date& d)
{
    //防止自己给自己赋值
    if (this != &d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    return *this;
}
//>
bool Date::operator>(const Date& d)
{
    //将满足>的条件全部写出来,剩下的就都是不满足的
    //1.可读性强的写法
    if (_year > d._year)
    {
        return true;
    }
    else if ((_year == d._year) && (_month > d._month))
    {
        return true;
    }
    else if ((_year == d._year) && (_month == d._month) && (_day > d._day))
    {
        return true;
    }
    else
    {
        return false;
    }
    2.简洁的写法
    //return (_year > d._year)
    //    || ((_year == d._year) && (_month > d._month))
    //    || ((_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) || (*this == d));
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
    return !(*this > d) || (*this == d);
}
// !=运算符重载
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;
}
//前置++
Date& Date::operator++()
{
    _day += 1;
    return *this;
}
//后置++
Date Date::operator++(int)
{
    Date tmp(*this);
    _day += 1;
    return tmp;
}
// 后置--
Date Date::operator--(int)
{
    Date tmp(*this);
    _day -= 1;
    return tmp;
}
// 前置--
Date& Date::operator--()
{
    _day -= 1;
    return *this;
}
// 日期+=天数
Date& Date::operator+=(int day)
{
    //针对a+=-100的情况,我们可以复用-=
    if (day < 0)
    {
        *this -= -day;
        return *this;
    }
    _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;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
    //针对a-=-100的情况,我们可以复用+=
    if (day < 0)
    {
        *this += -day;
        return *this;
    }
    _day -= day;
    while (_day <= 0)
    {
        //当day小于0,向前借月
        //此时需要注意借的天数是,本月往前推一个月的天数
        //例如现在是1月,我们要借月,借的应该是前一年的12月的天数,如果按照之前+=的方法处理,
        //就会出现0月的情况,所以我们调整一下顺序
        
        _month--;
        if (_month == 0)
        {
            _year--;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
    Date tmp(*this);
    //复用日期-=天数
    tmp -= day;
    return tmp;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
    //定义一个变量计算差值
    int count = 0;
    //创建临时Data防止*this数据改变
    Date tmp(*this);
    if (tmp > d)
    {
        while (tmp != d)
        {
            tmp -= 1;
            count++;
        }

    }
    else
    {
        while (tmp != d)
        {
            tmp += 1;
            count--;
        }
    }
    return count;
}
void Date::print()
{
    cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

总结

Date是对 类的构造函数,拷贝构造函数以及赋值重载一个非常好的练习,如果可以独立将这个功能写出来,那么对类的默认成员函数的理解差不多就有七七八八了。
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值