【C++】实践运算符重载:日期类的实现

Date.h

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1);
	Date(const Date& d);
	~Date();
	Date& 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);
	bool operator!=(const Date& d);
   
	
	Date operator+(int dayn);
	Date operator+=(int dayn);
	Date operator-=(int dayn);
	Date operator-(int dayn);

	Date& operator++();
	Date operator++(int);
	int operator-(const Date& d);


	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		int monthday[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 monthday[month];
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include"Date.h"
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
Date::~Date()
{

}
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
bool Date::operator==(const Date& d)
{
	return d._year == _year && d._month == _month && d._day == _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);
}

//先实现+再+=
//Date Date::operator+(int dayn)
//{
//	Date tmp=*this;
//	tmp._day += dayn;
//	while(tmp._day> GetMonthDay(_year, _month))
//	{
//		tmp._day -= GetMonthDay(_year, _month);
//		tmp._month++;
//		if (tmp._month > 12)
//		{
//			tmp._year++;
//			tmp._month = 1;
//		}
//
//	}
//	return tmp;
//}
//Date Date::operator+=(int dayn)
//{
//	*this = *this + dayn;
//	return *this;
//}
//先+=再+:
Date Date::operator+=(int dayn)

{
	_day = _day + dayn;
	while(_day > GetMonthDay(_year, _month))
	{
		_day -=  GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int dayn)
{
	Date tmp = *this;
	tmp += dayn;
	return tmp;
}

//先-=再-:
Date Date::operator-=(int dayn)
{
	_day = _day - dayn;
	while (_day <= 0)
	{
		_day = _day + GetMonthDay(_year, _month - 1);
		_month--;
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}
Date Date::operator-(int dayn)
{
	Date tmp = *this;
	tmp -= dayn;
	return tmp;
}
//前置++
Date& Date:: operator++()
{
	*this = *this + 1;
	return *this;
}
//后置++:
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}


int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (*this < d)
	{
		int flag = -1;
		max = d;
		min = *this;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

注意:先实现+=再+,比先实现+再+=更优
因为operator+(int dayn)中需要调用构造函数,而+=不用,如果先实现+再实现+=,就需要调用两次构造函数。先+=再+只需调用一次构造函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值