IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 黑马:C++运算符重载(121~126) -> 正文阅读

[C++知识库]黑马:C++运算符重载(121~126)

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

1.加号运算符重载

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>


class Person {
public:

	//1.成员函数重载+号
	Person operator+(Person& p) {
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp; 
	}
	int m_A;
	int m_B;

};

void test01() {
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10; 

	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;

	//成员函数重载调用的本质
	//Person p3=p1.operator+(p2);

	//全局函数重载调用的本质
	//Person p3=operator+(p1+p2);

	Person p3 = p1 + p2;
	cout << "p3.m_A: " << p3.m_A << endl;
	cout << "p3.m_B: " << p3.m_B << endl;
}

//全局函数重载+号
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;
}


int main(int argc,char**argv) {
	
	
	test01();
	system("pause");
	return  0;
}

2.左移运算符重载

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

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>


class Person {
public:

	//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
	
	int m_A;
	int m_B;

};

//只能利用全局函数重载左移运算符
ostream&  operator<<(ostream& cout, Person& p) {
	cout << "m_A:  " << p.m_A << endl;
	cout << "m_B:  " << p.m_B << endl;
	return cout;
}

void test01() {
	Person p;
	p.m_A = 10;
	p.m_B = 10;
	cout << p<<endl; //如果上面不是ostream& operator开头的话 这行加endl会报错;
}
int main(int argc,char**argv) {
	
	
	test01();
	system("pause");
	return  0;
}

3.递增运算符重载

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

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>


class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger() {
		m_Num = 0;
	}

	//重载前置++运算符 
	MyInteger& operator++() {  //前置递增必须返回引用,为了一直对一个数据进行递增操作
		//先进行++运算
		m_Num++;

		//再将自身返回
		return *this;
	}

	//重载后置++运算符
	//void operator++(int) int代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int) {  //后置递增必须返回值而不能是引用,temp是局部变量,返回值的话出区就被释放了 
		//先记录当时结果
		MyInteger temp = *this;
		
		//然后递增
		m_Num++;

		//最后将记录结果返回
		return temp;
	}
private:
	int m_Num;
};

//重载<<运算符
ostream& operator<<(ostream& cout, MyInteger myint) {
	cout << myint.m_Num;
	return cout;
}

void test01() {
	MyInteger myint;
	cout << ++myint << endl;
}

void test02() {
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}


int main(int argc,char**argv) {
	
	
	test01();
	system("pause");
	return  0;
}

4.赋值运算符重载

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>


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) {
		//编译器提供的是浅拷贝
		//m_Age=p.m_Age;

		//应该判断是否有属性在堆区,如果有,先释放干净,然后再深拷贝
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}

		//深拷贝
		m_Age = new int(*p.m_Age);

		//返回对象本身
		return *this;
	}

	int *m_Age;
};

void test01() {
	Person p1(18);
	cout << "p1年龄:" << *p1.m_Age << endl;

	Person p2(20);
	p2 = p1;

	cout << "p2年龄: " << *p2.m_Age << endl;
}

int main(int argc,char**argv) {
	
	
	test01();
	system("pause");
	return  0;
}

5.关系运算符重载

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

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>


class Person {
public:
	Person(string name, int age) {
		m_Name = name;
		m_Age = age;
	}

	//重载==号
	bool operator==(Person& p) {
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
			return true;
		}
		return false;
	}

	//重载!=号
	bool operator!=(Person& p) {
		if (this->m_Name != p.m_Name && this->m_Age != p.m_Age) {
			return true;
		}
		return false;
	}
	string m_Name;
	int m_Age;

};

void test01() {
	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(int argc,char**argv) {
	
	
	test01();
	system("pause");
	return  0;
}

6.函数调用运算符重载

函数调用运算符()也可以重载

由于重载后使用的方式很像函数的调用,因此成为仿函数

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

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>


class MyPrint {
public:
	//重载函数调用运算符
	void operator()(string test) {
		cout << test << endl;
	}
};

void test01() {
	MyPrint myprint;
	myprint("hello world");
}

//仿函数非常灵活,没有固定写法
//加法类
class MyAdd {
public:
	int operator()(int num1, int num2) {
		return num1 + num2;
	}
};

void test02() {
	MyAdd myadd;
	int ret=myadd(10, 20);
	cout << "ret=  " << ret << endl;

	//或者:匿名函数对象
	cout << MyAdd()(10, 20) << endl;

}
int main(int argc,char**argv) {
	
	
	test01();
	test02();
	system("pause");
	return  0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-07 13:40:26  更:2021-10-07 13:42:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 3:10:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码