17.C++之运算符重载(一)

学习目标:

在这里插入图片描述


学习内容:

运算符重载:对已有的运算符重新进行定义,赋予其另外一种能力,用来适应不同的数据类型。编辑器提供通用名operator来完成运算符的重载,

1. 加号运算符重载

作用:实现两个自定义数据类型相加的运算。有两种方式来重载:

  • 成员函数重载+号
  • 全局函数重载+号
1.1 成员函数重载+号

描述:实现两个类中相同属性的相加

//1.成员函数重载+号
#include<iostream>
#include<string>
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;
	}
	int m_A;
	int m_B;
};
void test1()
{
	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 = p1.operator+(p2);          //本质上调用!!!!!!!
	cout << "p3.m_A:  " << p3.m_A << endl;
	cout << "p3.m_B:  " << p3.m_B << endl;
}
int main()
{
	test1();
	system("pause");
	return 0;
}

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

1.2 全局函数重载+号
//1.全局函数重载+号
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
	int m_a;
	int m_b;
};

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;
}

void test()
{
	person p1;
	p1.m_a = 10;
	p1.m_b = 20;

	person p2;
	p2.m_a = 10;
	p2.m_b = 20;

	person p3;
	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()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.3 函数发生重载

描述:实现成员属性与整型相加

//函数也可以发生重载
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
	int m_a;
};

person operator+(person& p1, int num)
{
	person temp;
	temp.m_a = p1.m_a + num;
	return temp;
}

void test()
{
	person p1;
	p1.m_a = 10;
	
	person p = p1 + 90;
	cout << "p.m_a = " << p.m_a << endl;	
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

2. 左移运算符重载

直接输入对象,就可以输出对象的属性;
在这里插入图片描述

//左移运算符
#include<iostream>
using namespace std;
class Person
{
public:
	int m_A = 15;
	int m_B = 30;
};
ostream& operator<<(ostream& cout, Person& p)
{
	cout << "m_A = " << p.m_A << ", m_B = " << p.m_B << endl;
	return cout;

}
void test()
{
	Person p;
	//cout << p << endl;
	operator<<(cout, p);
}

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

在这里插入图片描述

3. 赋值运算符重载

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

  • 默认构造函数(无参,函数体为空);
  • 默认析构函数(无参,函数体为空);
  • 默认拷贝构造函数,对属性进行值拷贝;
  • 赋值运算符operator=,对属性进行值拷贝;

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

//赋值运算符重载
#include<iostream>
#include<string>
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 = new int(*p.m_Age);

		//返回对象本身
		return *this;
	}
	int *m_Age;
};

void test()
{
	Person p1(18);

	Person p2(20);

	p2 = p1;//赋值操作
	cout << "p1的年龄为:" << *p1.m_Age << endl;

	cout << "p2的年龄为:" << *p2.m_Age << endl;

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

在这里插入图片描述

4. 关系运算符重载

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

//关系运算符重载
#include<iostream>
using namespace std;
class Person
{
public:
	Person(int age)
	{
		this->m_Age = age;
	}
	bool operator==(Person& p)
	{
		if (this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	int m_Age;
};

void test()
{
	Person p1(10);
	Person p2(10);
	if (p1 == p2)
	{
		cout << "二者相等!!!!!" << endl;
	}
	else
	{
		cout << "二者不相等!!!!" << endl;
	}

}

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

在这里插入图片描述
在这里插入图片描述

5. 函数调用运算符重载

函数调用运算符()也可发生重载;由于重载之后使用的方式和函数调用非常相似,因此称为仿函数(在stl中比较常见)。仿函数形式灵活。

//函数调用运算符重载
#include<iostream>
#include<string>
using namespace std;
//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};
void test()
{
	MyPrint myprint;
	myprint("hello world!");
}

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

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值