C++类与对象的运算符重载详解

 

运算符重载

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

 

加号运算符重载

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

#include <iostream>
using namespace std;
class Person {
public:
	// 构造函数
	Person(int num1, int num2){
		this->num1 = num1;
		this->num2 = num2;
	}
	// 加法函数
	Person operator+(const Person &p){
		Person temp(0, 0);
		temp.num1 = this->num1 + p.num1;
		temp.num2 = this->num2 + p.num2;
		return temp;
	}
	// 打印输出
	void printMessage() {
		cout << "num1 = " << num1 << " num2 = " << num2 << endl;
	}
private:
	int num1;
	int num2;
};
void func() {
	Person p1(1, 2);
	Person p2(3, 4);
	Person p3 = p1.operator+(p2); // 可以简化为 Person = p1 + p2;
	p3.printMessage(); // num1 = 4 num2 = 6
	Person p4 = p3 + p2;
	p4.printMessage(); // num1 = 7 num2 = 10
}
int main() {
	func();
	system("pause");
	return 0;
}

 

左移运算符重载

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

第一种:成员函数(对象 << cout)

#include <iostream>
using namespace std;
class Person {
public:
	// 构造函数
	Person(int num1, int num2){
		this->num1 = num1;
		this->num2 = num2;
	}
	// 左移运算符 p.operator<<(cout); 简化版本 p << cout
	void operator <<(ostream &cout) {
		cout << "num1 = " << num1 << " num2 = " << num2 << endl;
	}
private:
	int num1;
	int num2;
};
void func() {
	Person p1(1, 2);
	p1.operator<<(cout); // 简化为 p1 << cout
	Person p2(3, 4);
	p2 << cout;
}
int main() {
	func();
	// outputs:num1 = 1 num2 = 2
	//		   num1 = 3 num2 = 4
	system("pause");
	return 0;
}

第二种:全局函数(cout << 对象)

#include <iostream>
using namespace std;
class Person {
	// 友元
	friend ostream& operator <<(ostream &cout, Person &p);
public:
	// 构造函数
	Person(int num1, int num2){
		this->num1 = num1;
		this->num2 = num2;
	}
private:
	int num1;
	int num2;
};
// 左移运算符 p.operator<<(cout); 简化版本 p << cout
ostream& operator <<(ostream &cout, Person &p) {
	cout << "num1 = " << p.num1 << " num2 = " << p.num2;
	return cout;
}
void func() {
	Person p1(1, 2);
	operator<<(cout, p1); // 简化为 cout << p1;
	cout << endl;
	cout << p1 << " 你是猪!" << endl;
}
int main() {
	func();
	// outputs:num1 = 1 num2 = 2
	//		   num1 = 1 num2 = 2 你是猪!
	system("pause");
	return 0;
}

 

递增运算符重载

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

#include <iostream>
using namespace std;
class MyInteger {
	//friend ostream& operator <<(ostream &cout, MyInteger &p);
	friend ostream& operator <<(ostream &cout, MyInteger p);
public:
	// 构造函数
	MyInteger(){
		this->m_num = 0;
	}
	// 重载前置递增运算符
	MyInteger& operator++() {
		++this->m_num;
		return *this;
	}
	// 重载后置递增运算符
	MyInteger operator++(int) { // int 是占位参数,用于区分前置和后置递增
		MyInteger temp = *this;
		++this->m_num;
		return temp;
	}
private:
	int m_num;
};
// 用于前置递增,左移运算符 p.operator<<(cout); 简化版本 p << cout
//ostream& operator <<(ostream &cout, MyInteger &p) {
//	cout << "m_num = " << p.m_num;
//	return cout;
//}
// 用于后置递增,左移运算符 p.operator<<(cout); 简化版本 p << cout
ostream& operator <<(ostream &cout, MyInteger p) {
	cout << "m_num = " << p.m_num;
	return cout;
}
void func() {
	MyInteger m_int;
	//cout << m_int.operator++() << endl; // m_num = 1
	//cout << ++m_int << endl; // m_num = 2
	cout << m_int++ << endl; // m_num = 0
	cout << m_int << endl; // m_num = 1
}
int main() {
	func();
	system("pause");
	return 0;
}

 

递减运算符重载

通过重载递减运算符,实现自己的整型数据

