iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++无痛实现日期类的示例代码
  • 108
分享到

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

C++实现日期类C++日期类 2022-11-13 18:11:35 108人浏览 安东尼
摘要

目录日期类的实现构造函数析构函数拷贝构造函数打印函数获取天数函数运算符重载区赋值重载整体代码Date.hDate.cpp日期类的实现 凡是要写类必须要提到六大默认成员(六位大爷):构

日期类的实现

凡是要写类必须要提到六大默认成员(六位大爷):构造函数、析构函数、拷贝构造函数、赋值重载函数、取地址重载函数(包括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++日期类的资料请关注编程网其它相关文章!

--结束END--

本文标题: C++无痛实现日期类的示例代码

本文链接: https://www.lsjlt.com/news/169597.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c语言怎么计算字符串长度
    在C语言中,可以使用内置的strlen()函数来计算字符串的长度。该函数位于string.h头文件中。以下是一个示例代码: #inc...
    99+
    2024-05-14
    c语言
  • c语言怎么判断三个数的大小
    可以通过比较三个数之间的大小关系来判断它们的大小。以下是一个示例代码: #include <stdio.h> int ...
    99+
    2024-05-14
    c语言
  • c++排序函数sort怎么使用
    在C++中,使用sort函数对容器中的元素进行排序,可以按照默认的升序排列,也可以自定义排序规则。 下面是一个简单的例子,对一个ve...
    99+
    2024-05-14
    c++
  • c++中string的compare函数怎么使用
    在C++中,可以使用string的compare函数来比较两个string对象的大小。该函数的原型如下: int compare(c...
    99+
    2024-05-14
    c++
  • c++中string的compare函数有什么用
    c++中string的compare函数用于比较两个字符串的大小关系。该函数返回一个整数值,表示两个字符串的比较结果。 具体来说,如...
    99+
    2024-05-14
    c++
  • c++中print函数的功能有哪些
    在C++中,通常使用std::cout来进行输出操作,而不是使用print函数。std::cout是C++标准库中的一个输出流对象,...
    99+
    2024-05-14
    c++
  • c语言数据结构怎么使用
    在C语言中,数据结构可以通过定义结构体来实现。以下是一些常见的数据结构的使用方法示例: 链表: #include <std...
    99+
    2024-05-14
    c语言
  • c++中print函数的用法是什么
    在C++中没有内置的print函数,但可以使用cout来输出内容到控制台。cout是C++标准库中的一个输出流对象,可以使用<...
    99+
    2024-05-14
    c++
  • c++中concept的用法是什么
    在C++20中,Concept是一种新的语言特性,用于定义类型要求和约束。Concept可以被用来约束函数模板、类模板和普通函数的参...
    99+
    2024-05-14
    c++
  • c++中concept的作用是什么
    在C++中,concept的作用是定义一种通用的约束,用于限制模板参数的类型范围。通过使用concept,可以在编译时对模板参数进行...
    99+
    2024-05-14
    c++
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作