【P121 39】C++ 运算符重载(加号、左移、递增、赋值、关系、函数调用)

本文详细介绍了C++中的运算符重载,包括加号运算符、左移运算符、递增运算符、递减运算符、赋值运算符、关系运算符和函数调用运算符的重载。通过成员函数和全局函数两种方式实现不同类型的运算符重载,同时展示了如何处理深拷贝和浅拷贝问题,以及如何通过仿函数实现类似函数调用的效果。
摘要由CSDN通过智能技术生成

P121 39


运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

一、加号运算符重载

作用:实现两个自定义数据类型相加的运算
1、成员函数重载+号
2、全局函数重载+号
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、成员函数重载+号

#include<iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
	//1、成员函数重载+号
	//Person p3=p1.operator+(p2);
	Person operator+(Person& p)
	{
		Person temp;
		temp.A = this->A + p.A;
		temp.B = this->B + p.B;
		return temp;
	}
	int A;
	int B;	
};

Person p3=p1.operator+(p2);
简化为
Person p3=p1+p2;

void test1()
{
	Person p1;
	p1.A = 10;
	p1.B = 10;

	Person p2;
	p2.A = 10;
	p2.B = 10;

	Person p3=p1+p2;	//Person p3=p1.operator+(p2);
	cout << "p3.A=" << p3.A << endl;
	cout << "p3.B=" << p3.B << endl;
}

int main() 
{
	test1();
	return 0;
}

在这里插入图片描述

2、全局函数重载+号

//加号运算符重载
class Person
{
public:
	int A;
	int B;	
};
//2、全局函数重载+号
//Person p3=operator+(p1,p2);
Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.A = p1.A + p2.A;
	temp.B = p1.B + p2.B;
	return temp;
}

Person p3=operator+(p1,p2);
简化为
Person p3=p1+p2;

void test1()
{
	Person p1;
	p1.A = 10;
	p1.B = 10;

	Person p2;
	p2.A = 10;
	p2.B = 10;

	Person p3=p1+p2;//Person p3=operator+(p1,p2);
	cout << "p3.A=" << p3.A << endl;
	cout << "p3.B=" << p3.B << endl;
}

在这里插入图片描述

3、函数重载的版本

在这里插入图片描述

#include<iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
	int A;
	int B;	
};
//函数重载的版本
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.A = p1.A + num;
	temp.B = p1.B + num;
	return temp;
}

Person p3 = p1 + 10;//Person+int

void test1()
{
	Person p1;
	p1.A = 10;
	p1.B = 10;

	Person p2;
	p2.A = 10;
	p2.B = 10;

	Person p3 = p1 + 10;//Person+int

	cout << "p3.A=" << p3.A << endl;
	cout << "p3.B=" << p3.B << endl;
}

在这里插入图片描述

三种重载全部代码

#include<iostream>
using namespace std;

//加号运算符重载
//1、成员函数重载+号
//2、全局函数重载+号

//加号运算符重载
class Person
{
public:
	1、成员函数重载+号
	Person p3=p1.operator+(p2);
	//Person operator+(Person& p)
	//{
	//	Person temp;
	//	temp.A = this->A + p.A;
	//	temp.B = this->B + p.B;
	//	return temp;
	//}
	int A;
	int B;	
};

2、全局函数重载+号
Person p3=operator+(p1,p2);
//Person operator+(Person& p1, Person& p2)
//{
//	Person temp;
//	temp.A = p1.A + p2.A;
//	temp.B = p1.B + p2.B;
//	return temp;
//}
//函数重载的版本
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.A = p1.A + num;
	temp.B = p1.B + num;
	return temp;
}

void test1()
{
	Person p1;
	p1.A = 10;
	p1.B = 10;

	Person p2;
	p2.A = 10;
	p2.B = 10;

	//1、成员函数重载本质调用
	//Person p3=p1.operator+(p2);

	//2、全局函数重载本质调用
	//Person p3=operator+(p1,p2);

	//Person p3=p1+p2;
	运算符重载 也可以发生函数重载

	Person p3 = p1 + 10;//Person+int

	cout << "p3.A=" << p3.A << endl;
	cout << "p3.B=" << p3.B << endl;
}

int main() 
{
	test1();
	return 0;
}

二、左移运算符重载

作用:可以输出自定义数据类型

在这里插入图片描述

class Person
{
public:
	int m_A;
	int m_B;
};

void test1()
{
	Person p;
	p.m_A = 10;
	p.m_B = 10;

	cout << p << endl;
}

在这里插入图片描述

//利用成员函数重载左移运算符pIoperatorK(cout)简化版本 p << cout
//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
在这里插入图片描述
只能利用全局函数重载左移运算符

class Person
{
public:
	//利用成员函数重载左移运算符pIoperatorK(cout)简化版本 p << cout
	//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
	//void operator<<(cout)
	//{

	//}
	int m_A;
	int m_B;
};

//只能利用全局函数重载左移运算符
void operator<<(ostream &cout, Person &p)//本质operator<<(cout,p),简化为cout<<p;
{
	cout << "m_A=" << p.m_A << "  m_B=" << p.m_B;
}
void test1()
{
	Person p;
	p.m_A = 10;
	p.m_B = 10;

	cout << p;
}

在这里插入图片描述
可以输出,但是没有换行
因为cout 后面没有换行符endl
但是加了endl,就会报错
在这里插入图片描述
因为在调用左移运算符重载函数后,返回值为void类型,所以后面继续追加报错

将左移运算符重载函数,修改成ostream类型,并且引用

ostream& operator<<(ostream &cout, Person &p)//本质operator<<(cout,p),简化为cout<<p;
{
	cout << "m_A=" << p.m_A << "  m_B=" << p.m_B;
	return cout;
}

在这里插入图片描述

“函数中ocout”可以修改为任意为任意名字,

因为在函数ostream& operator<<(ostream &cout, Person &p)中ostream &cout,引用就是起别名

ostream& operator<<(ostream &o, Person &p)//本质operator<<(cout,p),简化为cout<<p;
{
	o << "m_A=" << p.m_A << "  m_B=" << p.m_B;
	return o;
}

在这里插入图片描述
完整程序

#include<iostream>
using namespace std;

//左移运算符重载

class Person
{
public:
	//利用成员函数重载左移运算符pIoperatorK(cout)简化版本 p << cout
	//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
	//void operator<<(cout)
	//{

	//}
	int m_A;
	int m_B;
};

//只能利用全局函数重载左移运算符
ostream& operator<<(ostream & cout, Person &p)//本质operator<<(cout,p),简化为cout<<p;
{
	cout << "m_A=" << p.m_A << "  m_B=" << p.m_B;
	return cout;//函数中的cout可以修改为任意名字
}
void test1()
{
	Person p;
	p.m_A = 10;
	p.m_B = 10;
	cout << p<<endl;
}

int main() 
{
	test1();
	system("pause");
	return 0;
}

三、递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

引入

先回忆一下,a++和++a区别
在这里插入图片描述
在类中的递增
在这里插入图片描述
在这里插入图片描述

递增运算符重载

//递增运算符重载
class MyInteger
{
public:
	MyInteger()
	{
		m_Num = 0;
	}
private:
	int m_Num;
};

void test1()
{
	MyInteger myint;
	cout << myint << endl;
}

在这里插入图片描述
出错是因为没有左移运算符重载函数

//重载左移运算符
ostream & operator<<(ostream &out,MyInteger m)
{
	out << m.m_Num ;
	return out;
}

在这里插入图片描述

于是加上重载左移运算符,出现了新的错误
int m_Num是私有成员,operator要想访问私有,必须是友元

在类的最上面家加上
friend ostream& operator<<(ostream& out, MyInteger m);
//递增运算符重载
class MyInteger
{
	friend ostream& operator<<(ostream& out, MyInteger m);
public:
	MyInteger()
	{
		m_Num = 0;
	}
private:
	int m_Num;
};

则可以正常运行了

#include<iostream>
using namespace std;

//递增运算符重载
class MyInteger
{
	friend ostream& operator<<(ostream& out, MyInteger m);
public:
	MyInteger()
	{
		m_Num = 0;
	}
private:
	int m_Num;
};

//重载左移运算符
ostream & operator<<(ostream &out,MyInteger m)
{
	out << m.m_Num ;
	return out;
}
void test1()
{
	MyInteger myint;
	cout << myint << endl;
}

int main() 
{
	test1();
	return 0;
}

在这里插入图片描述

2 重载递增运算符

