C++无痛实现日期类的示例代码

 更新时间:2022年10月18日 10:59:12   作者:梨+苹  
凡是要写类必须要提到六大默认成员(六位大爷):构造函数、析构函数、拷贝构造函数、赋值重载函数、取地址重载函数(包括const对象和普通对象);那么这次的日期类又需要伺候哪几位大爷呢?本文就来详细说说

日期类的实现

凡是要写类必须要提到六大默认成员(六位大爷):构造函数、析构函数、拷贝构造函数、赋值重载函数、取地址重载函数(包括const对象和普通对象);那么这次的日期类又需要伺候哪几位大爷呢?

日期类的实现中函数与函数之间有较强的耦合性,所以实现的逻辑顺序一定要把握好,不然会晕头转向的!!! 下面是我的实现顺序:

构造函数

Date(const Date& d)//拷贝构造函数
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        
    }

析构函数

~Date()//析构函数
    {
        _year = 1;
        _month = 1;
        _day = 1;
    }

拷贝构造函数

    Date(const Date& d)//拷贝构造函数
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        
    }

打印函数

    void Print()//打印函数
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

这里我们还需要写一个获取月份对应天数的函数

获取天数函数

    int GetTrueDay(int year, int month)//得到正确月份天数
    {
        static int monthday[] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
        || (year % 400 == 0)))
        {
            return 29;
        }
        else
        {
            return monthday[month];
        }
    }

这里就是大体框架了,接下来是各个细节部分

运算符重载区

判断两个日期是否相等(*this==d)

bool operator==(const Date& d) const;//等于
//---相等为真(返回1);不相同为假(返回0)
bool Date:: operator==(const Date& d) const//等于
{//年相等才判断到月,月相等才判断到年
    return _year == d._year
        && _month == d._month
        && _day == d._day;
}

判断前一个日期是否大于后一个日期(*this>d)

bool operator>(const Date& d) const;//大于
//---相等为真(返回1);不相同为假(返回0)
bool Date:: operator>(const Date& d) const//大于
{//这里一样的判断顺序依次是年---月---日
    if (_year > d._year)
    {
        return true;
    }
    else if (_month > d._month)
    {
        return true;
    }
    else if (_day == d._day)
    {
        return true;
    }
    else
    {
        return false;
    }
//大于
}

判断前一个日期是否大于等于后一个日期(*this>=d)

这里直接重载!!!

bool operator>=(const Date& d) const//大于等于
    {
        return *this > d || *this == d;
    }

判断前一个日期是否小于后一个日期(*this<d)

这里直接重载!!!

    bool operator<(const Date& d)const //判断小于
    {
        return !(*this >= d);
    }

判断前一个日期是否小于等于后一个日期(*this<=d)

这里直接重载!!!

    bool operator<=(const Date& d)const//小于等于
        {
            return !(*this > d);
        }

赋值重载

前一个日期等于后一个日期(*this=d)—可以连续赋值

    Date& operator=(const Date& d)//赋值重载
        {
            if (this!=&d)
            {
                _year = d._year;
                _month = d._month;
                _day = d._day;
            }
            else
            {
                return*this;
            }
        }

对日期减天数-不影响自身-用拷贝构造

    Date operator-(int day) const;//减天数
Date Date:: operator-(int day) const//减天数-不影响本身-不用引用-用拷贝构造函数
{
    Date tmp(*this);
    tmp-= day;
    return tmp;
}

对日期加天数-不影响自身-用拷贝构造

Date operator+(int day) const;//加天数
Date Date:: operator+(int day) const//加天数-不影响本身-不用引用-用拷贝构造函数
{
    Date tmp(*this);
    tmp+= day;
    return tmp;
}

日期减等天数-影响自身-用引用

Date& operator-=(int day) ;//减等天数
Date& Date:: operator-=(int day) //减等天数- 影响本身-用引用-不加const
{
    if (day < 0)
    {
        return *this += abs(day);
    }
    _day -= day;
    while (_day<=0)
    {
        --_month;
        if (_month == 0)
        {
            --_year;
            _month = 12;
        }
        _day += GetTrueDay(_year, _month);
    }
    return *this;
    
}

日期加等天数-影响自身-用引用

Date& operator+=(int day);//加天数
Date& Date:: operator+=(int day) //加天数- 影响本身-用引用-不加const
{
    if (day < 0)
    {
        return *this -= abs(day);
    }
    _day += day;
    while (_day > GetTrueDay(_year, _month))
    {
        _day -= GetTrueDay(_year,_month);
        _month++;
        if (_month == 13)
        {
            _year++;
            _month = 1;
        }
    }
    return *this;
}

