温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中运算符重载的示例分析

发布时间:2021-10-18 12:41:31 来源:亿速云 阅读:126 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“C++中运算符重载的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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++中运算符重载的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI