C++入门<类和对象>练习->日期计算类

本文将展示一个日期计算类的代码

首先是大纲:

class Time
{
public:


	Time(int year = 2024, int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0);//构造函数


	Time(const Time& des);//拷贝构造函数
	

	Time operator+(int days);//对日期进行+几日的操作
	

	Time& operator+=(int days);//让日期+=几日的操作
	

	Time operator-(int days);;//对日期进行-几日的操作
	

	Time& operator-=(int days);//让日期-=几日的操作


	Time& operator++();//对日期前置++
	

	Time operator++(int);//对日期后置++


	Time& operator--();//对日期前置--
	

	Time operator--(int);//对日期后置--


	void Print();//输出该日期类记录的日期


	~Time();//析构


private:
	int _year;
	int _month;
	int _day;
	int _hour;
	int _minute;
	int _second;


	void Leap_year(int _year);//判断是否是闰年


	inline int Get_month_days(int month)//获取该月份天数
	{
		return Months[month];
	}

};


构造函数:

构造函数很重要,关系到你这个日期类初始化的正确与否,先上代码:

Time(int year, int month, int day, int hour, int minute, int second)
	:
	_year(year), _month(month), _day(day), _hour(hour), _minute(minute), _second(second)
{
	Leap_year(_year);
	bool flag = true;
	while (flag)
	{
		if (_month < 1 || _month>12)
		{
			cout << "月份非法,请重新输入>>";
			cin >> _month;
			continue;
		}
		flag = false;
	}
	flag = true;
	int T = Get_month_days(_month);
	while (flag)
	{
		if (_day < 1 || _day>T)
		{
			cout << "日期非法,请重新输入>>";
			cin >> _day;
			continue;
		}
		flag = false;
	}
	flag = true;
	while (flag)
	{
		if (_hour < 0 || _hour>23)
		{
			cout << "小时非法,请重新输入>>";
			cin >> _hour;
			continue;
		}
		flag = false;
	}
	flag = true;
	while (flag)
	{
		if (_minute < 0 || _minute>59)
		{
			cout << "分钟非法,请重新输入>>";
			cin >> _minute;
			continue;
		}
		flag = false;
	}
	flag = true;
	while (flag)
	{
		if (_second < 0 || _second>59)
		{
			cout << "秒钟非法,请重新输入>>";
			cin >> _second;
			continue;
		}
		flag = false;
	}

}

很多人的构造函数非常的简单化,直接将传过来的数据对类中的内置类型进行初始化,而完全不对其进行检查,导致后续会出很多问题,如出现时间错误,蹦出个2024年13月都有可能,所以在编写构造函数的时候,我们需要对其输入的数据进行检查,例如上面对月份的检查,如果month>12或者month<1则输出异常提醒然后让用户对月份进行重新输入,同理日期、时、分、秒也是一样的操作,这样一个简的构造函数就写成了。

析构函数:

析构函数也是非常重要的,它能帮助我们在对象即将走出自己的生命域前,将对象进行析构,重置对象数据,释放占用的内存等,有效避免了内存泄露的问题,不过这里我们日期类的内存是在栈上申请,在调用结束后就会回收,问题不大,如果是new,malloc出来的就需要我们写一个delete或free了。

:~Time()
{
	_year = 2024;
	_month = 1;
	_day = 12;
	_hour = 17;
	_minute = 38;
	_second = 35;
}

这里我们可以直接任意写一个时间即可,都行。

运算符重载函数:

下面是一些运算符重载的日期类代码

只对其中的+运算符重载函数进行说明

因为是+,不是+=,所以在使用+运算符时,被+的时间不能发生改变,而是返回了其+上对应天数的日期的拷贝,接下来是对该函数的思路讲解:

首先是定义一个Time类对象Temp,将被加的对象,也就是this指针指向的对象拷贝给它,然后对+的天数进行判断,因为我们无法保证用户输入的days一定是正数,有可能是负数,所以在此进行特殊判断,这里就不展开多讲。

接下来就是进行日期的计算,我们直接将+的天数days直接让Temp.day+=上,然后进行计算,使用Get_month_days函数取得对应年份对应月份的天数,然后进行循环,每次循环对Temp.day进行-=对应月份的天数,知道Temp.day的天数符合对应月份,此时日期就是正确的,其中Leap_year函数是进行年份判断,因为在Temp.day-=的过程中,月份会增加,有可能月份会超过12,此时年份就要+1,所以我们需要进行闰年的判断,使得月份合理

最后循环结束之后,将Temp的拷贝传回,此时this指向的时间不变,一个Time类的+运算符重载函数就完成了!

Time Time::operator+(int days)
{

	Time Temp = *this;
	if (days < 0)
	{
		int T = (-days);
		return Temp - T;
	}
	Temp._day += days;
	int _month_days = Get_month_days(Temp._month);
	while (Temp._day > _month_days)
	{
		Temp._day -= _month_days;
		Temp._month++;
		if (Temp._month > 12)
		{
			Temp._year++;
			Leap_year(Temp._year);
			Temp._month -= 12;
		}
		_month_days = Get_month_days(Temp._month);
	}
	return Temp;
}


Time& Time::operator+=(int days)
{
	Time T = *this;
	if (days < 0)
	{
		int T1 = (-days);
		*this = T - T1;
		return *this;
	}
	*this = T + days;
	return *this;
}


Time Time::operator-(int days)
{
	Time Temp = *this;
	if (days < 0)
	{
		int T = (-days);
		return Temp + T;
	}
	Temp._day -= days;
	int _month_days = Get_month_days(Temp._month);
	while (Temp._day <= 0 || Temp._month < 1)//判断日期
	{
		if (Temp._day == 0)
		{
			Temp._month--;
			if (Temp._month == 0)
			{
				Temp._year--;
				Leap_year(Temp._year);
				Temp._month += 12;
			}
			Temp._day = Get_month_days(Temp._month);
			break;
		}
		Temp._month--;
		if (Temp._month < 1)
		{
			Temp._year--;
			Leap_year(Temp._year);
			Temp._month += 12;
		}
		_month_days = Get_month_days(Temp._month);
		Temp._day += _month_days;

	}
	return Temp;
}

Time& Time::operator-=(int days)
{
	Time T = *this;
	if (days < 0)
	{
		int T1 = (-days);
		*this = T + T1;
		return *this;
	}
	*this = T - days;
	return *this;
}

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

Time Time::operator++(int)//后置++
{
	Time Temp = *this;
	(*this) += 1;
	return Temp;
}

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

Time Time::operator--(int)//后置++
{
	Time Temp = *this;
	(*this) -= 1;
	return Temp;
}



bool Time::operator >(const Time& obj)
{
	double cmp1 = Time_sum(*this);
	double cmp2 = Time_sum(obj);
	return cmp1 > cmp2;
}

bool Time::operator <(const Time& obj)
{
	double cmp1 = Time_sum(*this);
	double cmp2 = Time_sum(obj);
	return cmp1 < cmp2;
}

bool Time::operator ==(const Time& obj)
{
	double cmp1 = Time_sum(*this);
	double cmp2 = Time_sum(obj);
	return cmp1 == cmp2;
}

bool Time::operator !=(const Time& obj)
{
	double cmp1 = Time_sum(*this);
	double cmp2 = Time_sum(obj);
	return !(cmp1 == cmp2);
}

bool Time::operator >=(const Time& obj)
{
	return (*this) > obj || (*this) == obj;
}

bool Time::operator <=(const Time& obj)
{
	return (*this) < obj || (*this) == obj;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值