#include <iostream>
using namespace std;
class MyInteger {
	friend ostream& operator <<(ostream &cout, MyInteger &p);
	//friend ostream& operator <<(ostream &cout, MyInteger p);
public:
	// 构造函数
	MyInteger(){
		this->m_num = 0;
	}
	// 重载前置递减运算符
	MyInteger& operator--() {
		--this->m_num;
		return *this;
	}
	// 重载后置递减运算符
	MyInteger operator--(int) { // int 是占位参数,用于区分前置和后置递增
		MyInteger temp = *this;
		--this->m_num;
		return temp;
	}
private:
	int m_num;
};
// 用于前置递减,左移运算符 p.operator<<(cout); 简化版本 p << cout
ostream& operator <<(ostream &cout, MyInteger &p) {
	cout << "m_num = " << p.m_num;
	return cout;
}
// 用于后置递减,左移运算符 p.operator<<(cout); 简化版本 p << cout
//ostream& operator <<(ostream &cout, MyInteger p) {
//	cout << "m_num = " << p.m_num;
//	return cout;
//}
void func() {
	MyInteger m_int;
	cout << m_int.operator--() << endl; // m_num = -1
	cout << --m_int << endl; // m_num = -2
	//cout << m_int-- << endl; // m_num = 0
	//cout << m_int << endl; // m_num = -1
}
int main() {
	func();
	system("pause");
	return 0;
}

 

赋值运算符重载

C++ 编译器至少给一个类添加 4 个函数(此处不举例,具体可见核心篇 5)

#include <iostream>
using namespace std;
class Student {
public:
	// 构造函数
	Student(int id) {
		m_id = new int(id); // 从堆区开辟内存用来存储 id
	}
	// 拷贝构造函数
	Student(const Student &s) {
		this->m_id = new int(*s.m_id);
	}
	// 析构函数
	~Student() {
		if (m_id != NULL) {
			delete m_id;
			m_id = NULL;
		}
	}
	// 重载赋值运算符
	Student& operator=(Student &s) {
		if (m_id != NULL) {
			delete m_id;
			m_id = NULL;
		}
		m_id = new int(*s.m_id);
		return *this;
	}
	// 取出 id
	int getId() {
		return *m_id;
	}
private:
	int *m_id;
};
void func() {
	Student s1(1);
	cout << s1.getId() << endl; // 1
	// 用拷贝构造函数来作深拷贝
	Student s2(s1);
	cout << s2.getId() << endl; // 1
	// 用赋值重载运算符来作赋值
	Student s3(2); // id 为 2
	s1 = s3; // 复杂版本:s1.operator=(s3)
	cout << s1.getId() << endl; // 2
	// 多段赋值运算符,例如 a = b = c
	Student s4(3);
	s1 = s2 = s3 = s4; // id 均为 3
	cout << s1.getId() << s2.getId() << s3.getId() << s4.getId() << endl; // 3333
}
int main() {
	func();
	system("pause");
	return 0;
}

 

关系运算符重载

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

#include <iostream>
using namespace std;
class Student {
public:
	// 构造函数
	Student(int id) {
		m_id = new int(id); // 从堆区开辟内存用来存储 id
	}
	// 拷贝构造函数
	Student(const Student &s) {
		this->m_id = new int(*s.m_id);
	}
	// 析构函数
	~Student() {
		if (m_id != NULL) {
			delete m_id;
			m_id = NULL;
		}
	}
	// 重载 == 运算符
	bool operator==(Student &s) {
		if (this->getId() == s.getId()) {
			return true;
		}
		else {
			return false;
		}
	}
	// 取出 id
	int getId() {
		return *m_id;
	}
private:
	int *m_id;
};
void func() {
	Student s1(1);
	Student s2(1);
	Student s3(2);
	if (s1 == s2) {
		cout << "s1 和 s2 相等" << endl;
	}
	if (s1 == s3) {
		cout << "s1 和 s3 相等" << endl;
	}
	else {
		cout << "s1 和 s3 不相等" << endl;
	}
}
int main() {
	func();
	// outputs:s1 和 s2 相等
	//		   s1 和 s3 不相等
	system("pause");
	return 0;
}

 

函数调用运算符重载

特点:函数调用运算符 () 也可以重载;重载后调用的方式很像函数,被称为仿函数;没有固定写法,非常灵活

#include <iostream>
using namespace std;
#include <string>
class Print {
public:
	void operator()(string text) {
		cout << "利用函数调用重载运算符打印输出:" << text << endl;
	}
};
void func() {
	Print p1;
	p1("你是一个小猪猪!"); // 利用函数调用重载运算符打印输出:你是一个小猪猪!
}
int main() {
	func();
	// outputs:s1 和 s2 相等
	//		   s1 和 s3 不相等
	system("pause");
	return 0;
}

 

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程宝库的更多内容!

之前只是看过C++虚函数表相关介绍,今天有空就来写代码研究一下。面向对象的编程语言有3大特性:封装、继承和多态。C++是面向对象的语言(与C语言主要区别),所以C++也拥有多态的特性。C++中多态分为两种:静态多态和动 ...