日期类的实现C++(笔记)

我们前面已经基本学习完了C++的类和对象,并且我们也使用了日期类的部分来在类和对象部分举例,这篇文章将实现一个小的日期类:

首先我们先在VS中创建好三个文件:

Test.cpp用来测试我们的日期类是否正确。

对于一个日期类,在我们日常生活,我们可以对一个日期 加 或 减整数天,比如对2004年2月1日加上3天,就得到了2004年2月4日,同理减法。但我们对一个日期进行乘除也就没意义了,所以我们在进行运算符重载就不需要乘除。

我们先来看看我们在头文件中都需要实现什么功能:

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
	Date(int year = 2004, int month = 2, int day = 2);
	void Print();
	Date(const Date& d);

	~Date()
	{
		_year = 0;
		_month = 0;
		_day = 0;
	}
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		static int MonthArr[13] = { -1,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;
		}

		return MonthArr[month];
	}

	bool CheckDate();

	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator>(const Date& d)const;
	bool operator>=(const Date& d)const;

	Date& operator=(const Date& d);
	Date operator++();
	Date operator--();
	Date operator++(int);
	Date operator--(int);

	Date& operator+=(int day);
	Date operator+(int day)const;

	Date& operator-=(int day);
	Date operator-(int day)const;

	int operator-(const Date& d)const;

private:
	int _year;
	int _month;
	int _day;

};

其实内容上大部分都是对一个日期进行 加 和 减 的操作,其次就是判断日期大小操作等等。

 对于Date的构造函数,我想应该很简单,这里直接展示代码:

Date::Date(int year, int month, int day)
	:_year(year)
	,_month(month)
	,_day(day)
{
	if (!CheckDate())
	{
		cout << "非法日期>:" << _year << "/" << _month << "/" << _day << endl;
	}
}

这里进行初始化列表,并且缺省参数规定给在函数声明时,因此这就是Date构造函数。这里还有一个检查日期是否合法的函数,也比较简单,直接展示:

bool Date::CheckDate()
{
	if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	return true;
}

 接下来是拷贝构造函数,也是很简单:

Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

 对于GetMonthDay这个函数,我相信大家在学习C语言的时候,肯定写过这个函数,大家认真看一下肯定能懂,我们主要来讲解重载:

    bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator>(const Date& d)const;
	bool operator>=(const Date& d)const;

对于这几个判断的运算符,我们其实写上2个就行了,因为写出了两个,其他的判断运算符基本上是复用,比如我们写了< 和 ==,我们通过返回取反即可,以<为例:

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

因为类的成员函数默认第一个参数是this,并且<左边为第一个参数,右边为第二个参数。如果this的_year都比d的_year小,那么<就返回true,如果两个年份相等了,再比较月、日等等,如果条件都不符合,最终返回false即可。这就是一个<运算符的重载。

我们再来看==的重载:

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

 如果两个参数的年 月 日均相等,返回true,如果有一个不相等,那么结果就是false,返回false。

当我们写完了这两个运算符的重载之后,后面就简单许多了。

例如我直接展示<= 和 > : 

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

如果符合 < 或者 == 的其中一个,那么<= 就成立;对于>来说我们只需返回<=的取反即可,其他的操作符重载类似,这里就不展示代码了。

我们再来看对于日期的+ - 相关运算符的重载:

    Date& operator=(const Date& d);
	Date operator++();
	Date operator--();
	Date operator++(int);
	Date operator--(int);

	Date& operator+=(int day);
	Date operator+(int day)const;

	Date& operator-=(int day);
	Date operator-(int day)const;

	int operator-(const Date& d)const;

先来看第一个赋值运算符的重载。我们都知道,在C语言中,我们可以进行这段操作:

int a = b = c = 2;假设b,c已经声明为(int),因为他是从右向左,c = 2的结果是2,然后给b,b = 2

的结果是2,然后给a。因此在类的操作符重载的时候,我们也需要实现。

对于赋值运算符的重载:

Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

 第一个操作数在左边,即为我们的this,第二个操作数在右边,即为我们的d,代码也很简单,最终返回*this的引用,因为this生命周期并不在这个函数里面,因此我们不用担心空引用问题。

对于+、+=、++(-、-=、--)运算符重载,这里只展示+相关的,因为 - 的与 + 的类似,先来看+=:

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

 我们都知道,每个月的天数不一样,并且一旦天数达到了上限,我们就要给月+1,一旦月份加到了13,我们就要给年+1,并且将月份修改为1,因为12月+1就到了明年的1月。

如果这里我们 += 的是负数,我们就去调用 -= -day,例如要加上 -100天其实就是减100天。

如果不是负数,我们先直接给_day把天数加上,然后如果_day大于对应月的天数,我们就需要给_day减去该月的天数,然后_month++,如果_month都加到了13,此时修改为1即可。最后不要忘了返回*this。

看完了 += 后, 其实 + 的运算就十分简单了:

Date Date::operator+(int day)const
{
	Date tmp = *this;
	tmp += day;

	return tmp;
}

对于整形来说, c = a + b; a和b的值都不会变,因此我们在函数中创建一个tmp = *this,此时tmp的日期和*this的日期就一致了,然后进行 += 天数运算(我们刚刚写过),最后返回tmp即可;注意,这里不能返回tmp的引用,因为一旦出了作用域,tmp销毁,那么我们得到的就是空引用。

最后我们来看 ++ 运算符的重载:

//前置++
Date Date::operator++()
{
	*this += 1;
	return *this;
}

//后置++
Date Date::operator++(int)
{
	Date tmp = *this;

	*this += 1;

	return tmp;
}

这个也比较简单,我们之前已经讲过,如果要变成后置++,只需要加一个int参数即可。而 前置++ 与 后置++ 的唯一区别就是前置++用的时候已经加1了,后置是先使用没加之前的值,在加1,我相信同学们能看懂这两段代码。

接下来我们来实现一下两个日期相减,得到两个日期之间相差的天数:

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

我相信很多同学在写这个重载的时候,肯定都是一头雾水,想来想去也不知道该怎么计算,因为不仅有平年与闰年的区别,更复杂的是,每个月的天数也是不一样的。但是如果我们使用到之前的重载运算符,那么上面的代码你肯定能看懂。

因为左操作数是*this,右操作数是d,我们开始假设小的min日期为*this,大的max日期为d,如果假设成立,那么max - *this 将是一个负数,所以我们开始定义flag 为 -1;如果min > max。那么我们交换min 与 max,并把flag改为1。然后我们定义一个n来计数,如果min == max了,我们就break打破循环,如果min != max,那么我们++min并且再 n++;直到min与max相等,那么最终的n值就是二者相差的天数,最后返回 n*flag即可。

日期类的实现总的来说还是挺简单的,只要我们认真去钻研,都可以自己来实现。以上内容如果有错误的地方,欢迎各位批评指正!!! 

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值