C++——运算符重载

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

1、运算符重载的概念

  1. 运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
  2. 运算符重载的目的是让语法更加简洁
  3. 运算符重载不能改变本来寓意,不能改变基础类型寓意
  4. 运算符重载的本质是另一种函数调用(是编译器去调用)
  5. 这个函数统一的名字叫operator
  6. 重载函数可以写成全局或成员函数
  7. 重载函数如果写成全局的,那么双目运算符左边的是第一个参数,右边是第二个参数
  8. 重载函数如果写成成员函数,那么双目运算符的左边是this,右边是第一个参数
  9. 不能改变运算符优先级,不能改变运算符的参数个数。

2、加号运算符重载

1、同类型的对象相加

#include <iostream>
using namespace std;

class Maker
{
public:
	Maker(int id, int age)
	{
		this->id = id;
		this->age = age;
	}
	//写成成员函数,那么只需要一个参数,这个参数是加号的右边
	Maker operator+(Maker& m2)
	{
		Maker temp(this->id + m2.id, this->age + m2.age);
		return temp;
	}
public:
	int id;
	int age;
};
//重载加号运算符  全局函数方式
//Maker operator+(Maker &p1,Maker &p2)
//{
//	Maker temp(p1.id + p2.id, p1.age + p2.age);
//	return temp;
//}
void test()
{
	Maker m1(1, 10);
	Maker m2(2, 20);

	Maker m3 = m1 + m2;
	cout << m3.id << endl;
	cout << m3.age << endl;
}
int main()
{
	test();
	return 0;
}

2、不同对象类型相加

#include <iostream>
using namespace std;

class Maker
{
public:
	Maker(int id, int age)
	{
		this->id = id;
		this->age = age;
	}
	//写成成员函数,那么只需要一个参数,这个参数是加号的右边
	Maker operator+(Maker& m2)
	{
		Maker temp(this->id + m2.id, this->age + m2.age);
		return temp;
	}
public:
	int id;
	int age;
};

class Student
{
public:
	Student() {
		mid = 0;
	}
	Student(int id) {
		mid = id;
	}
public:
	int mid;
};

Maker operator+(Maker& m1, Student& s1)
{
	Maker temp(m1.id + s1.mid, 20);
	return temp;
}
Student operator+(Student& s1, Maker& m1)
{
	Student temp(s1.mid + m1.id);
	return temp;
}
void test()
{
	Maker m1(1, 10);
	Student s1(2);
	Maker m3 = m1 + s1;
	cout << m3.id << endl;
	
	Student s2 = s1 + m1;
	cout << s2.mid<<endl;
}
int main()
{
	test();
	return 0;
}

3、减号运算符重载

#include <iostream>
using namespace std;

class Maker
{
public:
	Maker(int id, int age)
	{
		this->id = id;
		this->age = age;
	}
	//写成成员函数,那么只需要一个参数,这个参数是加号的右边
	Maker operator-(Maker& m2)
	{
		Maker temp(this->id - m2.id, this->age - m2.age);
		return temp;
	}
public:
	int id;
	int age;
};
void test()
{
	Maker m1(10, 18);
	Maker m2(5, 15);

	Maker m3 = m1 - m2;
	cout << m3.id << endl;
	cout << m3.age << endl;
}
int main()
{
	test();
	return 0;
}

4、左移运算符重载

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

class Maker
{
public:
	Maker(int id, string name)
	{
		this->id = id;
		this->name = name;
	}
public:
	int id;
	string name;
};
//1、形参和实参是一个对象
//2、不能改变库类中的代码
//3、ostream中把拷贝构造函数私有化了
//4、如果要和endl一起使用,那么必须返回ostream的对象
ostream& operator<<(ostream& out, Maker& m1)
{
	cout << m1.id << " " << m1.name << endl;
	return out;
}
void test()
{
	Maker m1(10, "薯片");
	cout << m1 << endl;
}
int main()
{
	test();
	return 0;
}

5、右移运算符

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

class Maker
{
public:
	Maker(int id, string name)
	{
		this->id = id;
		this->name = name;
	}
	int getAge() {
		return this->id;
	}
public:
	int id;
	string name;
};

istream &operator>>(istream& in, Maker& m1)
{
	cin >> m1.id;
	cin >> m1.name;
	return in;
}
void test()
{
	Maker m(10, "薯片");
	cin >> m;
	cout << m.getAge() << endl;
}
int main()
{
	test();
	return 0;
}

6、关系运算符重载

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

class Maker
{
public:
	Maker() {};
	Maker(int id)
	{
		this->id = id;
	}
	bool operator==(Maker &m) {
		if (this->id == m.id) {
			return true;
		}
		return false;
	}
	bool operator!=(Maker& m) {
		if (this->id != m.id) {
			return true;
		}
		return false;
	}
public:
	int id;
};

void test()
{
	Maker m1(10);
	Maker m;
	if (m1 == m) {
		cout << "真" << endl;
	}
	else {
		cout << "假" << endl;
	}

	if (m1 != m) {
		cout << "真" << endl;
	}
	else {
		cout << "假" << endl;
	}
	
}
int main()
{
	test();
	return 0;
}

7、前置加加和后置加加

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

