【C++】实现一个Date类

本文详细介绍了C++中自定义Date类的设计与实现过程,包括构造函数、拷贝构造函数、析构函数,以及各种运算符重载,如加、减、比较等。通过这些重载,实现了日期的灵活运算和比较。此外,还展示了如何处理闰年和平年的月份天数,确保了日期的有效性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Date类的代码具体实现如下:

Date.h

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

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1);
	Date(const Date& d);
	Date& operator=(const Date& d);//赋值运算符重载
	~Date();
	void Print();
	int GetMonthDay(int year, int month);
	
	Date operator+(int day);
	Date& operator+=(int day);

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

	int operator-(const Date& d);//日期减日期

	//++d ->d.operator++(&d)
	Date& operator++();
	//d++ ->d.operator++(&d,1) 前置后置这样区分构成重载
	Date operator++(int);

	Date& operator--();
	Date operator--(int);


	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);
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include "Date.h"

int Date::GetMonthDay(int year, int month)
{
	static const int MonthArray[13] = { 0, 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 MonthArray[month];
}

//构造函数
Date::Date(int year , int month, int day)
{
	if (year >= 0 &&
		month > 0 && month < 13 &&
		day > 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}else
	{
		cout << "Date Invalid" << endl;
	}
}
//拷贝构造函数
//析构函数
Date::~Date()
{
}

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

bool Date:: operator>(const Date& d)
{
	return (_year > d._year)
		|| (_year == d._year && _month > d._month)
		|| (_year == d._year && _month == d._month && _day > d._day);
}
//复用 运算符重载
bool Date::operator>=(const Date& d)
{
	return *this > d || *this == d;
}
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator==(const Date& d)
{
	return _year == d._year && _month > d._month && _day > d._day;
}
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;//可以支持连续赋值
}
Date Date::operator+(int day)
{
	//Date ret(*this);
	//ret._day += day;
	//while (ret._day > GetMonthDay(ret._year, ret._month))
	//{
	//	ret._day -= GetMonthDay(ret._year, ret._month);
	//	ret._month++;
	//	if (ret._month == 13)
	//	{
	//		ret._year++;
	//		ret._month = 1;
	//	}
	//}
	//return ret;
	Date ret(*this);
	ret += day;
	return ret;
}

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::operator-(int day)
{
	//Date ret(*this);
	//ret._day -= day;
	//while (ret._day <= 0)
	//{
	//	--ret._month;
	//	if (ret._month == 0)
	//	{
	//		ret._year--;
	//		ret._month = 12;
	//	}
	//	ret._day += GetMonthDay(ret._year, ret._month);
	//}
	//return ret;
	Date ret(*this);
	ret -= day;
	return  ret;
}

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;
}
//日期减日期
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 n = 0;
	while (min != max)
	{

		++min;
		++n;
	}
	return n*flag;
}


//++d ->d.operator++(&d)
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//d++ ->d.operator++(&d,1) 前置后置这样区分构成重载
Date Date::operator++(int)
{
	Date tmp(*this);
	 *this += 1;
	return tmp;
}
//-> --d
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//-> d--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}



int main()
{
	Date d1(2000, 2, 29);
	d1.Print();

	Date d2(d1);//拷贝构造
	d2.Print();

	Date d3;
	d1 = d3;//赋值
	d3.Print();

	Date d4 = d1;//拷贝构造
	d4.Print();

	Date(2018, 11, 18).Print();//匿名对象

	Date d5(2018, 1, 1);
	Date d6 = d5 + 20;
	d6.Print();

	Date d7(2018, 1, 1);
	Date d8 = d7 - 10;
	d7.Print();

	Date d9(2018,11,14);
	d9.Print();
	(++d9).Print();
	d9.Print();
	(d9++).Print();
	d9.Print();

	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值