运算符重载详解,日期类型的实现

一、运算符重载

        当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编译报错。

        类中含有多种变量,原本的运算是不适用的,因此必须我们自己进行重载。

        运算符重载是具有特殊名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体 即operator + 运算符

        重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元 运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数 在这点上就需要我们注意参数的顺序,以免出现刚好相反的结果

        如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算 符重载作为成员函数时,参数⽐运算对象少⼀个。

        运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。

        .*(成员对象指针选择运算符)   ::(域作用限定符)   sizeof(求在内存中所占用的字节数的运算符)   ?: .(三目操作符) 注意以上5个运算符不能重载。

        重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。 C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。

        重载>时 ,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位 置,第⼀个形参位置是左侧运算对象,调⽤时就变成了 对象<<cout不符合使⽤习惯和可读性.重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类型对象。

二、日期类的实现

        既然了解了这么多,那我们也来小试牛刀吧,相信对各位来说都是小菜一碟!!

        

Date.h:

class Date
{
public:
	//构造 列表初始化
	/*Date(int year = 2004,int month = 1,int day = 1)
		:_year(year)
		,_month(month)
		,_day(day)
	{}*/
	Date(int year = 1900, int month = 1, int day = 1);

	//析构
	~Date()
	{}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	//1月31天,2月28天(闰年有29天),3月31天,4月30天,5月31天,6月30天,
	// 7月31天,8月31天,9月30天,10月31天,11月30天,12月31天
	int GetMonthDay(int year, int month)
	{
		int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			//是闰年 二月份为29天
			return 29;
		}
		else
		{
			return MonthDay[month];
		}

	}

	int GetYear()const
	{
		return _year;
	}
	int GetMonth()const
	{
		return _month;
	}
	int GetDay()const
	{
		return _day;
	}
	// 拷贝构造函数
    // d2(d1)
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	bool CheckDate();

	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;

	// d1 += 天数
	Date& operator+=(int day);
	Date operator+(int day) const;
	// d1 -= 天数
	Date& operator-=(int day);
	Date operator-(int day) const;
	
	// d1 - d2
	int operator-(const Date& d) const;
	// ++d1 -> d1.operator++()
	Date& operator++();
	// d1++ -> d1.operator++(0)
	// 为了区分,构成重载,给后置++,强⾏增加了⼀个int形参
	// 这⾥不需要写形参名,因为接收值是多少不重要,也不需要⽤
	// 这个参数仅仅是为了跟前置++构成重载区分
	Date operator++(int);
	
	Date& operator--();
	Date operator--(int);
	
private:
	int _year;
	int _month;
	int _day;
};
// 重载
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp:

#include"Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(CheckDate()))
	{
		cout << "非法日期" << endl;
	}
}
	
//检查日期是否正确
bool Date::CheckDate()
{
	if (_month > 12 || _month < 1 || _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}
//运算符重载
bool Date::operator<(const Date& d) const
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _day < d._day;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

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

bool Date::operator==(const Date & d) const
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

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

// d1 += 天数
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)
		{
			//原本是12月,加一个月后应是明年的一月
			_month = 1;
			++_year;
		}
	}
	return *this;
}
Date Date::operator+(int day) const
{
	//+的计算结果是加后的,而日期是不变的
	Date tmp = *this;
	tmp += day;
	return tmp;
}
// d1 -= 天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}
	_day -= day;
	while (_day < 1)
	{
		--_month;
		//小心,这加的是上一个月的日期
		if (_month == 0)
		{
			//原本是一月,减掉后应为去年12月
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

Date Date::operator-(int day) const
{
	//与+类似,返回结果是减后的,但原日期不变
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

// d1 - d2
int Date::operator-(const Date& d) const
{
	//两日期相减
	//先假设d1>d2,
	Date max = *this;
	Date min = d;

	//标记
	int falg = 1;

	//若假设错误
	if (max < min)
	{
		max = d;
		min = *this;
		//更改标记
		falg = -1;
	}
	//记录日期
	int countDay = 0;
	while (max != min)
	{
		++min;
		++countDay;
	}
	//标记的作用来了,若d1 > d1,则相减的结果为正
	//否则为负
	return countDay * falg;
}
// ++d1 -> d1.operator++()
Date& Date::operator++()
{
	//前置++,先++再使用,意味着原日期是会改变的
	*this += 1;
	return *this;
}
// d1++ -> d1.operator++(0)
// 为了区分,构成重载,给后置++,强⾏增加了⼀个int形参
// 这⾥不需要写形参名,因为接收值是多少不重要,也不需要⽤
// 这个参数仅仅是为了跟前置++构成重载区分
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;
}

// 重载
//返回值是为了能够连续输入和输出
ostream& operator<<(ostream& out, const Date& d)
{
	out << d.GetYear() << "/" << d.GetMonth() << "/" << d.GetDay() << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;

		if (!d.CheckDate())
		{
			cout << "输入日期非法:";
			d.Print();
			cout << "请重新输入!!!" << endl;
		}
		else
		{
			break;
		}
	}

	return in;
}

        okok!!

        以上就是全部内容了,谢谢观看!!!

                           

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值