C++类和对象实战——日期类的实现

本文详细介绍了如何实现一个日期类,包括构造函数、拷贝构造函数、赋值运算符、日期比较(等于、小于、大于等)、加减运算符重载,以及友元函数和测试用例。通过实例展示了日期类的规范和优化过程。
摘要由CSDN通过智能技术生成

目录

前言

日期类的实现

 定义日期类

构造函数

拷贝构造函数

赋值运算符重载

日期打印函数 

 获取某月天数

 日期类大小比较

⭐ == 运算符重载 

⭐ < 运算符重载

⭐ <= 运算符重载

⭐ > 运算符重载

⭐ >= 运算符重载

⭐ != 运算符重载

日期相关计算

⭐ += 运算符重载 

⭐ + 运算符重载 

⭐ -= 运算符重载

⭐ - 运算符重载(日期-天数)

⭐ 前置++ 运算符重载 

⭐ 后置++ 运算符重载

⭐后置-- 运算符重载

⭐前置-- 运算符重载

⭐ - 运算符重载(日期-日期)也构成函数重载

日期类规范和优化

✨Date.h

✨Date.cpp

 ✨Test.cpp


前言

经过类和对象三节内容的深入学习,为了更好的理解所学的知识,那我们就要运用所学的知识去实现日期类,完成一个简单的日期计算器所具备的功能。那么,我们现在开始吧~


日期类的实现

日期类其实是一个比较简单的类,而我们的目标是实现以下功能:

  • 日期比较大小
  • 日期+天数
  • 日期-天数
  • 日期 = 日期
  • 日期-日期
  • 日期++和日期--

实现上面这些功能,为了简化代码将会用到许多的运算符重载和一些函数的写法,便也起到了对前面学习的知识进行了回顾复习。

声明:为了讲解主要功能,我将代码全部放在一个.cpp文件里面,结尾会对日期类优化并且规范分文件。

 定义日期类

#include <iostream>
using namespace std;
class Date
{
public:

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

定义了三个私有的成员变量为_year,_month,_day分别代表年,月,日;这里变量名添加下划线是为了后续便于区分。

构造函数

Date(int year = 2024, int month = 3, int day = 14)
		:_year(year)
		,_month(month)
		,_day(day)
	{}

使用初始化列表进行初始化,传递了三个缺省参数。 

拷贝构造函数

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

其实在这个类的实现中, 我们是不许要写拷贝构造函数的,这里只是做一个复习。

赋值运算符重载

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

赋值运算符需要有返回值,由于这块程序走完this指针还在,所以可以用传引用返回来提高效率。

日期打印函数 

void Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

 获取某月天数

int GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);
	int Month_Day_array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//数组下标对应月份
	//2月份在闰年有29天,在平年有28天所以这要单独进行判断
	if (month == 2)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
	}
	return Month_Day_array[month];
}

这个函数非常重要,它的存在是为了判断某一年某个月共有多少天,因为存在平年与闰年而导致二月的天数是不一样的,这里便完美的解决了这个问题。

有了这个函数,便可以对构造函数进行优化

Date(int year, int month, int day)
{
	if ((month > 0 && month < 13)&&(day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期非法" << endl;
	}
}

 日期类大小比较

很多人会想,日期之间的比较关系该怎么比较呢,由于编译器是无法完成自定义类型之间的比较,那该如何比较日期那便由我们自己来决定了,我们将原本只能够进行内置类型比较的运算符重载,来实现日期之间的比较。

⭐ == 运算符重载 

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

 对于日期而言,实现 == 运算符重载是非常容易的,只要年月日分别相等即可。

⭐ < 运算符重载

bool operator<(const 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;
	}
}

对于日期而言,实现<运算符重载,采用的逻辑是年小就是小,年相等月小就是小,年月都相等天小就是小;其他的情况全都返回0。

⭐ <= 运算符重载

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

当 == 和 < 运算符重载写好后,我们便可以直接结合逻辑运算符利用起来,可以大大节省代码量。 

⭐ > 运算符重载

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

⭐ >= 运算符重载

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

⭐ != 运算符重载

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

提醒: 在运用时需要注意重载的顺序。

日期相关计算

⭐ += 运算符重载 

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

对于日期类+=天数,这里我们要去想,如果日期加上天数后没有超过当月的总天数时,可以直接得到结果;但是,如果超过时就需要往月进位,当月超过12时就应该往年进位此时需将月份重置成1月份。有了这个思想,那用代码呈现出来就没有那么困难了,使用一个while循环在内部再进行一个判断。

⭐ + 运算符重载 

Date operator+(int day)
{
	Date tmp(*this);//复制一个临时对象
	tmp += day;//直接使用已经重载好的运算符
	return tmp;
}

