C++面向对象(七)—— 运算符重载

本文介绍了C++中的运算符重载,包括加号运算符、左移运算符、递增运算符、赋值运算符、关系运算符以及函数调用运算符的重载方式。通过实例展示了成员函数和全局函数的不同实现,并强调了深拷贝和浅拷贝在赋值运算符重载中的重要性。文章还提到了仿函数的概念,即函数调用运算符的重载应用。
摘要由CSDN通过智能技术生成

hello 好久不见

今天给大家分享的是运算符重载

其中主要分为以下六类:

加号运算符重载

左移运算符重载

递增运算符重载

赋值运算符重载

关系运算符重载

函数调用运算符重载

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

那我们正式开始今天的内容:

加号运算符重载

我们先讲一个知识点,那就是operstor

我们要知道这个关键字是执行重载的必要条件

#include<iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
	//成员函数加号重载
	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; 
	}
	
public:
	
	int m_A;
	int m_B;	
};
//全局函数运算符重载 
//Person operator+(Person &p1,Person &p2)
//{
//	Person temp;
//	temp.m_A = p2.m_A + p1.m_A;
//	temp.m_B = p2.m_B + p1.m_B;
//	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;
	// P3 = P2.operator+(p1);
	p3 = p2 + p1;
	//p3 = operator(p1,p2);
	//p3 = p1 + p2;
	cout << "p3 : m_A = " << p3.m_A << endl;
	cout << "p3 : m_B = " << p3.m_B << endl;
} 
//运算符重载可以进行函数重载
 
int main()
{
	test01();
	return 0;
}

加号运算符重载函数 可分为:全局函数,成员函数

各有各的表示形式

希望读者能自行区分

左移运算符重载

我们先了解一下什么是左移运算符:<<

也就是cout 后面所接的符号

我们鼠标右键点击 "cout" 点开到定义: 

我们会发现 ostream 是 cout 的类型

(如果有想详细了解 ostream 的内容的同学可以转到 C++之 ostream详细用法

所以我们在编写左移运算符重载的时候,如果要实现链式编程的思想,那么我们编写左移运算符重载函数的返回值应该是 ostream

故有以下代码:

#include<iostream>
using namespace std;
//左移运算符重载
class Person
{
	friend ostream& operator<<(ostream &cout, Person &p); 
	
public:
	Person()
	{
		m_A = 10;
		m_B = 10;
	}
	
private:	
	int m_A;
	int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream &cout, Person &p)
{
	cout << "m_A = " << p.m_A << endl;
	cout << "m_B = " << p.m_B << endl; 
	return cout;
}
void test01()
{
	Person p;
    //测试链式
	cout << p << p;
}
int main()
{
	test01();
	return 0;
}

递增运算符重载

这里可以观察前++和后++之间的区别

#include<iostream>
using namespace std;
class MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger()
	{
		this->m_Num = 0;
	}
	
	//前置++
	MyInteger& operator++()
	{
		//先加加
		m_Num++;
		//再返回
		return *this; 
	}
	
	//后置++ 
	MyInteger operator++(int)
	{
		MyInteger temp = *this;
		//记录当前本身的值,然后让本身的值加一
		//但是返回的是以前的值,达到先返回后++;
		this->m_Num++;
		return temp; 
	}
	
private:
	int m_Num;
}; 

ostream& operator<<(ostream& cout, MyInteger myint)
{
	cout << "m_NUM = " << myint.m_Num;
	return cout;
}

void test01()
{
	MyInteger myint;
	cout << ++(++myint) << endl;
	cout << myint <<endl;
}

void test02()
{
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint <<endl;
}

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

赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)

  2. 默认析构函数(无参,函数体为空)

  3. 默认拷贝构造函数,对属性进行值拷贝

  4. 赋值运算符 operator=, 对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

如图:(深浅拷贝带来的问题)

 所以我们有:

#include<iostream>
using namespace std;
class Person
{
	friend void test01();
	friend ostream& operator<<(ostream &cout, Person &p);
	
public:
	Person(int age)
	{
		m_age = new int(age);
	}
	
	//重载赋值运算符
	Person& operator=(Person &p)
	{
		if(m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
		//编译器提供的代码是浅拷贝
		//m_age = p.mage
		
		//提供深拷贝 解决浅拷贝问题
		m_age = new int(*p.m_age);
		
		//返回自身
		return *this; 
		 		
	} 
	
	~Person()
	{
		if(m_age != NULL)
		{
			delete m_age;
			m_age = NULL; 
		}
	}
private:
	int *m_age;	
};

ostream& operator<<(ostream &cout, Person &p)
{
	cout << "p的年龄为:" << *p.m_age ;
	return cout;
}

void test01()
{
	Person p1(18);
	
	Person p2(20);
	
	Person p3(30);
	
	p3 = p2 = p1;
	
	cout << p1 << endl;
	
	cout << p2 << endl;
	
	cout << p3 << endl;
}

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

关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

这里比较简单,直接过:

#include<iostream>
#include<string>
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_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;
	}
	string m_name;
	int m_age;
};

void test01()
{
	Person p1("张三",18);
	Person p2("张三",18);
	if(p1 == p2)
		cout << "1" << endl;
	else
		cout << "0" << endl; 
	
}

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

函数调用运算符重载

  • 函数调用运算符 () 也可以重载

  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数

  • 仿函数没有固定写法,非常灵活

#include<iostream>
#include<string>
using namespace std;
class Myprint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}		
};
void test01()
{
	Myprint ooc;
	ooc("hello world");
}

class MyAdd
{
public:
	int operator()(int a1, int a2)
	{
		return a1 + a2;
	}		
};
void test02()
{
	MyAdd add;
	int ret = add(10,10);
	cout << ret <<endl;
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;	
} 

 感蟹观看/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值