日期类实现(类和对象的应用)

0.头文件(先供大家参考)

这些是所有我们要实现的内容,下面来带着大家一步步分析。

接下来会大量应用this指针,建议先理解其功能。

#pragma once
#include<iostream>
using namespace std;
#include<assert.h>

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	bool CheckDate() const;
	Date(int year = 1900, int month = 1, int day = 1);
	void Print() const;

	int GetMonthDay(int year, int month) const
	{
		assert(month > 0 && month < 13);
		static int monthDayArry[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 monthDayArry[month];
	}

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

	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++(int);//后置++
	Date& operator++();//前置++(更高效)
	int operator-(const Date& d) const;

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

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

1.类的创建

首先是public区域:

要实现日期类,首先要清楚平年或闰年、每月有几天(尤其是二月的天数)。

我们在类中创建出这个函数来解决问题:

int GetMonthDay(int year, int month) const
{
	assert(month > 0 && month < 13);
	static int monthDayArry[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 monthDayArry[month];
}

首先assert断言便于下文函数实现中能够快速检查出错误所在。

在利用静态数组一一罗列出各个月份的天数(先写平年,闰年单独if语句判断)注意这里给了13个数字是为了让数组标号与真实月份对其。

因为本函数未涉及参数的变化,所以加上const修饰使代码更不易出错。

接下来,构造函数Date(与类名相同),并赋值全缺省。

Date(int year = 1900, int month = 1, int day = 1);
bool CheckDate() const;//检查输入错误
//例如输入了(2024.6.31)但六月实际只有30天,系统就会检测出来
void Print() const;//测试打印用

最后在private区域中填上相应变量,初步框架即可实现。

2.基本函数实现

bool Date::CheckDate() const
{
	if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckDate())
	{
		cout << "非法输入";
		Print();
	}
}
void Date::Print() const
{
	cout << _year << "/" << _month << "/" << _day<<endl;
}
//这里是三个基本的函数,简单易懂,不做赘述

3.单元操作实现

Date operator+(int day) const;
//这里是某年某月某日加上多少多少天,输出的是加后的日期,原来的日期不变
//因此可以用const修饰
Date& operator+=(int day);
//这里输出的虽然也是加后的日期,但原有日期发生改变,因此不用const
//注意由于是+=,所以要传引用
Date operator-(int day) const;
Date& operator-=(int day);

先介绍加法类:

Date& Date::operator+=(int day)//实际传了两个参,但第一个默认赋给了this指针不再显示
{
	if (day < 0)
	{
		return *this -= (-day);
//这里实际上是调用了下文的“-=”运算,可以先忽略
//因为要是直接加上一个负数的话,结果也会输出负数,这显然是不合理的
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}
Date Date::operator+(int day) const
{
	Date tmp = *this;//创建tmp的原因是*this在“+”运算中是不可以改变的
	tmp += day;
	return tmp;
}

因为我们已经先实现了“+=”,而加法类的主体逻辑是类似的,因此再“+”中我们可以直接用上tmp += day。

以上是第一种写法,但问题来了,为什么不先实现“+”,再由它来赋值给“+=”呢?

当然可以!

我们来用另一种方式来实现减法类:

​
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		--tmp._month;
		if (tmp._month == 0)
		{
			tmp._month = 12;
			--tmp._year;
		}

		tmp._day += GetMonthDay(tmp._year, tmp._month);
	}

	return tmp;
}//实际上就是加了个tmp指针

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

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

显而易见,前者的实现方式不仅书写上比后者麻烦,实际效率也是低人一等。

因为相对于“-”,无论哪种方式都要调用两次拷贝(开头创建tmp一次,返回tmp又一次),并无差异;但对“-=”,其自己实现时不调用任何拷贝,但要利用“-”来实现就会再次调用“-”运算里的两次拷贝,再加上一次自身的赋值,就会降低效率。

4.二元比较类实现

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;

看着有六个很多,其实需要我们写的就两个。

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)
	    {
			return _day < d._day;
	    }
	}//把true情况全部列举出来
	return false;
}
bool Date::operator==(const Date& d) const
{
	return _year == d._year && _month == d._month && _day == d._day;
}

剩下的全都可以运用数学逻辑通过逻辑运算符来实现:

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

5.二元相减算天数差

就不说二元相加了(因为实际生活中没啥意义)。

int Date::operator-(const Date& d) const
//区别与上文,这里返回值是int
//它的第一个运算对象默认传给隐式的this指针,第二个也就是括号里的
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (d > *this)
	{
		max = d;
		min = *this;
		flag = -1;
	}//比较两个日期大小
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;//返回结果为前者-后者的天数实际值(可为负)
}

还有一个流插入,流提取问题,想要了解可去阅读本栏目的(其三)中有介绍。

感谢阅读!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值