爱上c++的第五天(核心课程):运算符重载

166 阅读3分钟

你的c++学习路上明灯_哔哩哔哩_bilibili 上述是我在B站发布的相应视频

目录

一,加号运算符重载:实现两个自定义数据类型相加的运算

1,通过全局函数

2,通过成员函数

二,左移运算符重载    “<<”

三,递增运算符   “++”

四,赋值运算符

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

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

六,函数调用运算符重载  “() ”

1,由于重载后的操作非常像函数的调用,因此被称为仿函数

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

首先我们来了解一下什么叫做运算符重载,众所周知,运算符只能对计算机内部的数据类型进行处理,但是我们在编写程序时,用的更多的却是自定义类型,尤其是我们的c++中,c++又叫做带类的c。类就是一个自定义类型。那么如果我们想让两个类之间进行相加或相减的时候,就可以对相应的符号进行重载,跟函数重载是差不多的意思。

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

下面让我们来了解一下一些常见的运算符重载,打字不易,点赞加关注鼓励一下吧。

一,加号运算符重载:实现两个自定义数据类型相加的运算

1,通过全局函数

2,通过成员函数

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class person {
public:
	int m_A;
	int m_B;

	//1.通过成员函数重载
	/*person operator+(person& p) {
		person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_A + p.m_A;
		return temp;
	}*/
	
};
//2.通过全局函数重载
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& p, int num) {
	person temp;
	temp.m_A = p.m_A + num;
	temp.m_B = p.m_B + num;
	return temp;
}
void test() {
	person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	person p2;
	p2.m_A = 10;
	p2.m_B = 10;

	person p3=p1+p2;
	//1.成员函数调用实质   person p3 = p1.operator+(p2);
	//2.全局函数调用实质   person p3 =operator+(p1,p2);
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;

	//运算符重载也可以发生函数重载
	person p4 = p3 + 100;
}

int main() {
	test();

	return 0;
}

二,左移运算符重载    “<<”

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class person {
	friend ostream& operator<<(ostream& cout, person& p);
private:
	int m_A;
	int m_B;
public:
	person(int a, int b) {
		m_A = a;
		m_B = b;
	}
	

};

//链式访问的思想
ostream& operator<<(ostream& cout,person &p) {
	cout << "p.m_A = " << p.m_A;
	cout << "p.m_B = " << p.m_B;
	return cout;
}

void test() {
	person p(10,10);
	cout << p << endl;
}

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

//不能利用成员函数进行重载,因为无法实现(无法实现cout在左边),所以只能使用全局函数。

三,递增运算符   “++”

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger Myint);
private:
	int m_Num;

public:
	MyInteger() {
		m_Num = 0;
	}
//前置递增返回引用,后置递增返回值
	//1.重载前置++运算符
	MyInteger& operator++() {
		m_Num++;
		return *this;
	}
	//2.重载后置++运算符
	MyInteger operator++(int) {
		MyInteger temp = *this;
		//temp.m_Num  = m_Num;
		m_Num++;
		return temp;
	}


};

//重载后置运算符时,后面的参数不能带引用,因为后置运算符返回的不是原本的对象,而是又重新创造的一个对象;不然就会报错,也不容易发现
ostream& operator<<(ostream& cout, MyInteger Myint) {
	cout << Myint.m_Num << endl;
	return cout;
}

void test1() {
	MyInteger Myint;
	cout << Myint << endl;
	cout << ++Myint << endl;
	cout << Myint << endl;

}
void test2() {
	MyInteger Myint;
	
	cout << Myint++ << endl;
	cout << Myint << endl;

}
int main() {
	test1();
	test2();

	return 0;
}

四,赋值运算符

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

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

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

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

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

#define _CRT_SECURE_NO_WARNINGS 1

#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 = new int(*p.m_Age);
		//只有返回对象本身才能进行链式访问
		return *this;
	}

	int* m_Age;


};

void test1() {
	person p1(10);
	person p2(20);
	cout << "p1的年龄为: " << *p1.m_Age << endl;
	cout << "p2的年龄为: " << *p2.m_Age << endl;
	person p3(30);
	p2 = p1 = p3;
	cout << "p1的年龄为: " << *p1.m_Age << endl;
	cout << "p2的年龄为: " << *p2.m_Age << endl;
	cout << "p3的年龄为: " << *p3.m_Age << endl;
}


int main() {
	test1();

	return 0;
}

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

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;
#include<string>

class person {
public:
	int m_Age;
	string m_Name;
	person(string name, int age) {
		m_Name = name;
		m_Age = age;
	}
	
	//对需要进行判断的,直接使用布尔类型
	bool operator==(person& p) {
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name) {
			return true;
		}
		return false;
	}
	bool operator!=(person& p) {
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name) {
			return false;
		}
		return true;
	}
};

void test1() {
	person p1("Tom", 18);
	person p2("Tom", 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() {
	test1();

	return 0;
}

六,函数调用运算符重载  “() ”

1,由于重载后的操作非常像函数的调用,因此被称为仿函数

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

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;
#include<string>

//创建输出类
class MyPrint {
public:
	
	void operator()(string test) {
		cout << test << endl;
	}
};
void myprint1(string test) {
		cout << test << endl;
}

void test1() {
	MyPrint myprint;
	//由于使用起来非常类似于函数调用,因此称为仿函数。
	myprint("hello world");

	myprint1("hello world");
}

//仿函数十分灵活,没有固定的写法

//加法类
class MyAdd {
public:
	int operator()(int num1, int num2) {
		return num1 + num2;
	}

};

int myadd1(int num1,int num2) {
	return num1 + num2;
}

void test2() {
	MyAdd myadd;
	myadd(100, 100);
	myadd1(100, 100);

	//匿名函数对象
	cout << MyAdd()(100, 100) << endl;
	//MyAdd()就代表一个匿名对象
}

int main() {
	test1();
	test2();

	return 0;
}

运算符重载使得程序的灵活性大大的提高。