C++运算符重载(简单易懂)

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

1.加号运算符(“+”)重载

//创建类 Person

class Person
{
public:
	//1.成员函数重载 + 号
	//(本质):Person p3 = p1.operator+(p2);
	
	Person operator+(Person &p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}

	//两个内置数据AB
	int m_A;
	int m_B;
};
//2.全局函数实现 + 号重载
//(本质):Person p3 = operator+(p1,p2);
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 text01()   //测试
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 20;
	Person p2;
	p2.m_A = 30;
	p2.m_B =40;

	Person p3 = p1+ p2;
	cout<<"p3.m_A="<<p3.m_A<<endl;
	cout<<"p3.m_B="<<p3.m_B<<endl;
    //进行函数重载
	Person p4 = p1 + 10;
	cout<<"p4.m_B="<<p4.m_A<<endl;
}

总结:对于内置的数据类型的表达式的运算符是不可以改变

2.左移运算符重载

class Person
{
public:
	
	//不会利用成员函数重载左移运算符

};
//利用全局函数重载左移运算符
ostream &operator<<(ostream &cout,Person &p)
{
	cout<<"m_A="<<p.m_A<<" "<<"m_B="<<p.m_B<<endl;
	return cout;
}
//如果需要访问private成员可以结合友元(friend)
void text01()//测试
{
	Person p;
	p.m_A = 10;
	p.m_B = 20;
	cout<<p<<endl;
}

3.递增运算符重载

class MyInteger
{
	friend ostream & operator<<(ostream & cout,MyInteger myint);//友元(访问私有对象)
public:
	//重载前置递增
	MyInteger& operator++()//返回引用是为了一直对一个数据进行操作
	{
		//先进行++运算
		a++;
		//再将自身返回
		return *this;
	}
	//重载后置递增(函数的返回值不可以作为函数重载的条件)
	MyInteger operator++(int)//int是一个占位参数,用于区分前后置
	{
		//先返回结果
		MyInteger temp = *this;
		//后递增
		a++;
		//最后将记录结果返回
		return temp;
	}
	MyInteger()
	{
		a = 0;
	}
private:
	int a;
};
void text01()//测试
{
	MyInteger myint;
	cout<<myint++<<endl;
	cout<<++myint<<endl;
}

4.赋值运算符重载

//c++至少给一个类添加4个函数
//1.默认构造函数(无参数 函数体为空)
//2.默认析构函数(无参数 函数体为空)
//3.默认拷贝构造函数 (对属性进行值拷贝)
//4.赋值运算符operator=,对属性进行值拷贝
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 text01()
{
	Person p(18);
	Person p2(20);
	p = p2;//浅拷贝将p2进行值拷贝到p(导致堆区的内存重复释放,需要利用深拷贝来解决(在堆区来重新new一个空间用于存放浅拷贝的值))
	Person p3(22);
	p3 = p2 = p;
	cout<<*p.m_Age<<endl;
	cout<<*p2.m_Age<<endl;
	cout<<*p3.m_Age<<endl;
}

5.关系运算符重载(将两个自定义数据类型进行操作)

class Person
{
public:
	Person()
	{
		this->m_Age;
	}
	Person(int age)
	{
		m_Age = age;
	}
	bool operator==(Person &p)
	{
		if(this->m_Age =  p.m_Age && this->m_Name == p.m_Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator!=(Person &p)
	{
		if(this->m_Age != p.m_Age && this->m_Name != p.m_Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	int m_Age;
	string m_Name;
};
void text01()
{

	Person p2(20);
	Person p1(0);
	p1.m_Name = "张三";
	p2.m_Name = "张三";
	if(p1 == p2)
	{
		cout<<"p1和p2是相等的"<<endl;
	}
	else
	{
		cout<<"p1和p2是不相等的"<<endl;
	}
	///
	if(p1 != p2)
	{
		cout<<"p1和p2是不相等的"<<endl;
	}
	else
	{
		cout<<"相等"<<endl;
	}
	cout<<p1.m_Age<<endl;
	cout<<p2.m_Age<<endl;
}

6.函数调用运算符重载

class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout<<test<<endl;
	}
};
void MyPrint02(string test)
{
	cout<<test<<endl;
}
void text01()
{
	MyPrint myPrint;
	myPrint("hello");//使用起来与函数的调用非常类似,因此也称为仿函数
	MyPrint02("word");
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyADD
{
public:
	int operator()(int a,int b)
	{
		return a+b;
	}
};
void text02()
{
	MyADD myadd;
	int ret = myadd(10,20);
	cout<<ret<<endl;
	//匿名函数对象
	cout<<MyADD()(100,100)<<endl;//当前行执行完后立即被释放
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值