C++运算符重载

运算符重载


什么是运算符重载?运算符重载是C++中具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)

话不多说,直接上代码:
假设现在有一个日期类:

//头文件
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date

{

public:

	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

	//打印
	void Print();

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);

	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);

//具体实现
#include"Date.h"

// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	//创建一个数组来存储全年的月份
	int arr[] = {0.31,28,31,30,31,30,31,31,30,31,30,31};

	//判断是否为闰年
	if (month == 2 && (year % 400 == 0)|| (year % 100 != 0 && year % 4 == 0))
	{
		arr[2] = 29;
	}

	return arr[month];
}

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

// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	if (year > 0 && (month > 0 && month < 13) && (day <= Date::GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "输入错误" << endl;
	}
}

// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
}

现在我创建了两个对象,d1和d2,我想用d1给d2赋值,但是我已经定义好了,不能使用拷贝构造,怎么办,这个时候运算符重载就起作用了

// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)//这里的参数使用引用传参,可以节省空间,避免拷贝
{
	_year = d._year;
	_month = d._month;
	_day = d._day;

	return *this;
}

在这里插入图片描述

可以看到使用 = 运算符可以给d2赋上d1的值,如果要使用=运算符,两个对象的是同一个父类才行
那=运算符函数内部要有返回值呢?因为要实现连续赋值
在这里插入图片描述
那如果有人使用d1给d1赋值呢?为了避免这种情况,我们可以增加一个判断:
在这里插入图片描述
返回值尽量使用引用,如果新建一个对象返回,还要重新拷贝,不然直接返回本身。

+=运算符:
年月日 += N天,也就是N天后的年月日,普通加法肯定是不能运用到年月日上的,但是也大差不差,我们可以使用进位法来计算:
在这里插入图片描述

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;

	while (_day > Date::GetMonthDay(_year, _month))
	{
		_day -= Date::GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

那如果+=的是负数怎么办,这个下面再说

+运算符:
+,本身的日期不能改变,而是返回本身的日期+N后的日期:

// 日期+天数
Date Date::operator+(int day)
{
	//这里可以覆用+=运算符
	//创建一个新对象 = this
	//再用新对象 + day
	//然后返回这个新对象

	Date d1(*this);
	d1 += day;
	return d1;
}

在这里插入图片描述

d1并不会被改版,而是产生一个新的对象返回

-=运算符:

// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += Date::GetMonthDay(_year, _month);

	}

	return *this;
}

有了这个-=运算符,上面的+=运算符负数的问题也迎刃而解了,只需要增加一个判断,判断是否为负数:

// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= day * -1;
		return *this;
	}
	_day += day;

	while (_day > Date::GetMonthDay(_year, _month))
	{
		_day -= Date::GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

同样+运算符也可以使用这个办法,大大减少了代码量

前置++运算符:
前置++是先加后用,所以直接再*this上自增就可以,这里可以直接覆用+=运算符:

// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

在这里插入图片描述

后置++:
先用后加,所以是返回++之前的值,但是本身还要自增,可以先创建一个新对象,将this赋值给新对象,然后本身自增,再返回this:

// 后置++
Date Date::operator++(int)
{
	Date d1(*this);
	*this += 1;
	return d1;
}

在这里插入图片描述

前置–和后置–:
和前置++后置++一样,只需要将+=改成-=即可:

// 后置--
Date Date::operator--(int)
{
	Date d1(*this);
	*this -= 1;
	return d1;
}

// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

比较运算符:>
判断一个日期是否大于另一个日期:

// >运算符重载
bool Date::operator>(const Date& d)
{
	return _year > d._year
		|| _month > d._month
		|| _day > d._day;
}

如果年大,就是大,如果年小,但是月大,也是大,如果年月都小,但是天大,也是大,如果年月日都不大于,则是false

==运算符:
判断两个日期是否相等:

// ==运算符重载
bool Date::operator==(const Date& d)
{
	//判断是否和自身比较
	if (this == &d)
		return true;

	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

年月日都相等才是相等

大于等于运算符:>=
一个日期大于或者等于另一个日期:
直接覆用> == 两个运算符:

// >=运算符重载
bool Date::operator >= (const Date& d)
{
	

	return *this > d || *this == d;
}

< 小于运算符
年小,就是小
年相等,月小就是小
年月都相等,天小就是小

// <运算符重载
bool Date::operator < (const Date& d)
{
	

	return _year < d._year
		|| _month < d._month
		|| _day < d._day;
}

<=运算符:

// <=运算符重载
bool Date::operator <= (const Date& d)
{
	
	return *this < d || *this == d;
}

!=运算符:
覆用==运算符,对结果进行取反:

// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}

日期减去日期,返回天数:

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	//首先判断哪个日期大,哪个日期小
	Date max(*this);
	Date min(d);

	if (*this < d)
	{
		max = d;
		min = *this;
	}

	//计算差值
	int n = 0;
	while (max != min)
	{
		min++;
		n++;
	}

	return n;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值