前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【C++修炼之路】类与对象实战:实现一个日期类

【C++修炼之路】类与对象实战:实现一个日期类

作者头像
f狐o狸x
发布2025-03-08 09:49:22
发布2025-03-08 09:49:22
3100
代码可运行
举报
运行总次数:0
代码可运行

经过前面两篇文章的学习,相信聪明的你应该已经初步了解类与对象了,现在我们将一起实现一个日期类,进一步加深我面对类的理解。

在软件开发中,日期和时间的处理无处不在,从日程管理到金融计算,从数据分析到天气预报,日期类的设计都是开发者必须面对的挑战。在本文中,我们将从零开始,一步步实现一个功能完备的日期类。无论你是C++新手,还是想巩固面向对象编程基础,这个项目都会让你收获满满。

一、日期类的核心功能

想象一下:如果你要实现你手机里的日历这个app,它应该有些什么功能呢?

我认为主要功能如下:

  1. 日期合法性校验
  2. 日期加减(支持天数、月数、年数)
  3. 日期差计算
  4. 重载运算符(+, -, ==, << 等)
  5. 星期的计算与格式化输出

二、日期类的定义

要想实现上面的功能,我们需要三个内部成员来表示日期(年、月、日),在通过各种函数来实现各种功能

代码语言:javascript
代码运行次数:0
复制
class Date
{
	//友元声明
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);

public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1);

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

	//打印日历
	void PrintCalendar()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	//操作符重载
	bool operator<(const Date& x) const;
	bool operator==(const Date& x) const;
	bool operator<=(const Date& x) const;
	bool operator>(const Date& x) const;
	bool operator>=(const Date& x) const;
	bool operator!=(const Date& x) const;
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

	Date& operator+=(int days);
	Date operator+(int days);
	Date& operator-=(int days);
	Date operator-(int days);
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

// 日期类成员
private:
	int _year;  // 年
	int _month; // 月
	int _day;   // 日

};

三、实现日期类比较大小

比较两天日期的大小我们用重载函数 bool operate<(const class& Date) const 和 bool operator==(const Date& x) const两个重载函数完成,剩下的可以用这两个表示

代码语言:javascript
代码运行次数:0
复制
bool Date::operator<(Date& d)
{
	if (_year < d._year)
		return true;
	else if (_year == d._year && _month < d._month)
		return true;
	else if (_year == d._year && _month == d._month && _day < d._day)
		return true;
	else
		return false;
}

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

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

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

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

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

四、日期类加减

想完成日期类的加减,其实就是把日期里的天数都合法化,大于这个月的天数,就加一个月,日期小于等于0,就减少一个月,当月份加到13的时候,就重置为1,年份加1,同理,当月份减少到0的时候,就置为12,年份减1。这里我们还需要写一个函数来获取这个月的天数有多少天(主要是判断是否为闰年闰月,如果是就返回29天,其他就正常返回)

代码语言:javascript
代码运行次数:0
复制
int Date::GetMonthDay(int year, int month)
{
	int days[13] = { 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;
	return days[month];
}

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

Date Date::operator+(int days) const
{
	Date tmp = (*this);
	tmp += days;
	return tmp;
}

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

Date Date::operator-(int days) const
{
	Date tmp = *this;
	tmp -= days;
	return tmp;
}

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

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

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


Date Date::operator--(int) const
{
	Date tmp = *this;
	tmp -= 1;
	return tmp;
}

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

五、输入输出日期

这里需要用到友元函数,才能将按我们的习惯把日出输出出来

代码语言:javascript
代码运行次数:0
复制
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day ;
	return _cout;
}

istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year >> d._month >> d._day;
	return _cin;
}

六、附带功能

完成这些之后,我们可以通过库函数<ctime>去获得当天的日期,然后做一份倒计时出来

代码语言:javascript
代码运行次数:0
复制
void TestCalendar1()
{
	Date d1,d2;
	d1.GetTodayData();
	cout << "今天是:" << d1 << endl;
	cout << "请输入蓝桥杯日期:" ;
	cin >> d2;
	cout << "距离蓝桥杯还有" << (d2 - d1) << "天,加油吧骚年" << endl;
}

int main()
{
	TestCalendar1();
	return 0;
}

这里就是日期类的全部实现内容啦,如果你也自己实现一遍,你对类和对象的理解将会上升一个层次,加油学习吧!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、日期类的核心功能
  • 二、日期类的定义
  • 三、实现日期类比较大小
  • 四、日期类加减
  • 五、输入输出日期
  • 六、附带功能
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档