C++日期类的实现

本文介绍了一个C++日期类的设计与实现细节,包括日期的构造、日期加减运算、日期比较等功能,并实现了日期对象的加减天数、日期之间的天数差计算等操作。

C++日期类

声明对象年、月、日,创建构造函数,实现日期类。

#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

class Date
{

 private:
	int year;
	int month;
	int day;

 public:
	 // 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)

	{
		if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > getDay(year, month))

		{
			cout << "日期无效" << endl;
			cout << "日期重置为:1900-1-1" << endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}
		else
		{
			_year = year;
			_month = month;
			_day = day;
		}
	}

实现函数中年月日的初始化,定义每月份天数数组,判断2月份天数、是否为闰年。

// 获取某年某月的天数
	int getDay(int year, int month)
	{
		static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int day = days[month];
		if (month == 2)
		{
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				++day;
		}
		return day;
	}

注:
1. 日期类创建中没有开辟新空间或打开文件等操作,不需要手动实现析构函数,因此使用系统默认析构函数。

2. 日期类的拷贝构造,使用编译器默认浅拷贝就足够。

赋值运算符重载:用来实现日期加减天数日期加减日期日期比较赋值等。

日期大小比较,用bool类型返回值。

    // ==运算符重载
	bool operator==(const Date & d ) 
	{
		return _year == d.year
			&& _month == d.month
			&& _day == d.day;
	}

	// !=运算符重载
	bool operator!=( const Date & d )
	{
		return!(*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);
	}

	 //<运算符重载
	bool operator<( const Date & d )
	{
		return !(*this >= d);
	}

日期加减天数

// 日期-天数
	Date operator -(int day)
	{
		Date tmp(*this);
		return tmp -= day;
	}

	// 日期+天数
	Date operator +(int day)
	{
		Date tmp(*this);
		return tmp += day;
	}

日期加减日期


	// 日期+=天数
	Date& operator+=(const int day)
	{

		if (day < 0)
		{
			return *this -= (-day);
		}

		_day += day;

		while (_day > getDay(_year, _month))
		{
			_day -= getDay(_year, _month);
			++_month;

			if (month == 13)
			{
				month = 1;
				++_year;
			}
		}
		return *this;
	}

	// 日期-=天数
	Date& operator-=(int day)
	{
		if (day < 0)
		{
			return  *this += (-day);
		}
		_day -= day;
		while (day <= 0)
		{
			--_month;
			if (_month = 0)
			{
				--_year;
				_month = 12;
			}
			_day += getDay(_year, _month);
		}
		return *this;
	}

日期减日期,返回天数

// 日期-日期 返回天数
	int operator-(const Date &d)
	{
		int flag = 1;
		Date max = *this;
		Date min = d;
		if(*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int count = 0;
		while (min != max)
		{
			++min;
			count++;
		}
		return count * flag;
	}
	private :
		int _year;
		int _month;
		int _day;
};

前置++/–:

加/减1,返回相加/减之后的内容

显式定义的参数个数:0

返回值类型:引用/值:(拷贝/效率低)

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

后置++/–:

加/减1,返回相加/减之前的内容

显式定义的参数个数:1(参数没有实际意义,作为标记)

返回值类型:只能是值

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值