C++使用类和对象实现日期类Date

个人主页:C++忠实粉丝
欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 C++忠实粉丝 原创

C++使用类和对象实现日期类Date

收录于专栏【C++语法基础
本专栏旨在分享学习C++的一点学习笔记,欢迎大家在评论区交流讨论💌

目录

1.创建日期类Date

 2.日期类相关函数的实现

1.获取某年某月的天数

2.全缺省的构造函数

3.拷贝构造函数

4.检查天数是否合法

5.赋值运算符重载

6.析构函数

7.日期+=天数

8.日期+天数

9.日期-=天数

10.日期-天数

11.前置++

12.后置++

13.后置--

14.前置--

15.<运算符重载

16.==运算符重载

17.>=运算符重载

18.>运算符重载

19.<=运算符重载

20.!=运算符重载

21.日期-日期 返回天数

22.输入流 

23.输出流

3.测试代码

测试1

​编辑

 测试2

 测试3

 测试4

4.取地址运算符重载 

5.总结 

6.代码参考


1.创建日期类Date

一个基本的日期类需要有构造函数,拷贝构造函数,析构函数,相关运算符重载,以及成员变量

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);

	//检查天数是否合法
	bool CheckDate() const;

	// 析构函数
	~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);

	// >=运算符重载
	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;
};

注意:

我们可以将获取天数的函数写在日期类内部(这样的函数会频繁使用),这样就默认为类的内联函数,在函数调用时直接展开,来增加日期类的运行速度,将其他函数的声明和定义分开,增加我们代码的可读性!!! 

内联函数的具体内容可以参考这篇博客----C++基础入门(下)-CSDN博客 

 2.日期类相关函数的实现

1.获取某年某月的天数

因为每月的天数除了2月份在变化,其他天的天数都是不变的,我们就可以用一个静态数组来存贮,单独分析2月份的情况.

	int GetMonthDay(int year, int month) const
	{
		assert(month > 0 && month < 13);

		static int monthDayArray[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 monthDayArray[month];
	}

断言检查

assert(month > 0 && month < 13); 确保传入的月份在1到12之间。这是有效的,保证了函数不会处理无效的月份。
月份天数数组

static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
该数组存储每个月的天数,其中索引从1到12对应月份1到12,索引 0 的值为 - 1表示无效的日期。static 关键字确保该数组在第一次调用时初始化,之后调用不会重新初始化,节省了开销。
闰年处理

if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
检查是否为2月(即闰月),并根据闰年的规则返回 29 天。如果是闰年,2 月返回 29 天,否则返回 28 天。
返回天数

return monthDayArray[month];
对于非2月的情况,从数组中返回天数。

2.全缺省的构造函数

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!CheckDate())
	{
		cout << "非法日期:";
		Print();
	}
}

注意:

我们写构造函数时,可以加入判断日期是否合法 

3.拷贝构造函数

// 拷贝构造函数
// d2(d1)

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

4.检查天数是否合法

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

5.赋值运算符重载

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

6.析构函数

注意:如果类中没有申请资源时,析构函数可以不写直接使⽤编译器⽣成的默认析构函数,如Date;如果默认⽣成的析构就可以⽤,也就不需要显⽰写析构,如MyQueue;但是有资源申请时,⼀定要⾃⼰写析构,否则会造成资源泄漏,如Stack。

析构函数具体内容可以看--C++类和对象(中)1-CSDN博客 

7.日期+=天数

// d1 += 100
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;
}

注意:

这里的Date可能为负数,如果这里Date为负数,我们可以转换为-=(-day),交给-=运算符重载来实现,这样能缩减我们的代码,提高代码的运行速率! 

8.日期+天数

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

	return tmp;
}

直接调用之前我们实现的+=运算符重载来实现 

9.日期-=天数

// d1 -= 100
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;
}

和+=类似,同样,如果day<0转换为+=(-day),交给+=运算符重载来实现

10.日期-天数

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

	return tmp;
}

直接调用-=来实现 

11.前置++

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

前缀自增 ++d1: 直接修改对象,返回自增后的对象的引用。

12.后置++

