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;
}
相关推荐
m0_570466415 分钟前
代码随想录算法训练营第二十八天 | 买卖股票的最佳实际、跳跃游戏、K次取反后最大化的数组和
java·开发语言·算法
程序喵大人6 分钟前
分享个C++线程池的实现源码
开发语言·c++·线程池
吃着火锅x唱着歌12 分钟前
LeetCode 1537.最大得分
算法·leetcode·职场和发展
数模加油站12 分钟前
25高教社杯数模国赛【C题超高质量思路+可运行代码】第十弹
算法·数学建模·数模国赛·高教社杯全国大学生数学建模竞赛
F2E_Zhangmo17 分钟前
基于cornerstone3D的dicom影像浏览器 第二章 加载本地文件夹中的dicom文件并归档
前端·javascript·css
ulias21226 分钟前
动态规划入门:从记忆化搜索到动态规划
算法·动态规划
山河君27 分钟前
webrtc之语音活动上——VAD能量检测原理以及源码详解
算法·音视频·webrtc·信号处理
用户214118326360233 分钟前
Nano Banana免费方案来了!Docker 一键部署 + 魔搭即开即用,小白也能玩转 AI 图像编辑
前端
FL162386312937 分钟前
[ubuntu][C++]onnxruntime安装cpu版本后测试代码
linux·c++·ubuntu
THMAIL1 小时前
深度学习从入门到精通 - LSTM与GRU深度剖析:破解长序列记忆遗忘困境
人工智能·python·深度学习·算法·机器学习·逻辑回归·lstm