Date(日期类):各种接口的实现

本文详细探讨了Date类中各种接口的实现,包括关键注意事项,如参数传递方式、赋值操作符的调用以及this指针的使用。提醒读者在实现过程中需关注指针与引用的差别,正确调用赋值操作符,并理解this关键字的作用。

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

问题

class Date 
{
public:    
	Date(int year = 1900, int month = 1, int day = 1);//构造函数
	Date(const Date& d);//拷贝函数
	Date& operator=(const Date& d);//赋值
	Date operator+(int days);// 从当前日期 + N 天

	Date operator-(int days);// 从当前日期 - N天
	int operator-(const Date& d);//两个日期相差的天数
	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;
};

下面实现函数:

#include<iostream>
#include<stdlib.h>

using namespace std;


class Date
{
public:


	int Monthday(int year, int month)//计算这个月有多少天
	{
		int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 2)
		{
			if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0)
			{
				return 29;
			}
			return 28;
		}
		return a[month];
	}

	Date(int year = 1900, int month = 1, int day = 1)//构造
	{
		_year = year;
		_month = month;
		_day = day;
	}

	Date(const Date& d)//拷贝
	{
		if (!(this == &d))//判断是不是自己赋值给自己
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
	}

	Date& operator=(const Date& d)//赋值,初始的定义如果是 = 的话是拷贝
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}

	Date operator+(int days)//思路:总共要加多少天,先把这个月填满,然后用days减去填满这个月用的天数。
	{
		while ((_day + days) >= Monthday(_year,_month))//判断要加的天数是否大于这个月的最大天数
		{
			days -= (Monthday(_year, _month) - _day);//先减去这月要补的天数
			if (_month == 12)//特殊条件,如果12月的话,年要进位
			{
				_year += 1;
				_month = 1;
			}
			_day = 0;
			_month += 1;
			
		}
		_day = _day + days;//如果要加的天数的日期没有超过这个月的最后一天
		
		return *this;//返回*this
	}

	Date operator-(int days)
	{
		while ((_day - days) <= 0)
		{
			days -= _day;
			if (_month == 1)
			{
				_year -= 1;
				_month = 12;
			}
			_day = Monthday(_year, _month - 1);
			_month -= 1;
			
		}
		_day = _day - days;
		return *this;
	}

	int operator-(const Date& d)
	{
		Date max = *this;//不改变d和this的原来数据
		Date min = d;
		int flag = 1;
		int days = 0;
		if (min >= max)//min一定要是相对较小的,不然会有死循环
		{
			Date tmp = max;
			max = min;
			min = tmp;
			flag = -1;
		}

		while (min != max)//一天一天加,知道min = max为止
		{
			++min;
			++days;
		}
		return days;
	}


	Date& operator++()//前置++,返回改变后this的值
	{
		*this + 1;
		return *this;
	}
	
	Date operator++(int)//后置++,返回为改变前this的值
	{
		Date tmp(*this);
		*this + 1;
		return tmp;
	}
	
	Date& operator--()
	{
		*this = *this - 1;
		return *this;
	}

	Date operator--(int)
	{
		Date tmp(*this);
		*this + 1;
		return tmp;
	}
//判断日期大小
	bool operator > (const Date& d)const//判断<
	{
		return (_year > d._year)
			|| (_year == d._year && _month > d._month)
			|| (_year == d._year && _month == d._month && _day > d._day);
	}

	bool operator>=(const Date& d)const//与<属于对立事件,直接调用<的结果,取反
	{
		return !(*this < d);//注意*this和d是同一个级别
	}

	bool operator<(const Date& d)const
	{
		return (_year < d._year)
			|| (_year == d._year && _month < d._month)
			|| (_year == d._year && _month == d._month && _day < d._day);
	}

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

	bool operator==(const Date& d)const
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}
	bool operator!=(const Date& d)const
	{
		return !(*this == d);
	}

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

实现:

//判断大小
void Test1()
{
	Date d1(2019, 1, 17);
	Date d2(2019, 1, 18);
	
	cout << (d1 >= d2) << endl;
	cout << (d1 <= d2) << endl;
	cout << (d1 != d2) << endl;
	cout << (d1 > d2) << endl;
	cout << (d1 < d2) << endl; 
	cout << (d1 == d2) << endl;

}
//日期加减
void Test2()
{
	//不能直接导入cout中
	Date d1(2019, 1, 17);
	d1 + 15;
	d1.Print();
	d1 - 17;
	d1++(0).Print();
	(++d1).Print();//调用前置++,要加括号
}

//两个日期的差值
void Test3()
{
	Date d1(2019, 1, 17);
	Date d2(2019, 2, 21);
	cout << (d1 - d2) << endl;

}

int main()
{
	Test1();
	Test2();
	Test3();
	system("pause");
	return 0;
}

提醒:

1.注意传过去的,是指针还是引用。
2.注意赋值操作符是怎么调用的
3.this = d
4.Date
const this = *this

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值