// d1++;
// d1.operator++(0);
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

后缀自增 d1++: 创建一个对象副本保存原始状态,修改当前对象,返回原始对象副本。 

13.后置--

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

14.前置--

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

15.<运算符重载

// d1 < d2
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;
		}
	}

	return false;
}

16.==运算符重载

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

17.>=运算符重载

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

18.>运算符重载

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

19.<=运算符重载

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

20.!=运算符重载

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

21.日期-日期 返回天数

// d1 - d2
int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

注意:

这里使用了flag来判断相差的天数的正负 

因为我们这里已经将前置++重载完毕,后面的日期转换我们这里不需要考虑!!!

22.输入流 

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;

		if (!d.CheckDate())
		{
			cout << "输入日期非法:";
			d.Print();
			cout << "请重新输入!!!" << endl;
		}
		else
		{
			break;
		}
	}

	return in;
}

23.输出流

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

3.测试代码

测试1

void TestDate1()
{
	Date d1(2024, 7, 12);
	Date d2 = d1 + 100;
	//Date d3(d1 + 100);
	d1.Print();
	d2.Print();

	//d1 += 100;
	//d1.Print();

	d1 += 30000;
	d1.Print();
}

测试我们的构造函数,拷贝构造函数,打印函数,以及相关运算符重载 

 测试2

void TestDate2()
{
	/*Date d1(2024, 7, 13);
	d1 -= 30000;
	d1.Print();*/

	Date d1(2024, 7, 13);
	Date ret1 = d1++;
	ret1.Print();
	d1.Print();

	Date d2(2024, 7, 13);
	Date ret2 = ++d2;
	ret2.Print();
	d2.Print();
}

测试前后置++是否正常 

 

 测试3

void TestDate4()
{
	Date d1(2034, 10, 1);
	Date d2(2024, 6, 31);

	cout << d1 - d2 << endl;
}

测试两个日期相减 

 测试4

void TestDate5()
{
	Date d1, d2;
	cin >> d1 >> d2;
	cout << d1 << d2;

	cout << d1 - d2 << endl;
}

 测试输入输出流

 

4.取地址运算符重载 

正常情况下, 我们不需要取地址运算符重载.

取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,⼀般这两个函数编译器⾃动⽣成的就可以够我们⽤了,不需要去显⽰实现。除⾮⼀些很特殊的场景,⽐如我们不想让别⼈取到当前类对象的地址,就可以⾃⼰实现⼀份,胡乱返回⼀个地址,让对方程序崩溃!!!

const Date* operator&() const
{
	//return this;
	return nullptr;
	//return (Date*)0x2673FE30;
}

测试:

void TestDate6()
{
	const Date d1(2024, 7, 13);
	d1.Print();

	Date d2(2024, 7, 13);
	d2.Print();

	cout << &d1 << endl;
	cout << &d2 << endl;
}

5.总结 

日期类可以很好的帮助我们梳理C++中类和对象的一些知识,并将知识转换成实际的应用,很有实践意义!!!

后面我也会持续更新C++中STL中的string,感兴趣的宝子们点赞关注不迷路哦!!!

大家也可以关注我的C++专栏[C++语法学习]后续的文章将会持续更新到里面,欢迎 点赞👍 收藏✨ 留言✉ 加关注💓

6.代码参考

#include<iostream>
#include<assert.h>

using namespace std;


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 monthDayArray[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 monthDayArray[month];
	}

	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 day) const;
	Date& operator+=(int day);

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

	// d1++;
	// d1.operator++(0);
	Date operator++(int);

	// ++d1;
	// d1.operator++();
	Date& operator++();


	// d1--;
	// d1.operator--(0);
	Date operator--(int);

	// --d1;
	// d1.operator--();
	Date& operator--();

	// d1 - d2
	int operator-(const Date& d) const;

	//void operator<<(ostream& out);

	//Date* operator&()
	//{
	//	//return this;
	//	//return nullptr;
	//	//return (Date*)0x2673FF40;
	//}

	const Date* operator&() const
	{
		//return this;
		return nullptr;
		//return (Date*)0x2673FE30;
	}
	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值