2021自学C++核心编程之运算符重载(八)

运算符重载

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

运算符重载概念看上去比较简单,理解起来却不是很容易到位,下面我们从需求的方面来充分理解这个概念,需求如下:

如何将自定义的类创建的三个对象,将其中两个对象进行相加赋值给第三个对象(注意:对象加对象,是指两对象中相同的成员属性的值进行相加)

代码示例:

#include<iostream>
using namespace std;
class Person {
public:
	int m_A;
	int m_B;
public:
	//用无参构造函数创建对象
	Person() {}
	//有参构造函数对成员变量进行初始化
	Person(int m_A,int m_B) {
		this->m_A = m_A;
		this->m_B = m_B;
	}
	//方式二:通过成员函数addPerson实现两对象属性值相加并赋值给第三个对象
	Person addPerson(Person &p){
		//创建一个Person类对象,用来接收两对象属性相加的值
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}
};
//方式三:利用全局函数实现两对象属性值相加并赋值给第三个对象
Person addPersons(Person &p1,Person &p2) {
	//创建一个Person类对象,用来接收两对象属性相加的值
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//方式一:通过对象.属性方式进行赋值实现两对象属性值相加并赋值给第三个对象
void test01() {
	Person p1 = Person(10, 10);
	Person p2 = Person(20, 20);
	Person p3;
	p3.m_A = p1.m_A + p2.m_A;
	p3.m_B = p1.m_B + p2.m_B;
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
}
//测试方式二:
void test02() {
	Person p1 = Person(10, 10);
	Person p2 = Person(20, 20);
	Person p3 = p2.addPerson(p1);
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
}
//测试方式三:
void test03() {
	Person p1 = Person(10, 10);
	Person p2 = Person(20, 20);
	Person p3 = addPersons(p1,p2);
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
}
int main() {
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

对于上述实现的三种方式,对于现阶段的我们来说都能想到并实现,对于上述利用函数实现的方法进行实现,不同的人定义的函数名也就可能不同,C++编译器就对利用函数实现的方法进行实现的函数名进行了统一,并且将对象相加的形式进行了简化,有利于提高代码的可读性和编写速度

4.5.1 加号运算符重载

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

代码示例:

#include<iostream>
using namespace std;
class Person {
public:
	int m_A;
	int m_B;
public:
	//用无参构造函数创建对象
	Person() {}
	//有参构造函数对成员变量进行初始化
	Person(int m_A,int m_B) {
		this->m_A = m_A;
		this->m_B = m_B;
	}
	//通过成员函数重载+号代替成员函数addPerson实现两对象属性值相加并赋值给第三个对象
	Person operator+(Person& p) {
		//创建一个Person类对象,用来接收两对象属性相加的值
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}
    //②.成员函数重载+号发生函数重载
	Person operator+(int num) {
		//创建一个Person类对象,用来接收两对象属性相加的值
		Person temp;
		temp.m_A = this->m_A + num;
		temp.m_B = this->m_B + num;
		return temp;
	}
};
//通过全局函数重载+号代替全局函数addPersons实现两对象属性值相加并赋值给第三个对象
Person operator+(Person& p1, Person& p2) {
	//创建一个Person类对象,用来接收两对象属性相加的值
	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类对象,用来接收两对象属性相加的值
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
//测试函数重载+号
void test04() {
	Person p1 = Person(10, 10);
	Person p2 = Person(20, 20);
	//成员函数重载+号的本质:Person p3 = p2.operator+(p1);简化为: Person p3 = p1 + p2;
	//全局函数重载+号的本质:Person p3 = operator+(p1,p2);简化为: Person p3 = p1 + p2;
	//注意:成员函数和全局函数的简化后调用相同,导致编译器无法判断,需要先注释一个
	Person p3 = p1 + p2;
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
}
//测试运算符重载进行重载
void test05() {
	Person p4 = Person(10, 10);
    //注意:成员函数和全局函数的简化后调用相同,导致编译器无法判断,需要先注释一个
	Person p5 = p4 + 10;
	cout << "p5.m_A = " << p5.m_A << endl;
	cout << "p5.m_B = " << p5.m_B << endl;
}
int main() {
	test01();
	test02();
	test03();
	test04();
	test04();
	system("pause");
	return 0;
}

注意事项:

  1. 对于内置的数据类型的表达式的的运算符是不可能改变的,例如:+ - * /

  2. 不要滥用运算符重载,例如:operator+()实现减法,不符合常理

4.5.2 左移运算符重载

作用:可以输出自定义数据类型

代码示例:

#include<iostream>
using namespace std;
class Person{
	//当Person类中属性为private权限时且全局函数operator<<对Person类中属性进行访问时,需要在Person类中定义全局函数operator<<的声明并用friend进行修饰,目的是告诉编译器,全局函数operator<<是Person类的好朋友,允许访问Person类中的私有内容
	friend ostream& operator<<(ostream &cout, Person &p);
private:
	int m_A;
	int m_B;
public:
	//无参构造函数
	Person(){}
	//有参构造函数利用初始化列表对属性进行初始化
	Person(int a,int b):m_A(a),m_B(b) {}
	//注意: 利用成员函数无法实现函数重载左移运算符,函数本质为: p.operator<<(cout) 简化为: p << cout; 完全不符合我们需求
	void operator<<(ostream &cout) {}
};
//目前只能利用全局函数重载左移运算符,函数本质为: operator<<(cout,p) 简化为: cout << p;
ostream& operator<<(ostream &cout, Person &p) {
	cout << "p.m_A = " << p.m_A << " p.m_B = " << p.m_B;
	return cout;
}
void test01() {
	Person p = Person(100, 10);
	cout << p << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

注意:重载左移运算符配合友元可以实现输出自定义数据类型

4.5.3 递增运算符重载

作用: 通过重载递增运算符,实现自己的整型数据

代码示例:

#include<iostream>
using namespace std;
class MyInteger {
	//利用友元访问MyInteger类中的private权限的属性
	friend ostream& operator<<(ostream& cout, MyInteger myinteger);
private:
	int n_Num;
public:
	//无参构造函数对属性进行初始化
	MyInteger() {
		n_Num = 0;
	}
	//成员函数实现前置++运算符重载,函数本质为: myinteger.operator++() 简化为: ++myinteger
	MyInteger& operator++() {
		//变量先进行++运算,后参与其他运算
		n_Num++;
		//返回调用对象本身为了能够保证是对同一个对象中的属性的值进行递增操作
		return *this;
	}
	//成员函数实现后置++运算符重载,函数本质为: myinteger.operator++(int) 简化为: myinteger++
	//注意点:
	//1.int代表占位符,在C++中可以用于区分前置和后置递增运算符重载
	//2.后置++运算符重载返回值类型接收返回的一定不是对象本身,而是对象中的数据
	MyInteger operator++(int) {
		//变量是先进行其他运算,后自身进行递增运算
		MyInteger temp = *this;
		n_Num++;
		//返回调用对象本身为了能够保证是对同一个对象中的属性的值进行递增操作
		return temp;
	}
};
//利用友元访问MyInteger类中的private权限的属性
ostream& operator<<(ostream &cout,MyInteger myinteger) {
	cout << myinteger.n_Num;
	return cout;
}
//测试前置++运算符重载
void test01() {
	MyInteger myinteger;
	cout << "前置++递增结果为:" << ++myinteger << endl;
	cout << "myinteger.n_Num = " << myinteger << endl;
}
//测试后置++运算符重载
void test02() {
	MyInteger myinteger;
	cout << "后置++递增结果为:" << myinteger++ << endl;
	cout << "myinteger.n_Num = " << myinteger << endl;
}
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

注意事项:

  1. int代表占位符,在C++中可以用于区分前置和后置递增运算符重载函数
  2. 前置递增返回引用,后置递增返回值
4.5.4 赋值运算符重载

c++编译器至少给一个类添加4个函数,之前我们学过前3个函数,接下来学习第四个函数,如下:

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

如果类中有属性指向堆区,做赋值操作时需要使用深浅拷贝来解决问题

代码示例:

#include<iostream>
using namespace std;
class Person {
public:
	int* m_Age;
public:
	Person(int age) {
		//将年龄数据存储在开辟的堆区空间中,并用指针指向堆区
		m_Age = new int(age);
	}
	~Person() {
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
	}
	//成员函数实现重载赋值运算符
	Person& operator=(Person &p) {
		//编译器提供浅拷贝,类中有属性在堆区中开辟了存储数据的空间,做赋值操作时存在重复释放堆区中指向同一块存储数据的空间问题
		//this->m_Age = p.m_Age;
		//注意:对于类对象中的属性是否在堆区中开辟了存储数据的空间,在进行深拷贝之前,需要先释放属性之前在堆区中的开辟的存储数据的空间,再进行深拷贝
		if (this->m_Age != NULL) {
			delete this->m_Age;
			this->m_Age = NULL;
		}
		//深拷贝,注意:*p.m_Age代表解引用
		this->m_Age = new int(*p.m_Age);
		//返回调用对象本身为了能够保证是同一个对象
		return *this;
	}
};
void test01() {
	Person p1 = Person(10);
	Person p2 = Person(20);
	Person p3 = Person(20);
	//赋值操作
	p3 = p2 = p1;
	cout << "p1.m_Age = " << *p1.m_Age << endl;
	cout << "p2.m_Age = " << *p2.m_Age << endl;
	cout << "p3.m_Age = " << *p3.m_Age << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}
4.5.5 关系运算符重载

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

代码示例:

#include<iostream>
using namespace std;
class Person {
	friend bool operator==(Person &p1, Person &p2);
	friend bool operator!=(Person &p1, Person &p2);
private:
	string m_Name;
	int m_Age;
public:
	//构造函数对属性进行初始化
	Person(string name,int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	//成员函数实现重载==运算符
	//注意:返回值类型为bool类型的值
	bool operator==(Person &p) {
		return this->m_Name == p.m_Name && this->m_Age == p.m_Age ? true : false;
	}

	//成员函数实现重载!=运算符
	//注意:返回值类型为bool类型的值
	bool operator!=(Person &p) {
		return this->m_Name != p.m_Name || this->m_Age != p.m_Age ? true : false;
	}
};
//全局函数实现重载==运算符
//注意:1.利用友元访问Person类中的private权限的属性;2.返回值类型为bool类型的值
bool operator==(Person &p1,Person &p2) {
	return (p1.m_Name == p2.m_Name ? true:false) && (p1.m_Age == p2.m_Age? true : false);
}
//全局函数实现重载!=运算符
//注意:1.利用友元访问Person类中的private权限的属性;返回值类型为bool类型的值
bool operator!=(Person &p1, Person &p2) {
	return (p1.m_Name != p2.m_Name ? true : false) || (p1.m_Age != p2.m_Age ? true : false);
}
void test01() {
	Person p1 = Person("qzp",25);
	Person p2 = Person("qzp",23);
	//成员函数本质为: p1.operator==(p2) 简化为:p1 == p2
	//全局函数本质为: operator==(p1,p2) 简化为:p1 == p2
	//因成员函数和全局函数实现重载==运算符函数简化后调用方式一样,导致出现二义性,编译器报错,注释掉其中一个才可以正常调用
	if (p1 == p2) {
		cout << "p1和p2对象相等" << endl;
	}
	else {
		cout << "p1和p2对象不相等" << endl;
	}
	//成员函数本质为: p1.operator!=(p2) 简化为:p1 == p2
	//全局函数本质为: operator!=(p1,p2) 简化为:p1 == p2
	//因成员函数和全局函数实现重载!=运算符函数简化后调用方式一样,导致出现二义性,编译器报错,注释掉其中一个才可以正常调用
	if (p1 != p2) {
		cout << "p1和p2对象不相等" << endl;
	}
	else {
		cout << "p1和p2对象相等" << endl;
	}
}
int main() {
	test01();
	system("pause");
	return 0;
}
4.5.6 函数调用运算符重载

函数调用运算符 () 也可以重载,由于重载后使用的方式非常像函数的调用,因此称为仿函数

注意: 仿函数没有固定写法,非常灵活

示例:

#include<iostream>
using namespace std;
class MyPrint {
public:
	//成员函数实现函数调用运算符重载
	void operator()(string test) {
		cout << "test为:" << test << endl;
		cout << "调用运算符重载operator()函数" << endl;
	}
	//仿函数没有固定写法,非常灵活
	int operator()(int a,int b) {
		return a + b;
	}
};
void function(string test) {
	cout << "test为:" << test << endl;
	cout << "调用全局函数function" << endl;
}
void test01() {
	MyPrint myprint;
	//调用全局函数function
	function("qzp");
	//调用函数调用运算符重载函数
	myprint("qzp");//由于调用运算符重载使用起来非常类似于全局函数的调用,因此称为仿函数
}
void test02() {
	MyPrint myprint;
	int result = myprint(100,100);
	cout << "result = " << result << endl;
	//MyPrint()匿名函数对象
	cout << MyPrint()(1,1) << endl;
}
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

读万卷书,行万里路,博主会持续更新学习笔记,如有问题,可以留言和私信,我们一起进步!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QZP51ZX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值