C++之日期类的实现之 默认成员函数和运算符重载的综合应用(+、-、=、<、>、+=、-=)

在完成日期类时,涉及到对应月份天数的限制,先构造出下面的函数,用以判断当月天数。

//获取某一月的天数
int Date::GetMonthDay(int year, int month)
{
    
    
	static int daysArray[13] = {
    
     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int days = daysArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
    
    
		days = 29;
	}
	return days;
}

Date.h文件

#pragma once
#include<iostream>
using namespace std;
class Date
{
    
    
public:
	int GetMonthDay(int year, int month);
	void Print();

	Date(int year = 1900, int month = 1, int day = 1);

	/*拷贝构造函数*/

	Date(const Date& d);

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);
	// 析构函数
	~Date();

	//d1 += 10
	Date& operator+=(int day);

	//d1 + 10
	Date operator+(int day);
	 
	//d1 -= 10
	Date& operator-=(int day);

	//d1 - 10
	Date operator-(int day);

	//++d1
	//d1.operator++(&d1)
	Date& operator++();

	//d1++
	//d1.operator++(&d1,0)
	//为了跟前置++区分,加了一个参数,但是这个参数不使用
	Date operator++(int);

	bool operator>(const Date& d);
	// ==运算符重载
	bool operator==(const Date& d);
	// >=运算符重载
	inline bool operator >= (const Date& d);
	// <运算符重载
	bool operator < (const Date& d);
	// <=运算符重载
	bool operator <= (const Date& d);
	// !=运算符重载
	bool operator != (const Date& d);
	// 日期-日期 返回天数
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp文件

#include"Date.h"

//拷贝构造函数
 Date::Date(const Date& d)
{
    
    
	 _year = d._year;
	 _month = d._month;
	 _day = d._day;
}
 // 赋值运算符重载
 // d2 = d3 -> d2.operator=(&d2, d3)
 Date& Date::operator=(const Date& d)
 {
    
    
	 if (this != &d)
	 {
    
    
		 _year = d._year;
		 _month = d._month;
		 _day = d._day;
	 }
	 return *this;
 }
 
 // 析构函数
 Date:: ~Date()
 {
    
    
	 // 清理工作,Date类中没有需要清理的资源
	 // 所以严格来说Date不需要写析构函数,因为我们不写,编译器默认生成就可以用
	 cout << "~Date()" << endl;
 }

 //打印函数
void Date::Print()
{
    
    
	cout << _year << "-" << _month << "-" << _day << endl;
}

//获取某一月的天数
int Date::GetMonthDay(int year, int month)
{
    
    
	static int daysArray[13] = {
    
     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int days = daysArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
    
    
		days = 29;
	}
	return days;
}
//判断日期是否非法
	Date::Date(int year, int month, int day) 
{
    
    
	_year = year;
	_month = month;
	_day = day;
	//cout << "Date(int year,int month,int day)" << endl;
	if (!(year >= 0 && month > 0 && month < 13 && day > 0 && day < GetMonthDay( year,  month)))
	{
    
    
		cout << "日期非法" << endl;
	}
		
}
	//d1 += 10
    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;
	}
	//d1 + 10
	//要实现+,d1的值是不能被改变的,所以拷贝构造一个ret
	//ret为+后的结果
	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;
		//	}
		//}
		Date ret(*this);
		ret += day;//直接调用+=,ret.operator(day)
		return ret;
 	}
	//d1-=day
	Date& Date::operator-=(int day)
	{
    
    
		if (day < 0)
		{
    
    
			return *this += -day;
		}
		//日期是否合法
		_day -= day;
		while (_day < 0)
		{
    
    
			_day += GetMonthDay(_year, _month);
			_month--;
			if (_month == 0)
			{
    
    
				_year--;
				_month = 1;
			}
		}
		return *this;
	}
	//d1-day
	Date Date::operator-(int day)
	{
    
    
		Date ret(*this);
		ret -= day;
		return ret;
	}
	//++d1 等价于 d1 += 1
	Date& Date::operator++()
	{
    
    
		*this += 1;//直接
		return *this;
	}
	//d1++
	Date Date::operator++(int)
	{
    
    
		Date ret(*this);//要返回++之前的值,所以先拷贝构造一个保存起来
		*this += 1;
		return ret;//出了作用域就不存在了,不能用引用返回
	}
	// ==运算符重载
	bool Date :: operator==(const Date& d)
	{
    
    
		return _year == d._year && _month == d._month && _day == d._day;
	}
	bool Date::operator>(const Date& d)
	{
    
    
		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)
	{
    
    
		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 !(*this == d);
	}
	// 日期-日期 返回天数
	int Date::operator-(const Date& d)//函数名相同,参数不同,构成重载
	{
    
    
		//直接进行日期之间的相减很难
		Date max = *this, min = d;
		int flag = 1;
		if (*this < d)
		{
    
    
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (min != max)//n 的数就是相差的天数
		{
    
    
			++n;
			++min;
		}
		return n*flag;
	} 


猜你喜欢

转载自blog.csdn.net/du1232/article/details/114676347