日期天数前置++【影响自身(自增)-用引用-不加const】

Date& operator++(); //天数前置++
Date& Date::operator++()//前置++-改变自身-用引用
{
    return *this += 1;
}

日期天数后置++【不影响自身-用拷贝构造】

Date operator++(int);//后置++
Date Date::operator++(int)//后置++-不改变自身-用拷贝函数-括号里+int
{
    Date tmp(*this);
    *this += 1;
    return tmp;
}

日期天数前置–【影响自身(自减)-用引用-不加const】

Date& operator--();//前置--
Date& Date::operator--()//前置-- --需要改变自身-用引用
{
    return *this -= 1;
}

日期天数后置–【不影响自身-用拷贝构造

Date operator--(int);//后置--
Date Date::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int
{
    Date tmp(*this);
    *this -= 1;
    return tmp;
 }

日期减日期(前一个日期减后一个日期-算差距天数)

int operator-(const Date& d)const;//日期减日期-算差距天数
int Date:: operator-(const Date& d) const//日期减日期-算差距天数-都不改变自身+const
{
	Date max = *this;
	Date min=d;
	int flag = 1;
	if (*this<d)
	{
		max = d;
		min = *this;
		flag = -1;//如果*this比d小则减出来是负数,所以要预备flag=-1
	}

	int n = 0;
	while (min < max)//min++,max--,最后相等时,n++得出的就是差距天数
	{
		n++;
		min++;
	}
	return flag * n;//*this比d小,得出来是负数-乘-1,*this比d大,得正数-乘1
}

流插入函数

friend ostream& operator<<(ostream& out, Date& d);//流插入友元声明
ostream& operator<<(ostream& out, Date& d)//流插入
{
    cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    return cout;
}

流提取函数

    friend istream& operator>>(istream& in, Date& d);//流提取友元声明
istream& operator>>(istream& in, Date& d)//流提取
{
    in>> d._year;
    in >> d._month;
    in >> d._day;
    return in;
    
 }

好啦,以上就是日期类实现各个模块啦,下面是整体代码!

整体代码

Date.h

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:

	Date(const Date& d)//拷贝构造函数
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		
	}

	~Date()//析构函数
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}

	void Print()//打印函数
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	int GetTrueDay(int year, int month)//得到正确月份天数
	{
		static int monthday[] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return monthday[month];
		}
	}
	Date(int year = 1, int month = 1, int day = 1)//构造函数
		: _year(year)
		, _month(month)
		, _day(day)
	{

	}

	bool operator==(const Date& d) const;//等于
	bool operator>(const Date& d) const;//大于
	bool operator>=(const Date& d) const//大于等于
	{
		return *this > d || *this == d;
	}
		bool operator<(const Date& d)const //判断小于
	{
		return !(*this >= d);
	}
		bool operator<=(const Date& d)const//小于等于
		{
			return !(*this > d);
		}

		Date& operator=(const Date& d)//赋值重载
		{
			if (this!=&d)
			{
				_year = d._year;
				_month = d._month;
				_day = d._day;
			}
			else
			{
				return*this;
			}
		}
		Date operator-(int day) const;//减天数
		Date operator+(int day) const;//加天数
		Date& operator-=(int day) ;//减等天数
		Date& operator+=(int day);//加天数
		Date& operator++(); //天数前置++
		Date operator++(int);//后置++
		Date& operator--();//前置--
		Date operator--(int);//后置--
		int operator-(const Date& d)const;//日期减日期-算差距天数
		friend ostream& operator<<(ostream& out, Date& d);//流插入友元声明
		friend istream& operator>>(istream& in, Date& d);//流提取友元声明
private:
	int _year;
	int _month;
	int _day;
}; 

Date.cpp

#include"Date.h"




bool Date:: operator==(const Date& d) const//等于
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date:: operator>(const Date& d) const//大于
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_month > d._month)
	{
		return true;
	}
	else if (_day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}

}

Date Date:: operator-(int day) const//减天数-不影响本身-不用引用-用拷贝函数
{
	Date tmp(*this);
	tmp-= day;
	return tmp;
}
Date Date:: operator+(int day) const//加天数-不影响本身-不用引用-用拷贝函数
{
	Date tmp(*this);
	tmp+= day;
	return tmp;
}

