6月2日运算符重载

4.5运算符重载

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

作用:实现两个自定义的数据类型相加

cpp 复制代码
#include<iostream>
using namespace std;
//加号运算符重载
//1.成员函数重载+号
//2.全局函数重载+号
class Person
{
public:

	int m_A;
	int m_B;

};
//2.全局函数重载+号
Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//函数重载的版本
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
void test01()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3 = p1 + p2;
	//本质:Person p3=operator+(p1,p2)
	cout << "p3.m_A= " << p3.m_A << endl;
	cout << "p3.m_B= " << p3.m_B << endl;
	//运算符重载 也可以发生函数重载
	Person p4 = p1 + 100;
	cout << "p4.m_A= " << p4.m_A << endl;
	cout << "p4.m_B= " << p4.m_B << endl;



}
int main()
{
	test01();
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
//加号运算符重载
//1.成员函数重载+号
//2.全局函数重载+号
class Person
{
public:

	int m_A;
	int m_B;

};
//2.全局函数重载+号
Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//函数重载的版本
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
void test01()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3 = p1 + p2;
	//本质:Person p3=operator+(p1,p2)
	cout << "p3.m_A= " << p3.m_A << endl;
	cout << "p3.m_B= " << p3.m_B << endl;
	//运算符重载 也可以发生函数重载
	Person p4 = p1 + 100;
	cout << "p4.m_A= " << p4.m_A << endl;
	cout << "p4.m_B= " << p4.m_B << endl;



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

4.5.2左移运算符重载

cpp 复制代码
#include<iostream>
using namespace std;
class Person
{
public:
	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;
	
}
void test01()
{
	Person p1,p2;
	p1.m_A = 10;
	p1.m_B = 10;
	p2.m_A = 20;
	p2.m_B = 20;
	cout << p1 << p2;
	
}
int main()
{
	test01();
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
class Person
{
public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
		
	}
	friend ostream& operator<<(ostream& cout, Person& p);

private:
	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;

}
void test01()
{
	Person p1(10, 10);
	Person p2(20, 20);
	cout << p1 << p2;

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

4.5.3递增运算符重载

cpp 复制代码
#include<iostream>
using namespace std;
class Myintrgger
{
	friend ostream& operator<<(ostream& cout, Myintrgger myint);

public:
	Myintrgger()
	{
		m_Num = 0;
	}
	//重载前置++运算符
	//返回引用是为了一直对一个数据相加
	Myintrgger& operator++()
	{
		m_Num++;//先做一个++运算,再返回
		return *this;
	}
	//重载后置++运算符
	//后置递增返回的是值
	Myintrgger operator++(int)//int代表占位参数,区分前置后置
	{
		//先记录当时的结果
		Myintrgger temp = *this;
		//后递增
		m_Num++;
		//最后将记录结果返回
		return temp;
	}


private:
	int m_Num;
};
//重载左移运算发
ostream& operator<<(ostream& cout, Myintrgger myint)
{
	cout << myint.m_Num << endl;
	return cout;
}
void test01()
{
	Myintrgger myint;
	cout << ++myint;
	cout << myint;

}
void test02()
{
	Myintrgger myint;
	cout << myint++;
	cout << myint;
}
int main()
{
	test01();
	test02();
	return 0;
}

重载右移运算符

cpp 复制代码
#include<iostream>
using namespace std;
class Myintter
{
	friend ostream& operator<<(ostream& cout, Myintter myint);

public:
	Myintter()
	{
		m_Num = 2;
	}
	//重载前置--
	Myintter& operator--()
	{
		m_Num--;
		return *this;
	}
	Myintter operator--(int)
	{
		Myintter temp = *this;
		m_Num--;
		return temp;
	}
private:
	int m_Num;
};
ostream& operator<<(ostream& cout, Myintter myint)
{
	cout << myint.m_Num << endl;
	return cout;
}
void test01()
{
	Myintter myint;
	cout << --myint << myint;
}
void test02()
{
	Myintter myint;
	cout << myint--<<myint;
}
int main()
{
	test01();
	test02();
	return 0;
}

赋值运算符重载

cpp 复制代码
#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;
		}
	}


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

}
int main()
{
	test01();
}

上述代码为浅拷贝,delete重复释放堆区内存,代码出错

解决方案,深拷贝代替浅拷贝

cpp 复制代码
#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;
		//应先判断是否有属性在堆区,先把p2原来的内存释放干净,然后在深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//深拷贝
		m_Age = new int(*p.m_Age);//自身指向新的空间
		return *	this;
	}


	int* m_Age;
};
void test01()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);
	p2 = p1;
    p3 = p2 = p1;
	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;
	cout << "p3的年龄为:" << *p3.m_Age << endl;


}
int main()
{
	test01();
	return 0;#include<iostream>
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;

		}
		return false;
	}
	bool operator!=(Person& p)
	{
		if (this->m_name == p.m_name && this->m_Age == p.m_Age)
		{
			return false;

		}
		return true;
	}

	string m_name;
	int m_Age;

	
};
void test01()
{
	Person p1("zhangsan", 18);
	Person p2("zhangsan", 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()
{
	test01();
	return 0;
}
}

4.5.5关系运算符重载

cpp 复制代码
#include<iostream>
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;

		}
		return false;
	}
	bool operator!=(Person& p)
	{
		if (this->m_name == p.m_name && this->m_Age == p.m_Age)
		{
			return false;

		}
		return true;
	}

	string m_name;
	int m_Age;

	
};
void test01()
{
	Person p1("zhangsan", 18);
	Person p2("zhangsan", 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()
{
	test01();
	return 0;
}

4.5.6函数调用操作符重载

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
class Myprint
{
public:
	//仿函数非常灵活
	void operator()(string text)
	{
		cout << text << endl;
	}
};
void Myprint02(string test)
{
	cout << test << endl;
}
void test01()
{
	Myprint myprint;
	myprint("hello word");//重载
	Myprint02("hello word");//函数调用
}
class Myadd//加法类
{
public:
	int operator()(int num1, int num2)
	{
		return num2 + num2;
	}
	
};
void test02()
{
	Myadd myadd;
	int ret = myadd(100, 200);
	cout << "ret= " << ret<< endl;
	cout << Myadd()(100, 200) << endl;//匿名对象,程序执行完成,立即被释放
}

int main()
{
	test01();
	test02();	
	return 0;
}
相关推荐
CVer儿1 分钟前
svd分解求旋转平移矩阵
线性代数·算法·矩阵
雪碧聊技术4 分钟前
深入解析Vue中v-model的双向绑定实现原理
前端·javascript·vue.js·v-model
快起来别睡了6 分钟前
手写 Ajax 与 Promise:从底层原理到实际应用
前端
Owen_Q9 分钟前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
打不着的大喇叭1 小时前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app
视觉人机器视觉1 小时前
Visual Studio2022和C++opencv的配置保姆级教程
c++·opencv·visual studio
liulilittle1 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
无我Code1 小时前
2025----前端个人年中总结
前端·年终总结·创业
程序猿阿伟1 小时前
《前端路由重构:解锁多语言交互的底层逻辑》
前端·重构
Wilber的技术分享1 小时前
【机器学习实战笔记 14】集成学习:XGBoost算法(一) 原理简介与快速应用
人工智能·笔记·算法·随机森林·机器学习·集成学习·xgboost