C++实现日期类

初步学习了类和对象后,为了能学以致用,我想用类和对象实现一个日期类,能实现日期的一些计算方法!

1.功能
1)日期的大小判断
2)日期变化的日期加减天数
3)计算两个日期间隔天数
4)日期变化的日期加减天数

2.实现代码
我把接口的声明和类的定义放在了Date1.h文件里:

#pragma once

#include<iostream>

using std::cout;
using std::endl;

class Date
{
public:
	inline int GetMonthDay(int year, int month) const  //获取每月的具体天数
	{
		static int Montharry[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if ((month == 2)   //对2月进行特殊对待,闰年29天,非闰年28天
			&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){
			return 29;
		}
		return Montharry[month];
	}
	Date(int year,int month,int day)    //自定义的构造函数
	{
		if (year >= 1900                             
			&& month > 0 && month<13
			&& day>0 && day <= GetMonthDay(year, month)){   //判断天数是否符合要求
			_year = year; 
			_month = month;
			_day = day;
		}
		else
			cout << "非法日期!" << endl;
	}
	~Date()
	{
		cout << "~Date()" << endl;
	}

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

	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++();		// 前置++
	Date operator++(int);	// 后置++

	Date operator--();		// 前置--
	Date operator--(int);	// 后置--

	Date operator+(int day) const;   //日期不变化的日期加减天数
	Date operator-(int day) const;

	Date& operator+=(int day);      //日期变化的日期加减天数
	Date& operator-=(int day);

	int operator-(const Date&d);   //两日期间隔天数
private:
	int _year;
	int _month;
	int _day;
};

接口的实现我放在了Date1.cpp文件里:

#include "Date1.h"

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){
			if (_day > d._day){
				return true;
			}
		}
	}
	return false;
}

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

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

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

bool Date::operator>=(const Date& d) const
{
	if ((*this == d) || (*this > d)){
		return true;
	}
	return false;
}

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

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

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

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

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

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

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

Date Date::operator+(int day) const
{
	Date res(*this);
	res += day;
	return res;
}
Date Date::operator-(int day) const
{
	Date res(*this);
	res -= day;
	return res;
}

int Date::operator-(const Date&d)
{
	if (*this < d){
		int cnt = 0;
		Date res(*this);
		while (1){
			res += 1;
			cnt++;
			if (res == d){
				return cnt;
			}
		}
	}
	else if(*this > d){
		int cnt = 0;
		Date ret(d);
		while (1){
			ret += 1;
			cnt++;
			if (ret == *this){
				return cnt;
			}
		}
	}
	return 0;
}

除了最后一个接口其他都比较好实现,因为实现了>,<=就出来了,只需要!一下就行,实现了==,!=就出来了,实现了+=,关于+的就都出来了,实现了-=,关于-的就都出来了,下面来说下最后一个的思想就是,以大的日期为最终值,看小的日期加到大的日期要加多少次,这个次数就是两日期间隔的天数。

测试函数我放在了test.cpp文件里:

#include<iostream>
#include "Date1.h"

int main()
{
	Date d1(2019, 4, 30);
	d1.Print();
	Date d2(2018, 5, 13);
    d2.Print();
	Date d3(2020, 1, 31);
	d3.Print();
	Date d4(2019, 6, 6);
	d4.Print();
	cout<<d1.operator>(d2)<<endl;
	cout << d1.operator==(d2) << endl;
	cout << d1.operator!=(d2) << endl;
	cout << d1.operator<(d2) << endl;
	cout << d1.operator>=(d2) << endl;
	cout << d1.operator<=(d2) << endl;
	
	d1.operator+=(30);   //d1变了
	d1.Print();			//2019-5-30
	
	d2.operator++();     //d2变了
	d2.Print();			//2019-5-14
	
	Date d = d2.operator++(5);   //d2m没变
	d.Print();			//2019-5-14 
	
	d1.operator-=(30);   //d1变了
	d1.Print();			//2019-4-30
	
	d1.operator--();     //d1变了
	d1.Print();			//2019-4-29  
	
	Date s = d1.operator--(5); //d1没变
	s.Print();			//2019-4-29

	d3.operator-=(100);   //d3变了
	d3.Print();          //2019-10-23

	Date q = d4.operator+(100);  //d4没变
	q.Print();             //2019-9-14

	int n = d3.operator-(d4);   //此时d3:2019-10-23   d4:2019-6-6
	cout << "两天相距:" << n << "天" << endl;
	return 0;
}

最后我想说这个日期类曾经是一个公司的面试题,就是考察我们类和对象的掌握情况,所以学好类和对象对我们来说至关重要!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期;日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期;日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值