C++日期类(Date)实现的示例代码

 更新时间:2022年07月11日 15:22:55   作者:繁华的梦境  
这篇文章主要为大家详细介绍了如何利用C++语言实现日期类(Date),可以实现确定某年某月有多少天、打印日期等功能,感兴趣的可以了解一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

类的定义

#pragma once
#include < iostream>
using std::cout;
using std::cin;
using std::endl;
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 全缺省的构造函数
	Date(int year = 0, int month = 1, int day = 1);
	void Print();
    //析构,拷贝构造,赋值重载函数可以不用写,默认生成的就够用
	//日期+=天数
	Date& operator+=(int day);

	// 日+天数
	Date operator+(int day);

	// 日期-天数
	Date operator-(int day);

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++   
	Date operator++(int);

	//后置--   
	Date operator--(int);
	
	// 前置--
	Date& operator--();
	
	// >运算符重载
	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);

	// 日期-日期 返回天数
	int operator-(const Date& d);

private:
	int _year;
	int _month;
	int _day;
};

确定某年某月有多少天

//因为闰年和平年二月份的天数不一样,随便加加减减会有问题,这个函数可以根据闰年和平年给出对应的天数
//因为后面需要频繁调用它,设置成内联函数
inline int Date::GetMonthDay(int year, int month) {
    //设置成静态变量,不用每次调用这个函数都开辟一个数组
    static int arr[13] = {0,31,28,31,30,31,30,31,31,30,31, 30, 31};
    int day = arr[month];
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
        day = 29;
    return day;
}

构造函数

Date::Date(int year, int month, int day) {
    //防止给的日期有错误
    if (year >= 0 && month > 0 && month < 13 
        && day >0 && day <= GetMonthDay(year, month)) {
        _year = year;
        _month = month;
        _day = day;
    }
    else {
        cout << "非法日期" << endl;
        cout << year << "年" << month << "月" << day << "日" << endl;
    }
}

打印日期

void Date::Print() {
    cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

日期+=天数

Date& Date::operator+=(int day) {
    if (day < 0){
        *this -= (-day);
    }
    else {
        _day += day;
        //如果天数大于月份对应的最大天数,就先减去这个月天数,然后月份++,然后再判断月份对应的天数对不对,往复循环
        while (_day > GetMonthDay(_year, _month)) {
            _day -= GetMonthDay(_year, _month);
            ++_month;
            if (_month > 12) {
                ++_year;
                _month = 1;
            }
        }
        //如果是传值返回的话传的是他的一个拷贝临时变量,又需要调用拷贝构造函数,因为出了函数作用域对象还在,所以传引用更好。
    }
    return *this;
}

日期+天数

Date Date::operator+(int day) {
    Date ret(*this);
    //复用operator+=函数
    ret += day;
    //ret是一个局部变量,出了作用域就销毁了,传不了引用
    return ret;
}

日期-=天数

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

日期-天数

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

前置++

//返回的是++后的值,可以复用+=函数,逻辑是一样的,把day换成1即可
Date& Date::operator++() {
    *this += 1;
    return *this;
}

后置++

//返回的是++前的值,不能传引用返回
//为了和前置++赋值重载函数构成函数重载,需要加个int ,不需要传实参,因为没用,如果不加参数那么前置++和后置++函数就一样了,们在进行前置++或者后置++操作时,编译器就不知道调用哪一个了
Date Date::operator++(int) {
    Date ret(*this);
    *this += 1;
    return ret;
}

后置–

//返回原对象,然后对象再--
Date Date::operator--(int) {
    Date ret = *this;
    *this -= 1;
    return ret;
}

前置–

返回--后的对象

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

>运算符重载

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

==运算符重载

bool Date::operator==(const Date& d)
{
    return _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);
}

<=运算符重载

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

!=运算符重载

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

计算两个日期之间的间隔天数,日期减去日期

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

到此这篇关于C++日期类(Date)实现的示例代码的文章就介绍到这了,更多相关C++日期类内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • Linux 软件看门狗 watchdog使用介绍

    Linux 软件看门狗 watchdog使用介绍

    这篇文章主要介绍了Linux 软件看门狗 watchdog使用介绍,需要的朋友可以参考下
    2016-10-10
  • C++中的struct和class的区别详解

    C++中的struct和class的区别详解

    这篇文章主要介绍了C++中的struct和class的区别详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • C++空类默认函数详细解析

    C++空类默认函数详细解析

    如果你只是声明一个空类,不做任何事情的话,编译器会自动为你生成一个默认构造函数、一个拷贝默认构造函数、一个默认拷贝赋值操作符和一个默认析构函数
    2013-10-10
  • C++中DeviceIoCteatol的用法实例

    C++中DeviceIoCteatol的用法实例

    这篇文章主要介绍了C++中DeviceIoCteatol的用法实例,对于学习C++针对硬件的操作有一定的参考借鉴价值,需要的朋友可以参考下
    2014-10-10
  • Qt中JSON使用的详细步骤

    Qt中JSON使用的详细步骤

    本文主要介绍了Qt中JSON使用的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • C++实现LeetCode(64.最小路径和)

    C++实现LeetCode(64.最小路径和)

    这篇文章主要介绍了C++实现LeetCode(64.最小路径和),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++连接mysql的方法(直接调用C-API)

    C++连接mysql的方法(直接调用C-API)

    首先安装mysql,点完全安装,才能在在安装目录include找到相应的头文件,注意,是完全安装,需要的朋友可以参考下
    2017-06-06
  • 详解C++中的this指针与常对象

    详解C++中的this指针与常对象

    这篇文章主要介绍了详解C++中的this指针与常对象,是C++入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • C++实现延迟的方法详解

    C++实现延迟的方法详解

    这篇文章主要为大家详细介绍了C++实现延迟的三个方法,文中的示例代码讲解详细,对我们深入了解C++有一定的帮助,感兴趣的小伙伴可以学习一下
    2022-12-12
  • C++事件处理中的__hook与__unhook用法详解

    C++事件处理中的__hook与__unhook用法详解

    这篇文章主要介绍了C++事件处理中__hook与__unhook的用法,C++中的COM类主要支持事件处理,需要的朋友可以参考下
    2016-01-01

最新评论

?


http://www.vxiaotou.com