重载前置++运算符 ++i;

//重载前置++运算符
MyInteger& operator++()//前置返回引用
{
	//先进性++运算
	m_Num ++ ;

	//再将自身返回
	return *this;
}

重载后置++运算符 i++;

//重载后置++运算符
	//void operator++(int) //int 代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int)//后置返回值
	{
		//先 记录记录当前结果
		MyInteger temp = *this;

		//后 递增
		m_Num++;

		//最后 将记录结果返回
		return temp;
	}

完整程序

#include<iostream>
using namespace std;

//递增运算符重载
class MyInteger
{
	friend ostream& operator<<(ostream& out, MyInteger m);
public:
	MyInteger()
	{
		m_Num = 0;
	}

	//重载前置++运算符
	MyInteger& operator++()//前置返回引用
	{
		//先进性++运算
		m_Num ++ ;

		//再将自身返回
		return *this;
	}

	//重载后置++运算符
	//void operator++(int) //int 代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int)//后置返回值
	{
		//先 记录记录当前结果
		MyInteger temp = *this;

		//后 递增
		m_Num++;

		//最后 将记录结果返回
		return temp;
	}


private:
	int m_Num;
};

//重载左移运算符
ostream & operator<<(ostream &out,MyInteger m)
{
	out << m.m_Num ;
	return out;
}
void test1()
{
	MyInteger myint;
	cout << ++(++myint) << endl;
	cout << myint << endl;
}


void test2()
{
	MyInteger myint2;
	cout << myint2++ << endl;
	cout << myint2 << endl;
}
int main() 
{
	test1();
	test2();
	return 0;
}

在这里插入图片描述

四、递减运算符重载

学会了递增运算符,递减运算符就非常简单了

只需把递增运算符中m_Num++改成m_Num–就可以了;其余地方做相应修改。

#include<iostream>
using namespace std;

//递减运算符重载
class MyInteger
{
	friend ostream& operator<<(ostream& out, MyInteger m);
public:
	MyInteger()
	{
		 m_Num = 0;
	}
	//前置递减
	MyInteger& operator--()
	{
		m_Num--;
		return *this;
	}

	//后置递减
	MyInteger operator--(int)
	{
		MyInteger temp = *this;
		m_Num--;
		return temp;
	}

private:
	int m_Num;
};

//重载左移运算符
ostream& operator<<(ostream& out, MyInteger m)
{
	out << m.m_Num;
	return out;
}
void test1()
{
	MyInteger myint1;
	cout << --myint1 << endl;
	cout << --myint1 << endl;

}

void test2()
{
	MyInteger myint2;
	cout << myint2-- << endl;
	cout << myint2-- << endl;
}
int main()
{
	test1();
	test2();
	return 0;
}

在这里插入图片描述

五、赋值运算符重载

在这里插入图片描述

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

class Person
{
public:
	Person(int age)
	{
		m_Age = new int(age);
	}
	~Person()
	{
		if (m_Age != NULL)
			delete m_Age;
		m_Age = NULL;
	}
	int *m_Age;
};

void test1()
{
	Person p1(18);
	Person p2(20);
	cout << "p1的年龄为 :" << *p1.m_Age << endl;
	cout << "p2的年龄为 :" << *p2.m_Age << endl;

}

在这里插入图片描述
可以看到现在输出正常

若果我们在test1()中,加一句赋值语句 p2=p1;那么运行一半就会出错;
也就是在析构函数释放时出错

void test1()
{
	Person p1(18);
	Person p2(20);
	p2 = p1;//赋值

	cout << "p1的年龄为 :" << *p1.m_Age << endl;
	cout << "p2的年龄为 :" << *p2.m_Age << endl;

}

在这里插入图片描述
因为在这里,没有写拷贝函数,系统默认的浅拷贝;
浅拷贝,直接将p1的地址和值都拷贝给p2了,那么在释放的时候,发生了重复释放,出错
在这里插入图片描述
用深拷贝解决问题
在这里插入图片描述
在person类中,重载赋值运算符

//重载 赋值运算符
	void operator=(Person &p)
	{
		//编译器提供的是浅拷贝
		//m_Age=p.m_Age;

		//应该先判断是否有属性在堆区,如果有 先释放干净,然后在深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);
	}

在这里插入图片描述

