运算符重载(实现日期类)

对基本的数据类型,都支持+-*/等操作,对于自定义类型,要实现这些操作就要自己编写函数。这时候,C++提供了运算符重载的语法,及简化了调用的代码,又增加了可读性!

运算符重载的特征:
  • operator+合法的运算符 构成函数名(eg operator<);
  • 运算符重载后,不能改变运算符的优先级/结合性/操作数个数。

下图就是改变了操作数的个数导致的错误:


注:5个C++不能重载的运算符:.*      ::        sizeof          ?:         .

实现日期类:

.h文件:

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


class Date
{
public:
	//构造函数
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		if (!IsValid())
			assert(false);
	}
	//析构函数
	~Date()
	{}
	//其它成员函数
	int GetMonthDays(int year, int month);
	bool IsValid();
	void show();
	
	Date& operator=(Date& d);
	bool operator==(Date& d);
	bool operator!=(Date& d);
	bool operator>(Date& d);
	bool operator<(Date& d);
	bool operator>=(Date& d);
	bool operator<=(Date& d);
	Date operator+(int day);
	Date& operator+=(int day);
	Date operator-(int day);
	Date& operator-=(int day);
	int operator-(Date& d);//两日期相减
	Date& operator++();      //前置 
	Date operator++(int);    //后置
	Date& operator--();      //前置 
	Date operator--(int);    //后置
private:
	int _year;
	int _month;
	int _day;
};

.cpp文件:

#include "Date.h"


int Date::GetMonthDays(int year, int month)
{
	int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = days[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		day = 29;
	return day;
}
bool Date::IsValid()
{
	return _year >= 0 && _month > 0 && _month<13 && _day>0 && _day < GetMonthDays(_year, _day);
}
void Date::show()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
Date& Date::operator=(Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
bool Date::operator==(Date& d)
{
	return _year == d._year&&_month == d._month&&_day == d._day;
}
bool Date::operator!=(Date& d)
{
	return !(*this == d);
}
bool Date::operator>(Date& d)
{
	if (_year > d._year || (_year == d._year&&_month > d._month) || (_year == d._year&&_month == d._month&&_day > d._day))
		return true;
	return false;
}
bool Date::operator<(Date& d)
{
	return !(*this == d) && !(*this>d);
}
bool Date::operator>=(Date& d)
{
	return !(*this < d);
}
bool Date::operator<=(Date& d)
{
	return !(*this>d);
}
Date Date::operator+(int day)
{
	if (day < 0)
		return *this - (-day);
	Date tmp(*this);
	tmp._day += day;
	while (tmp._day > GetMonthDays(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDays(tmp._year, tmp._month);
		tmp._month++;
		if (tmp._month > 12)
		{
			tmp._month = 1;
			tmp._year++;
		}
	}
	return tmp;
}
Date& Date::operator+=(int day)
{
	*this = *this + day;
	return *this;
}
Date Date::operator-(int day)
{
	if (day < 0)
		return *this + (-day);
	Date tmp(*this);
	tmp._day -= day;
	while (tmp._day <= 0)
	{
		if (tmp._month == 1)
		{
			tmp._day += GetMonthDays(tmp._year, 12);
			tmp._month = 12;
			tmp._year--;
		}
		else
		{
			tmp._day += GetMonthDays(tmp._year, tmp._month - 1);
			tmp._month--;
		}
	}
	return tmp;
}
Date& Date::operator-=(int day)
{
	*this = *this - day;
	return *this;
}
int Date::operator-(Date& d)
{
	Date max(*this);
	Date min(d);
	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (min < max)
	{
		++min;
		++count;
	}
	return flag*count;
}
Date& Date::operator++()      //前置 
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)    //后置
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date::operator--()      //前置 
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)    //后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值