您现在的位置是:群英 > 开发技术 > 编程语言
C语言运算符重载概念是什么,如何实现?
Admin发表于 2022-02-15 17:52:411263 次浏览

    这篇文章我们来了解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语言运算符重载就介绍到这,上述示例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多大家可以关注群英网络其它的相关文章。

文本转载自PHP中文网

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

相关信息推荐
2022-09-02 17:59:22 
摘要:在之前的文章中详细的介绍了几种PHP数组操作中常见数组合并的相关知识,本篇文章我们继续来看一下PHP数组操作的相关知识,希望对大家有帮助!
2022-09-02 17:59:22 
摘要:在之前的文章中详细的介绍了PHP中怎样去计算数组内所有元素和的相关知识,本篇文章我们继续来看一下PHP中数组的知识,看完了怎么计算数组的元素和,下面我们看一下怎么计算数组的元素乘积。
2022-10-08 17:52:29 
摘要:在PHP中内置了array_shif函数来删除数组中的第一个元素,还有array_pop函数用来删除数组中的最后一个元素,下面我们就分别来看一下这两种函数的用法。虽然都是删除元素,这两个函数不同于unset函数和array_splice函数,本篇介绍的两种函数不需要知道数组元素的键值和键名。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部