那么到这里,还没结束。
如果我们再增加一个p3,
实现连续赋值p3 = p2 = p1;出错

void test1()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);

	p3 = p2 = p1;//赋值

	cout << "p1的年龄为 :" << *p1.m_Age << endl;
	cout << "p2的年龄为 :" << *p2.m_Age << endl;
	cout << "p3的年龄为 :" << *p3.m_Age << endl;
}

在这里插入图片描述
原因,重载赋值运算符是一个无返回值类型
void operator=(Person &p)

修改如下

//重载 赋值运算符
	Person & operator=(Person &p)
	{
		//编译器提供的是浅拷贝
		//m_Age=p.m_Age;

		//应该先判断是否有属性在堆区,如果有 先释放干净,然后在深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);

		 //返回对象本身
		return *this;
	}

在这里插入图片描述
完整程序

#include<iostream>
using namespace std;

class Person
{
public:
	Person(int age)
	{
		m_Age = new int(age);
	}
	~Person()
	{
		if (m_Age != NULL)
			delete m_Age;
		m_Age = NULL;
	}

	//重载 赋值运算符
	Person & operator=(Person &p)
	{
		//编译器提供的是浅拷贝
		//m_Age=p.m_Age;

		//应该先判断是否有属性在堆区,如果有 先释放干净,然后在深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);

		 //返回对象本身
		return *this;
	}
	int *m_Age;
};

void test1()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);

	p3 = p2 = p1;//赋值

	cout << "p1的年龄为 :" << *p1.m_Age << endl;
	cout << "p2的年龄为 :" << *p2.m_Age << endl;
	cout << "p3的年龄为 :" << *p3.m_Age << endl;

}

int main()
{
	test1();
	return 0;
}

六、关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;

//重载关系运算符
class Person
{
public:
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	//重载 == 号
	bool operator==(Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return true;
		else return false;
	}

	//重载 != 号
	bool operator!=(Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return false;
		else return true;
	}

	string m_Name;
	int m_Age;
};

void test1()
{
	Person p1("Tom" ,18);
	Person p2("Tom", 18);

	//重载 == 号
	if (p1 == p2)
		cout << "p1和p2相等的" << endl;
	else
		cout << "p1和p2不相等的" << endl;

	//重载 != 号
	if (p1 != p2)
		cout << "p1和p2不相等的" << endl;
	else 
		cout << "p1和p2相等的" << endl;
}

int main()
{
	test1();
	return 0;
}

在这里插入图片描述

七、函数调用运算符重载

函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活

仿函数

#include<iostream>
#include<string>
using namespace std;

//函数调用运算符重载
//打印输出类
class MyPrint
{
public:
	void operator()(string test)
	{
		cout << test << endl;
	}

};

void test1()
{
	MyPrint  mypring;
	mypring("hellow world");
}

int main()
{
	test1();
	return 0;
}

在这里插入图片描述
函数调用运算符重载 和 函数调用 对比

//重载函数调用运算符
void operator()(string test)
{
	cout << test << endl;
}
//输出函数
void mypring2(string test)
{
	cout << test << endl;
}
void test1()
{
	MyPrint  mypring;
	mypring("hellow world");//由于使用起来非常类似于函数调用,因此称为仿函数
	mypring2("hellow world"); //函数调用

}

2 仿函数没有固定写法,非常灵活

仿函数没有固定写法,非常灵活

举例对比

//打印输出类

//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};

//加法类

class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
	
};

完整程序对比

#include<iostream>
#include<string>
using namespace std;

//函数调用运算符重载
//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};

//输出函数
void mypring2(string test)
{
	cout << test << endl;
}
void test1()
{
	MyPrint  mypring;
	mypring("hellow world");//由于使用起来非常类似于函数调用,因此称为仿函数
	mypring2("hellow world");

}


//仿函数没有固定写法,非常灵活
//加法类
class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
	
};
void test2()
{
	MyAdd myadd;
	int sum = myadd(100,10);
	cout << "sum= " << sum << endl;

	//匿名函数对象
	cout << myadd(100, 10) << endl;
}

int main()
{
	test1();
	test2();

	return 0;
}

在这里插入图片描述

参考:黑马程序员
哔哩哔哩 黑马程序员

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

R-G-B

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

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

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

打赏作者

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

抵扣说明:

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

余额充值