Date& Date:: operator-=(int day) //减等天数- 影响本身-用引用-不加const
{
	if (day < 0)
	{
		return *this += abs(day);
	}
	_day -= day;
	while (_day<=0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetTrueDay(_year, _month);
	}
	return *this;
	
}

Date& Date:: operator+=(int day) //加天数- 影响本身-用引用-不加const
{
	if (day < 0)
	{
		return *this -= abs(day);
	}
	_day += day;
	while (_day > GetTrueDay(_year, _month))
	{
		_day -= GetTrueDay(_year,_month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date& Date::operator++()//前置++-改变自身-用引用
{
	return *this += 1;
}

Date Date::operator++(int)//后置++-不改变自身-用拷贝函数-括号里+int
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()//前置-- --需要改变自身-用引用
{
	return *this -= 1;
}
Date Date::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
 }

int Date:: operator-(const Date& d) const//日期减日期-算差距天数-都不改变自身+const
{
	Date max = *this;
	Date min=d;
	int flag = 1;
	if (*this<d)
	{
		max = d;
		min = *this;
		flag = -1;//如果*this比d小则减出来是负数,所以要预备flag=-1
	}

	int n = 0;
	while (min < max)
	{
		n++;
		min++;
	}
	return flag * n;//*this比d小,得出来是负数-乘-1,比大,乘1
}

ostream& operator<<(ostream& out, Date& d)//流插入
{
	cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return cout;
}

istream& operator>>(istream& in, Date& d)//流提取
{
	in>> d._year;
	in >> d._month;
	in >> d._day;
	return in;
	
 }

以上就是C++无痛实现日期类的示例代码的详细内容,更多关于C++日期类的资料请关注脚本之家其它相关文章!

相关文章

  • C++实现单例模式日志输出详解

    C++实现单例模式日志输出详解

    这篇文章主要为大家详细介绍了C++中单例模式的相关知识,并给出单例模式日志实现,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-04-04
  • C++实现一个线程安全的单例工厂实现代码

    C++实现一个线程安全的单例工厂实现代码

    这篇文章主要介绍了 C++实现一个线程安全的单例工厂实现代码的相关资料,需要的朋友可以参考下
    2017-05-05
  • VS2017+Qt5+Opencv3.4调用摄像头拍照并存储

    VS2017+Qt5+Opencv3.4调用摄像头拍照并存储

    本文主要介绍了VS2017+Qt5+Opencv3.4调用摄像头拍照并存储,实现了视频,拍照,保存这三个功能。具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • 深入解析C++编程中的纯虚函数和抽象类

    深入解析C++编程中的纯虚函数和抽象类

    这篇文章主要介绍了深入解析C++编程中的纯虚函数和抽象类,是C++入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • C++ EasyX学习之鼠标操作详解

    C++ EasyX学习之鼠标操作详解

    EasyX是针对C/C++的图形库,可以帮助使用C/C++语言的程序员快速上手图形和游戏编程。本文将为大家详细讲讲EasyX的鼠标操作,需要的可以参考一下
    2022-07-07
  • C语言实现简易贪吃蛇游戏的示例代码

    C语言实现简易贪吃蛇游戏的示例代码

    这篇文章主要介绍了如何利用C语言实现一个经典的小游戏——贪吃蛇,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-10-10
  • C++实现完整功能的通讯录管理系统详解

    C++实现完整功能的通讯录管理系统详解

    来了来了,通讯录管理系统踏着七彩祥云飞来了,结合前面的结构体知识和分文件编写方法,我总结并码了一个带菜单的通讯录管理系统,在这篇文章中将会提到C的清空屏幕函数,嵌套结构体具体实现,简单且充实,跟着我的思路,可以很清晰的解决这个项目
    2022-05-05
  • C语言数据结构之判断循环链表空与满

    C语言数据结构之判断循环链表空与满

    这篇文章主要介绍了C语言数据结构之判断循环链表空与满的相关资料,希望通过本文能帮助到大家,让大家掌握这部分内容,需要的朋友可以参考下
    2017-10-10
  • 详解C++ new-handler机制

    详解C++ new-handler机制

    这篇文章主要介绍了C++ new-handler机制的相关资料,帮助大家更好的理解和使用c++,感兴趣的朋友可以了解下
    2020-11-11
  • C++深入刨析muduo中的抽象类Poller

    C++深入刨析muduo中的抽象类Poller

    muduo网络库中Poller类是一个抽象类,用户使用PollPoller或者EPollPoller类,下面跟随小编一起来详细了解一下
    2022-04-04

最新评论