C++运算符重载(日期类的运算符重载为例)

一、运算符重载简介

C++为了增强代码的可读性,引入了运算符重载。

运算符重载是一个拥有特殊函数名的函数,其函数名为 operator+重载的运算符,其余的返回值类型和参数列表与普通函数类似。调用时可以使用函数名调用,也可以直接使用重载的运算符来调用。

如下代码,d1 == d2会被编译器替换为operator==(d1, d2);d1 == d2会被编译器替换为d1.operator==(d2)

运算符重载注意事项:

1.不能用operator连接其他符号来创建新的运算符,如operator@(只能是C/C++中已存在的运算符)

2.不能改变用于内置类型的运算符含义

3.以下五个运算符不能重载:

.(类成员访问运算符)

.*(类成员指针访问运算符)

::(域运算符)

sizeof(长度运算符)

? :(条件运算符)

二、重载日期类中的运算符 ==  <  <=  >  >=  

1.全局的运算符重载(全局运算符重载时,类中的成员变量需要改为公有的,否则需要提供Get接口或者利用友元解决,此处以成员变量为公有的为例)

class Date {
public:
	int _year = 1;
	int _month = 1;
	int _day = 1;
public:
	//有参构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
};
//Date类==重载
bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year
		&& d1._month == d2._month
		&& d1._day == d2._day;
}
//Date类<重载
bool operator<(const Date& d1, const Date& d2)
{
	if (d1._year < d2._year)
	{
		return true;
	}
	else if (d1._year == d2._year)
	{
		if (d1._month < d2._month)
		{
			return true;
		}
		else if (d1._month == d2._month)
		{
			if (d1._day < d2._day)
			{
				return true;
			}
		}
	}
	return false;
}

//Date类>重载
bool operator>(const Date& d1, const Date& d2)
{
	return !((d1 == d2) || (d1 < d2));
}

//Date类<=重载
bool operator<=(const Date& d1, const Date& d2)
{
	return (d1 < d2) || (d1 == d2);
}

//Date类>=重载
bool operator>=(const Date& d1, const Date& d2)
{
	return (d1 > d2) || (d1 == d2);
}

int main()
{
	Date d1(2024, 2, 17);
	Date d2(2024, 2, 16);

	cout << operator==(d1, d2) << endl;//0
	cout << (d1 == d2) << endl;//0

	cout << operator<(d1, d2) << endl;//0
	cout << (d1 < d2) << endl;//0

	cout << operator>(d1, d2) << endl;//1
	cout << (d1 > d2) << endl;//1

	cout << operator>=(d1, d2) << endl;//1
	cout << (d1 >= d2) << endl;//1

	cout << operator<=(d1, d2) << endl;//0
	cout << (d1 <= d2) << endl;//0
	return 0;
}

2.运算符重载为成员函数

class Date {
public:
	int _year = 1;
	int _month = 1;
	int _day = 1;
public:
	//有参构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//Date类==重载
	bool operator==(const Date& d)
	{
		return this->_year == d._year
			&& this->_month == d._month
			&& this->_day == d._day;
	}
	//Date类<重载
	bool operator<(const Date& d)
	{
		if (this->_year < d._year)
		{
			return true;
		}
		else if (this->_year == d._year)
		{
			if (this->_month < d._month)
			{
				return true;
			}
			else if (this->_month == d._month)
			{
				if (this->_day < this->_day)
				{
					return true;
				}
			}
		}
		return false;
	}
	//Date类>重载
	bool operator>(const Date& d)
	{
		return !((*(this) < d) || (*(this) == d));
	}
	//Date类<=重载
	bool operator<=(const Date& d)
	{
		return ((*this) < d) || (*(this) == d);
	}
	//Date类>=重载
	bool operator>=(const Date& d)
	{
		return (*(this) > d) || (*(this) == d);
	}
};

int main()
{
	Date d1(2024, 2, 17);
	Date d2(2024, 2, 16);

	cout << d1.operator==(d2) << endl;//0
	cout << (d1 == d2) << endl;//0

	cout << d1.operator<(d2) << endl;//0
	cout << (d1 < d2) << endl;//0

	cout << d1.operator<=(d2) << endl;//0
	cout << (d1 <= d2) << endl;//0

	cout << d1.operator>(d2) << endl;//1
	cout << (d1 > d2) << endl;//1

	cout << d1.operator>=(d2) << endl;//1
	cout << (d1 >= d2) << endl;//1

	return 0;
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南林yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值