简单的实现Date类----运算符重载

本文介绍了一个简单的Date类实现,重点在于运算符的重载,包括日期的加减运算、比较运算以及前后置增减运算。通过重载,使得日期的运算更加直观和方便。

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

简单实现一个Date类,主要是运算符的重载!

首先在Date类的实现过程中,需要注意以下几点:

日期+天数:

1.加的天数为负数的情况    
2.加之后的天数超过当月最大天数  ,月数+1,天数-已经加过的天数
3.月数+1之后大于12   年数+1
 

日期-天数:

1.减的天数为负数的情况
2.减之后的天数超过当月最大天数  ,月-1,天数-已经加过的天数
3.月数-1之后小于1   年数-1
 

在Getmonthyear时对二月应注意特殊判断:

if (IsLeapYear() && month == 2)
	{
		array[2] = 29;
	}

首先定义Date.h

#pragma once
#include<iostream>
using namespace std;



class Date
{
public:
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);

	//构造函数
	Date(int year = 0, int month = 10, int day = 1);
	//拷贝构造函数
	Date(const Date& d);
	//判断是否为闰年
	bool IsLeapYear()const;
	//运算符重载
	Date& operator=(const Date& d);
	Date operator+(int day); //日期加天数
	Date operator-(int day);   //日期减天数
	Date& operator++();     //前置++
	Date operator++(int);            //后置++
	Date& operator--();           //前置--
	Date operator--(int);            //后置--
	//比大小
	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;


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

在Date.cpp中实现函数重载

#include"Date.h"
#include<assert.h>
#include<iostream>
using namespace std;

Date::Date(int year, int month, int day)
	:_year(year),
	_month(month),
	_day(day)
{
	_year = year;
	_month = month;
	_day = day;
}

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

bool IsLeapYear()
{
	int year;
	assert(year > 0);
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
		return true;
	}
	return false;
}


int GetMonthYear(int year, int month)
{
	assert(month < 0 && month>13);
	int array[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (IsLeapYear() && month == 2)
	{
		array[2] = 29;
	}
	return array[month];

}

ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}

istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year;
	_cin >> d._month;
	_cin >> d._day;
	return _cin;

}

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

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

bool Date::operator!=(const Date& d)const
{
	return this->_year != d._year||this->_month != d._month||this->_day != d._day;
}

bool Date::operator>(const Date& d)const    //大于
{
	if ((this->_year > d._year)
		||(this->_year==d._year&&this->_month>d._month)
		|| (this->_year == d._year&&this->_month == d._month&&this->_day > d._day)) {
		return true;
	}
	return false;
}

bool Date::operator>=(const Date& d)const
{
	return operator>(d) || operator==(d);
}

bool Date::operator<(const Date& d)const    ///小于
{
	if ((this->_year < d._year)
		|| (this->_year == d._year&&this->_month<d._month)
		|| (this->_year == d._year&&this->_month == d._month&&this->_day < d._day)) {
		return true;
	}
	return false;
}
bool Date::operator<=(const Date& d)const
{
	return operator<(d) || operator==(d);
}

//1.加的天数为负数的情况
//2.加之后的天数超过当月最大天数  ,月+1,天数-已经加过的天数
//3.月数+1之后大于12   年数+1
//4.剩余天数>
Date Date::operator+(int day)      
{
	if (day < 0) {
		return *this - (-day);
	}
	Date tmp = *this;
	tmp._day += day;
	while (tmp._day > GetMonthYear(tmp._year,tmp._month))
	{
		tmp._day -= GetMonthYear(tmp._year, tmp._month);
		tmp._month += 1;
		if (tmp._month == 13) {
			tmp._month = 1;
			tmp._year += 1;
		}
	}
	return tmp;
}

Date Date::operator-(int day)
{
	if (day < 0) {
		return *this - (0 - day);
	}
	Date tmp = *this;
	tmp._day -= day;
	while (tmp._day > GetMonthYear(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthYear(tmp._year, tmp._month);
		tmp._month -= 1;
		if (tmp._month == 0) {
			tmp._month = 12;
			tmp._year -= 1;
		}
	}
	return tmp;
}


Date& Date::operator++()    //前置++      this++
{
	/*Date tmp = *this;
	tmp._day += 1;
	if (tmp._day > GetMonthYear(tmp._year, tmp._month) {
		tmp._month += 1;
	*/

	*this = *this + 1;
	return *this;
}

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


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


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

这时就基本实现了Date类的基本功能,后续会添加Time类,继续完善。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值