class Maker
{
	friend ostream& operator<<(ostream& os, Maker& m);
public:
	Maker(int id)
	{
		this->id = id;
	}
	//重置前置加加
	Maker& operator++()
	{
		++this->id;
		return *this;
	}
	//重置后置加加
	Maker operator++(int)//占位参数,必须是int
	{
		Maker tmp(*this);//tmp是局部变量,局部变量不能以引用返回
		++this->id;
		return tmp;
	}
private:
	int id;
};

ostream& operator<<(ostream& out, Maker& m) {
	cout << m.id << endl;
	return out;
}

void test()
{
	Maker m1(10);
	cout << ++m1;
	cout << m1++;
	
	
}
int main()
{
	test();
	return 0;
}

8、智能指针类

8.1、智能指针类是管理另一个类的对象的释放

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

class Maker
{
public:
	Maker() {
		cout << "Maker的无参构造" << endl;
	}
	~Maker() {
		cout << "Maker的析构函数" << endl;
	}

};

//智能指针类
class SmartPoint
{
public:
	SmartPoint(Maker* p)
	{
		this->pMaker = p;
	}
	~SmartPoint()
	{
		cout << "SmartPoint的析构函数" << endl;
		if (this->pMaker != NULL)
		{
			delete this->pMaker;
			this->pMaker == NULL;
		}

	}
private:
	Maker* pMaker;
};
void test()
{
	Maker* p = new Maker;//在堆区开辟的数据,需要手动delete掉

	SmartPoint sm(p);//栈区  会调用析构函数
	//当test()函数结束时,会调用smartPoint的析构函数。
	//在这析构函数中delete了Marker的对象,会调用Maker的析构函数
}
int main()
{
	test();
	return 0;
}

c++运算符重载,c++,算法,数据结构

8.2、指针运算符重载

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

class Maker
{
public:
	Maker() {
		cout << "Maker的无参构造" << endl;
	}
	void printMaker()
	{
		cout << "Hello Maker" << endl;
	}
	~Maker() {
		cout << "Maker的析构函数" << endl;
	}

};

//智能指针类
class SmartPoint
{
public:
	SmartPoint(Maker* p)
	{
		this->pMaker = p;
	}
	//重载指针运算符
	Maker* operator->()
	{
		return this->pMaker;
	}
	~SmartPoint()
	{
		cout << "SmartPoint的析构函数" << endl;
		if (this->pMaker != NULL)
		{
			delete this->pMaker;
			this->pMaker == NULL;
		}

	}
private:
	Maker* pMaker;
};
void test()
{
	Maker* p = new Maker;

	SmartPoint sm(p);
	
	sm->printMaker();
}
int main()
{
	test();
	return 0;
}

c++运算符重载,c++,算法,数据结构文章来源地址https://www.toymoban.com/news/detail-585821.html

8.3、重载星号

	//重载星号
	Maker& operator*()
	{
		return *pMaker;
	}

void test()
{
	Maker* p = new Maker;

	SmartPoint sm(p);
	(*sm).printMaker();
}

9、重载函数调用符号

9.1、类里有重载函数调用符号的类实例化的对象也叫仿函数

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

//一个类如果重载了函数调用符号,那么这个类实例化出的对象也叫仿函数
//仿函数的作用:1、方便代码维护
class Maker
{
public:
	Maker(string name) 
	{
		this->m_Name = name;
	};
	void printMaker()
	{
		cout << "hello " <<this->m_Name<< endl;
	}
	//重载()
	void operator()()
	{
		cout << "hello" << endl;
	}
public:
	string m_Name;
};


void test()
{
	Maker func("薯片");
	func();//看着像函数,但func是对象

}
int main()
{
	test();
	return 0;
}

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

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

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

相关文章

  • 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++中的重载运算符

    🐶博主主页: @ᰔᩚ. 一怀明月ꦿ   ❤️‍🔥 专栏系列: 线性代数,C初学者入门训练,题解C,C的使用文章,「初学」C++ 🔥 座右铭: “不要等到什么都没有了,才下定决心去做” 🚀🚀🚀大家觉不错的话,就恳求大家点点关注,点点小爱心,指点指点🚀🚀🚀 目录 🐰

    2024年02月05日
    浏览(41)
  • C++中重载输出运算符 <<

    C++输入输出标准库提供了“”和“”运算符执行输入、输出操作,但标准库只定义了基本数据类型的输入、输出操作,若要直接对类对象进行输入、输出,则需要在类中重载这两个运算符。与其他运算符不同的是, 输入、输出运算符只能重载成类的友元函数 。 通常情况下,

    2024年02月13日
    浏览(55)
  • 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)
  • c++ 友元 运算符重载详解

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

    2024年02月11日
    浏览(30)
  • C++ 面向对象(3)——重载运算符和重载函数

    C++ 允许在同一作用域中的某个 函数 和 运算符 指定多个定义,分别称为 函数重载 和 运算符重载 。 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。 当您调用一个 重载函数 或 重载运算符 时

    2024年02月10日
    浏览(39)
  • C++核心编程——详解运算符重载

    C++的一大特性就是重载,重载使得程序更加简洁高效。在C++中不只函数可以重载,运算符也可以重载, 运算符重载主要是面向对象之间的。 ①基本概念 运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 运算符重载语法: 在C++中

    2024年02月09日
    浏览(31)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包