运算符重载(个人学习笔记黑马学习)

这篇具有很好参考价值的文章主要介绍了运算符重载(个人学习笔记黑马学习)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、加号运算符重载

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

//加号运算符重载
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;
};


//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& p1, int num) {
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}

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 p4 = p1 + 100;// Person + int
	cout << "p4.m_A = " << p4.m_A << endl;
	cout << "p4.m_B = " << p4.m_B << endl;

	Person p3 = p1 + p2;

	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
}





int main() {

	test01();

	system("pause");
	return 0;
}

运算符重载(个人学习笔记黑马学习),黑马c++学习笔记,学习,笔记,c++


2、左移运算符重载

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

//左移运算符

class Person {
	friend ostream& operator<<(ostream& cout, Person& p);
//public:

public:
	Person(int a, int b) {
		m_A = a;
		m_B = b;
	}
private:
	//利用成员函数重载 左移运算符 p,operator<<(cout) 简化版本p<<cout
	//不会利用成员函数重载<<运算符 因为无法实现 cout在左侧
	/*void operator<<(cout) {

	}*/

	int m_A;
	int m_B;

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

}

void test01() {
	/*Person p;
	p.m_A = 10;
	p.m_B = 10;*/

	Person p(10,10);

	cout << p << endl;;
}

int main() {

	test01();

	system("pause");
	return 0;
}

3、递增运算符重载

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

//递增运算符重载

//自定义整型
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) {
		//先 记录当前结果
		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;
	cout << ++(++myint) << endl;
	cout << myint << endl;
}

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

int main() {

	//test01();
	test02();

	system("pause");
	return 0;
}

4、赋值运算符重载

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

//赋值运算符重载
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);
	Person p2(20);
	Person p3(30);


	p3 = p2 = p1;//赋值操作


	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;
	cout << "p3的年龄为:" << *p3.m_Age << endl;

}

int main() {

	test01();

	system("pause");
	return 0;
}

运算符重载(个人学习笔记黑马学习),黑马c++学习笔记,学习,笔记,c++


5、关系运算符重载

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

//重载关系运算符

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 false;
		}
		return true;
	}

	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() {

	test01();

	system("pause");
	return 0;
}

运算符重载(个人学习笔记黑马学习),黑马c++学习笔记,学习,笔记,c++


6、函数调用运算符重载

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

//函数调用运算符重载

//打印输出类
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(100, 100);
	cout << "ret = " << ret << endl;

	//匿名函数对象
	cout << MyAdd()(100, 100) << endl;
}

int main() {

	//test01();
	test02();


	system("pause");
	return 0;
}

运算符重载(个人学习笔记黑马学习),黑马c++学习笔记,学习,笔记,c++文章来源地址https://www.toymoban.com/news/detail-695123.html

到了这里,关于运算符重载(个人学习笔记黑马学习)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C++——运算符重载

    运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 运算符重载的目的是让语法更加简洁 运算符重载不能改变本来寓意,不能改变基础类型寓意 运算符重载的本质是另一种函数调用(是编译器去调用) 这个函数统一的名字叫opera

    2024年02月16日
    浏览(40)
  • 【C++】运算符重载

    目录 1. 基本概念 1.1 直接调用一个重载的运算符函数 1.2 某些运算符不应该被重载 1.3 使用与内置类型一致的含义 1.4 赋值和复合赋值运算符 1.5 选择作为成员或者非成员 2. 输入和输出运算符 2.1 输出运算符重载 2.2 输入运算符重载 3. 算术和关系运算符 3.1 算数运算符重载 3.2

    2024年02月11日
    浏览(37)
  • C++:重载运算符

    1.重载不能改变运算符运算的对象个数 2.重载不能改变运算符的优先级别 3.重载不能改变运算符的结合性 4.重载运算符必须和用户定义的自定义类型的对象一起使用,其参数至少应该有一个是类对象,或类对象的引用 5.重载运算符的功能要类似于该运算符作用于标准类型数据

    2024年02月10日
    浏览(37)
  • 复习 --- C++运算符重载

    .5 运算符重载 运算符重载概念:对已有的运算符重新进行定义,赋予其另外一种功能,以适应不同的数据类型 4.5.1 加号运算符重载 作用:实现两个自定义数据类型相加的运算 4.5.2 左移运算符重载 4.5.3递增运算符重载 作用:通过重载递增运算符,实现自己的整型数据 4.5.4 赋

    2024年02月07日
    浏览(35)
  • C++——类和对象3|日期类型|Cout运算符重载|Cin运算符重载|const成员|

    目录 日期类型  Date.h  Date.cpp  Test.cpp  实现Cout运算符重载  实现Cin运算符重载  根据日期算星期  修改后完整代码   Date.h  Date.cpp  const成员  取地址及const取地址操作符重载 习题  计算日期到天数转换     一个类到底可以重载哪些运算符,要看哪些运算符对这个类型有

    2023年04月13日
    浏览(49)
  • 【C++】中类的6个默认成员函数 取地址及const成员函数 && 学习运算符重载 && 【实现一个日期类】

    1.1 运算符重载【引入】 C++为了增强代码的可读性引入了 运算符重载 ,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。 函数名字为: operator后面接需要重载的运算符符号。 函数原型:

    2024年02月21日
    浏览(38)
  • c++ 友元 运算符重载详解

    c++是面向对象的,目的之一:封装 封装: 优点之一,就是安全。 缺点:在某些特殊的场合,不是很方便。 华为与IBM 40亿的咨询故事 IBM需要对华为各级部门做深度咨询分析, 为了提高咨询效率,由任正非直接授权,直接获取各部门的所有权限。 使用前提: 某个类需要实现

    2024年02月11日
    浏览(31)
  • C++中重载相等运算符 ==

    相等运算(==)是一种关系运算,与不等运算(!=)关系密切。 通常情况下,C++ 中的类通过定义相等运算符来检验两个对象是否相等。也就是说它们会比较对象的每一个数据成员,只有当所有对应的 成员都相等时才认为两个对象相等。依据这一思想,我们的 Sales_data 类的相等

    2024年02月12日
    浏览(33)
  • 『C++成长记』运算符重载

    🔥 博客主页: 小王又困了 📚 系列专栏: C++ 🌟 人之为学,不日近则日退 ❤️ 感谢大家点赞👍收藏⭐评论✍️ 目录 一、运算符重载 📒1.1两个日期大小的比较 📒1.2运算符重载的引入 📒1.3将运算符重载函数写成成员函数 二、赋值运算符重载      我们比较两个对象的大

    2024年02月03日
    浏览(24)
  • C++语法——详解运算符重载

    运算符重载是C++的一个重要特性。有了运算符重载,在代码编写时能更好的实现封装。 目录 一.运算符重载介绍 二.运算符重载形式 (一).参数 (二).返回值 (三).应用 三.特殊的运算符重载 (一).默认赋值运算符重载 (二).自增运算符A++与++A (三).流提取与流插入

    2023年04月25日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包