当有了+=,我们再去实现+的重载变得格外容易,我们只要注意+是不会改变自身的值,那我们就需要拷贝一份临时对象,对临时对象使用+=后返回这个临时变量,这里是临时变量运算结束便直接销毁,所以必须采用传值返回。

⭐ -= 运算符重载

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

对于日期-=天数就稍微复杂一点,不过我们可以这样去思考,如果日期的天-天数得到的值是正数,便可以直接得到结果;如果日期的天-天数得到的值<=0的话,那月份就该减1,然后此时的日期的天需要加上当前月的总天数;其中还需要做一个判断,当月份减为0时就应该年减1且将月份重置成12。下图便于理解,

⭐ - 运算符重载(日期-天数)

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

 当有了-=,再去实现-运算符重载同样也变得非常容易,直接调用-=即可。

⭐ 前置++ 运算符重载 

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

直接使用+运算符重载,便可以实现。

⭐ 后置++ 运算符重载

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

我们知道后置++,是先返回值再加1;那么就需要拷贝一份临时变量,使用this指针对自己加1,然后返回临时变量。

⭐后置-- 运算符重载

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

⭐前置-- 运算符重载

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

⭐ - 运算符重载(日期-日期)也构成函数重载

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

日期-日期得到天数,所以返回值应该设定为整型,参数使用传引用传参;这里可以使用计数器的思路解决,创建两个临时对象max和min分别拷贝自身和参数,再设立一个标签为1,然后需要做个简单的判断大小为了得到小日期-大日期的情况产生的负数;再创建一个计数的count,然后使用循环只要min != max,那么min就+1此时count也就+1,直到相等时跳出循环,此时count便是两个日期相差的天数。

日期类规范和优化

✨Date.h

#pragma once
#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:
	Date(int year = 2024, int month = 3, int day = 14);
	int GetMonthDay(int year, int month);
	void Print();
	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);
	Date& operator=(const Date& d);
	Date& operator+=(int day);
	Date operator+(int day);
	Date& operator-=(int day);
	Date operator-(int day);
	int operator-(const Date& d);
	Date& operator++();
	Date operator++(int);//后置
	Date& operator--();
	Date operator--(int);//后置
private:
	int _year;
	int _month;
	int _day;
};

✨Date.cpp

#include "Date.h"
Date::Date(int year, int month, int day)
{
	if ((month > 0 && month < 13)&&(day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期非法" << endl;
	}
}
//获取某一年某一个月的天数
int Date::GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);
	int Month_Day_array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//数组下标对应月份
	//2月份在闰年有29天,在平年有28天所以这要单独进行判断
	if (month == 2)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
	}
	return Month_Day_array[month];
}
//打印日期
void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}
//d1 == d2
bool Date::operator == (const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
//d1<d2
bool Date::operator<(const 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;
	}
}
//d1<=d2
bool Date::operator<=(const Date& d)
{
	return *this < d || *this == d;
}
//d1>d2
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}
//d1!=d2
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}
//d1>=d2
bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

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

//d1+100
Date Date::operator+(int day)
{
	Date tmp(*this);//复制一个临时对象
	tmp += day;//直接使用已经重载好的运算符
	return tmp;
}
//d1-=100
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return (*this += -day);
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//d1-100
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
//++d1
Date& Date::operator++()
{
	*this = *this + 1;
	return *this;
}
//d1++
	Date Date::operator++(int)
	{
		Date tmp(*this);
		*this = *this + 1;
		return tmp;
	}
//--d1
	Date& Date::operator--()
	{
		*this -= 1;
		return *this;
	}
//d1--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}//d1-d2
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (min != max)
	{
		++min;
		++count;
	}
	return count * flag; 
}

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

//>>(流提取)
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

 ✨Test.cpp

#include "Date.h"
void TestDate1()
{
	Date d1(2024, 3, 14);
	d1.Print();
	Date d2(d1);
	d2 += 100;
	d2.Print();
	Date d3(d1);
	d3 += -100;
	d3.Print();
}
void TestDate2()
{
	Date d1;
	Date d2;
	d2 = d1 + 5000;
	d2.Print();
}
void TestDate3()
{
	Date d1;
	++d1;
	d1.Print();
}

void TestDate4()
{
	Date d1;
	d1 -= 1000;
	d1.Print();
}
void TestDate5()
{
	Date d1;
	Date d2(1998, 5, 26);
	cout << (d1 - d2) << endl;

}
void TestDate6()
{
	Date d1;
	Date d2(1, 1, 1);
	cout << d1 << d2 << endl;
}
void TestDate7()
{
	Date d1;
	cin >> d1;
	cout << d1;
}
int main()
{
	TestDate7();
	return 0;
}

相信有了日期类的练习,一定会对前面学的知识在掌握层面上更上一层楼的!

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值