【C++】类和对象--一篇带你解决运算符重载实例--日期类

C++日期类运算符重载实例解析

目录

1. 全缺省的构造函数

2. opeartor ==

3. operator <

4. operator <=

5. operator !=

6.operator >

7.operator >= 

8. operator +=

9. operator +

10. operator -=

11. operator -

12. 前置++、前置--、后置++、后置--

13.日期-日期


上一篇我么讲解了运算符重载的知识,今天我们通过一个例子Data日期类来巩固运算符重载的知识。

本篇文章我们将实现下面下面这些函数接口:

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// 拷贝构造函数
    //d2(d1) 
	Date(const Date& d);
	// 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);
	// 析构函数
	~Date();
	// 日期+=天数
	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);
	// >=运算符重载
	inline 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;
 
};

我们可以采用多文件的形式储存:


1. 全缺省的构造函数

构造函数其实就是初始化的一个过程,尤其注意的是对于日期的一个合法性的判断。但是因为月份的天数的细微差距,这里调用一个GetMonthDay()函数接口来完成:

int Date::GetMonthDay(int year, int month)
{
	assert(year >= 0 && month < 13 && month>0);
	//开辟一个静态区间,用来解决不同月份的天数不同
	static int monthDayAraay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && isLeapYear(month))
	{
		return 29;
	}
	return monthDayAraay[month];
}

这里因为月份对应的天数是固定的,所以我们选择将每个月的天数储存在一个静态数组中,这样一来就可以做到一次的开辟,之后可以进行多次的调用

同时因为二月天数收年份影响,所以这里继续编写一个判断是都为闰年的函数接口isLeapYear(),如果十二月份并且是闰年则直接返回29:

bool isLeapYear(int year)
{
	return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
}

上面就是我们应该在构造函数中进行的日期是否合法的判断,所以我们最终的构造函数应该是:

Date::Date(int year, int month, int day)
{
	if (year > 0 && month < 13 && month>0 && day > 0 && day <= GetMonthDay(year,month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期不合翻!" << endl;
		exit(-1);
	}
}

2. opeartor ==

判断两个日期是否相等很简单,只需要将年月日的比较结果进行与逻辑判断即可:

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

3. operator <

要想判断日期<日期:

  • 先比较year,如果year小的话,后面就无须比较了,直接返回ture
  • 如果year相等,就比较month,如果month小的话,后面也无须比较,直接返回ture
  • 如果year、month都相等时,最后才比较day,如果day小,直接返回ture
bool Date::operator < (const Date& d)
{
	if (_year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day)
		)
	{
		return true;
	}
	else
	{
		return false;
	}
}

4. operator <=

因为我们已经写了 < 和 <=的接口了,而 <= 逻辑包含了前两者,所以只需要任意满足一个即可返回true:

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

5. operator !=

显然 == 和 != 形成逻辑非关系,所有只需要对 == 取反即可:

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

6.operator >

> 和 <= 构成逻辑非,因此只需要对<=取反即可。

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

7.operator >= 

>= 和 < 构成逻辑非,因此只需要对 < 取反即可:

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

由上面的实现我们可以知道只要有了 == 和 < 接口我们就可以通过直接调用这两个接口实现其他的接口。因为接口的代码量比较小,所以我们选择将其直接写在类中,因为在类中实现的函数,编译器会将其默认为内联函数(内联函数可以减少函数调用的开销)


8. operator +=

+= 的逻辑是给出一个日期和一个天数,然后计算这个日期加上天数后的新日期,也就是说要返回一个Date日期类:

Date& Date::operator += (int day)
{
    //如果day是负数,那么就转换成为正数的-=运算
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day>GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

运行结果如下:

显然结果正确!!!


9. operator +

我们前面已经实现了 += ,两者的区别在于 += 会改变日期本身,但是 + 只是修改了一个拷贝(并不会影响原来的数据):

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

测试结果:


10. operator -=

-= 的逻辑和 += 差不多,但是要注意一点细节:

Date& Date::operator -= (int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)    //注意这个边界条件
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

11. operator -

同理:

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

测试结果:


12. 前置++、前置--、后置++、后置--

我们发现前置++和后置++的操作符都是++操作符,并且他们的返回类型,函数名,参数列表都一模一样。那么他们我们应该怎么区别呢?

这个问题其实编译器也考虑到了,因此编译器为了区别前置++和后置++,编译器规定,后置++(或后置--)的参数列表中多穿一个值。此时因为两个函数的参数列表的参数个数不同,两个函数就构成了函数重载。就可以区分两个操作符了。

需要注意的是,加的这个参数是为了区分前置和后置,因此传过来的值无所谓。我们一般可以省略写就直接只写数据类型。

因此,前置++就相当于是 +=1:

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

前置--就相当于 -=1:

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

后置++先使用,因此要创建一个tmp存上,再自身++:

Date operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

后置--先使用,因此要创建一个tmp存上,再自身--:

	Date operator--(int)//加参数为了区分,与传过来的值无关
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}

测试结果:

注意哦!重载之后的后置--,同样是先赋值再--。


13.日期-日期

这个接口我们要实现一个日期与另一个日期之间相差多少天。如果是小日期-大日期,应该返回一个负数。因此我们要对这个小细节进行处理。

这里的实现逻辑是:

  • 我们先默认第一个日期为大日期,第二个日期为小日期
  • 我们在对这两个进行比较,如果第一个是小日期,我们就将max和min进行日期调换。如果调换说明是小日期-大日期,最终的结果理应该是个负数。因此我们定义一个flag = 1,如果这里调换的话,让flag = -,最终返回n*flag即可
  • 我们要始终保持小追大。我们使用前置++运算,让小的日期追大的日期,这里需要定义一个计数器n。每++一次,n就++一次
int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;//默认认为第一大 
	Date min = d;
	if (*this < d)
	{
		min = *this;
		max = d;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		++min;
	}
	return n*flag;
}

测试结果:


(本篇完)

完整代码在下面哦

Date类代码

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值