运算符重载

定义:

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

作用:

实现两个自定义数据类型相加的运算

一,加号运算符重载

格式:

类名 operator+(数据类型  变量名)

#include<iostream>
using namespace std;
class Person {
	Person() {};
public:
	Person(int a, int b)//这样定义可以直接赋值
	{
		this->m_A = a;
		this->m_B = b;
	}
	//成员函数实现+号运算符重载
	Person operator+(const Person& p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;

	}
	int m_A;
	int m_B;
};
//全局函数实现加号运算符重载
Person operator+(const Person& p1, const Person& p2)
{
	Person temp(0, 0);
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//运算符重载可以发生函数重载
Person operator+(const Person& p1, int n)
{
	Person temp(0, 0);
	temp.m_A = p1.m_A + n;
	temp.m_B = p1.m_B + n;
	return temp;
}
void test()
{
	Person p1(10, 10);
	Person p2(20, 20);
	//成员函数方式
	Person p3 = p2 + p1;//相当于p2.operator+(p1)
	cout << "m_A:" << p3.m_A << "m_B:" << p3.m_B << endl;
	Person p4 = p3 + 10;//相当于operate+(p3,10)
	cout << "m_A:" << p4.m_A << "m_B:" << p4.m_B << endl;
}
int main()
{
	test();
	return 0;
}

二,左移运算符重载

#include<iostream>
using namespace std;
class Person {
	Person() {};
public:
	Person(int a, int b)//这样定义可以直接赋值
	{
		this->m_A = a;
		this->m_B = b;
	}
	//成员函数实现不了 p<<cout不是我们想要的效果
	//void operator<<(Person &p)不可实现
	int m_A;
	int m_B;
};
//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p)
{
	out << "a:" << p.m_A << " b:" << p.m_B;
	return out;
}
void test()
{
	Person p1(10, 20);
	cout << p1 << " nananana" << endl;//链式编程
	
}
int main()
{
	test();
	return 0;
}

访问私有成员:

class Person

{//在顶部定义友元函数

friend ostream&operator<<(ostream&out,Person &p)

}

三,递增运算符重载

#include<iostream>
using namespace std;
class MyInteger {
	friend ostream& operator<<(ostream& out, MyInteger myint);
	//友元函数定义输出,需要访问私有成员时可以使用友元
public:
	MyInteger()//构造函数

	{
		m_Num = 0;//初始化
	}
	//前置++
	MyInteger& operator++() {
		//先++
		m_Num++;
		return *this;
	}
	//后置++
	MyInteger& operator++(int) {//曾加一个int用于区分前置和后置
//先返回
		MyInteger temp = *this;
		//记录本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++
		m_Num++;
		return temp;
	}
private:
	int m_Num;
};
ostream& operator<<(ostream& out, MyInteger myint)//这样自增可以直接对象自增
{
	out << myint.m_Num;
	return out;
}
//前置
void test1()
{
	MyInteger myInt;
	cout << ++myInt << endl;//1
	cout << myInt << endl;//1
}
void test2()
{
	MyInteger myInt;
	cout << myInt++ << endl;//0
	cout << myInt << endl;//1
}
int main()
{
	test1();
	test2();
	return 0;
}

练习:递减运算符重载

#include<iostream>
using namespace std;
class MyInteger {
public: 
	friend ostream& operator<<(ostream& out, MyInteger Myint);
	//前置
	MyInteger()
	{
		m_num = 1;
	}
	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 Myint)//注意out用引用方式,直接可以实现链式输出
{
	out << Myint.m_num ;
	return out;
}
void test1()
{
	MyInteger p;
	cout << --p << endl;//0
	cout << p << endl;//0
}
void test2()
{
	MyInteger p;
	cout <<p-- << endl;//1
	cout << p << endl;//0
}
int main()
{
	test1();
	test2();
	return 0;
}

四,赋值运算符重载

#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)//用引用返回自身
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//编译器提供的代码是浅拷贝,例如:m_Age=p.m_Age;
		//浅拷贝可能导致堆区数二次释放,深拷贝可以解决该问题

		m_Age = new int(*p.m_Age);//每次赋值开辟一个新堆区地址存放指针
		return *this;
	}
	

	int* m_Age;//属性创建在堆区时浅拷贝会出错
};
void test()
{
	Person p1(10);
	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()
{
	test();
	return 0;
}

五,关系运算符重载

如:“==",”!="

#include<iostream>
using namespace std;
class Person
{public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->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 test()
{
	Person p1("小王", 18);
	Person p2("小王", 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()
{
	test();
	return 0;
}

六,函数调用运算符重载

函数调用运算符()也可以发生重载,成为仿函数

#include<iostream>
using namespace std;
class MyPrint {
public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};
void test1()
{
	//重载的()操作符,也称为仿函数
	MyPrint myFunc;
	myFunc("hello world!");
}
class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};
void test2()
{
	MyAdd add;
	int ret = add(10, 10);
	//匿名对象调用
	cout << "MyAdd()(100,100)=" << MyAdd()(100, 100) << endl;
}
int main()
{
	test1();
	test2();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值