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;
}
相关推荐
. . . . .6 分钟前
shadcn组件库
前端
2501_9447114314 分钟前
JS 对象遍历全解析
开发语言·前端·javascript
A星空12318 分钟前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
凡人叶枫40 分钟前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
power 雀儿1 小时前
掩码(Mask)机制 结合 多头自注意力函数
算法
发现一只大呆瓜1 小时前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试
会叫的恐龙1 小时前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串
小糯米6011 小时前
C++顺序表和vector
开发语言·c++·算法
css趣多多1 小时前
ctx 上下文对象控制新增 / 编辑表单显示隐藏的逻辑
前端
独望漫天星辰1 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++