C++运算符重载

本文深入探讨了C++中的运算符重载,包括友元函数和类成员函数两种重载方式,详细阐述了流运算符、增量和减量运算符的重载,以及对象隐式转换的应用。通过实例展示了如何使用运算符重载实现自定义类型的数据操作,如复数加法、比较和流输入输出。此外,还涵盖了综合案例,演示了各种运算符的重载组合应用。
摘要由CSDN通过智能技术生成

C++运算符重载

前言

  • 什么是运算符重载
    • 赋予运算符具有操作自定义类型数据功能
  • 运算符重载的实质是什么
    • 运算符重载的实质本身就是函数调用
      • 运算符重载函数的写法
        函数返回值 函数名(函数参数)
        函数返回值:运算符运算完成后的值决定的 Complex
        函数名 :operator加上重载运算符组成函数名 operator+
        参数 :看运算符的操作数,具体参数个数是要看你重载函数形式是什么
        函数体 :学运算符具体想要的操作


提示:以下是本篇文章正文内容,下面案例可供参考

一、友元函数重载运算符

#include <iostream>
using namespace std;

class Complex
{
public:
	Complex(int a=0, int b=0) :a(a), b(b)
	{

	}
	void print()
	{
		cout << a << endl;
		cout << b << endl;
	}
	friend Complex operator+ (Complex one, Complex two);
	protected:
	int a;
	int b;
};
//友元重载:参数个数就是操作数据
Complex operator+ (Complex one, Complex two)
{
	return Complex(one.a + two.a, one.b + two.b);
}
int main()
{
	Complex one(1, 1);
	Complex two(2, 0);
	Complex three;
	three = one + two;	//Complex 重载函数的隐式调用
	three.print();
	//显式调用
	Complex result;
	result = operator+(one, two);
	while (1);
	return 0;
}

运算结果:
在这里插入图片描述

二、类成员函数重载运算符

#include <iostream>
using namespace std;

class Complex
{
public:
	Complex(int a=0, int b=0) :a(a), b(b){}
	void print()
	{
		cout << a << endl;
		cout << b << endl;
	}
	friend Complex operator+ (Complex one, Complex two);
	//类成员函数重载,参数个数等于操作减一
	bool operator> (Complex object)
	{
		if (this->a > object.a)
		{
			return true;
		}
		else if (this->a == object.a&&this->b > object.b)
		{
			return true;
		}
		else
			return false;
	}
protected:
	int a;
	int b;
};
//友元重载:参数个数就是操作数据
Complex operator+ (Complex one, Complex two)
{
	return Complex(one.a + two.a, one.b + two.b);
}
int main()
{
	Complex one(1, 1);
	Complex two(2, 0);
	Complex three;
	three = one + two;	//Complex 重载函数的隐式调用
	three.print();
	//显式调用
	Complex result;
	result = operator+(one, two);
	if (one > two)	//one>two是bool
	{
		cout << "one比较大" << endl;
	}
	if (one.operator> (two))	
	{
		cout << "one比较大" << endl;
	}

	while (1);
	return 0;
}

运算结果:
在这里插入图片描述

三、特殊运算符重载

1.流运算符重载

  • cin类型:istream类的对象
  • cout类型:ostream类的对象
  • 流运算符>> <<
  • 必须采用友元函数的形式重载
#include <iostream>
#include <string>
using namespace std;

class STU
{
public:
	STU(string name="", int age=18) :name(name), age(age){}
	friend istream& operator>>(istream& in, STU& lisi);
	friend ostream& operator<<(ostream& out, STU& lisi);
protected:
	string name;
	int age;
};
//其他运算符
//=()-》[]只能采用类的成员函数形式重载
//流重载采用友元方式
//. .*  ?: :: 不能被重载
istream& operator>>(istream& in, STU& lisi)
{
	in >> lisi.name >> lisi.age;
	return in;
}
ostream& operator<<(ostream& out, STU& lisi)
{
	out << lisi.name << "\t"<<lisi.age << endl;
	return out;
}
int main()
{
	string str;
	cin >> str;
	cout << str;
	STU lisi;
	cin >> lisi;		//void operator>>(istream& in,STU& lisi)
	cout << lisi;	//void operator<<(ostream& out,STU& lisi)
	cin >> str >> lisi;
	cout << str << endl << lisi;
	while (1);
	return 0;
}

运算结果:
在这里插入图片描述

2.++ --运算符重载

  • 解决问题:前置和后置的问题:
    • 增加无用参数 int去表示当前运算符重载是后置操作

3.文本重载

4.其他运算符

  • =()-》[]只能采用类的成员函数形式重载
  • 流重载采用友元方式
  • . .* ?: :: 不能被重载
#include <iostream>
#include <string>
using namespace std;
class STU
{
public:
	STU(string name, int age) :name(name), age(age){}
	friend ostream& operator<<(ostream& out, STU& object)
	{
		out << object.name << "\t" <<object.age<< endl;
		return out;
	}
	STU operator++(int)	//需要一个无用参数 充当标记
	{
		return STU(name, age++);
	}
	STU operator++()
	{

		return STU(name, ++age);
	}
protected:
	string name;
	int age;
};

int main()
{
	STU lisi("李四", 18);
	cout << lisi << endl;
	int num = 1;
	int result = num++;
	cout << result << "\t" << num << endl;
	result = ++num;
	cout << result << "\t"<<num << endl;

	STU object = lisi++;  //age=18 lisi:19
	cout << object;		 //age:20  lisi:20
	object = ++lisi;
	cout << object<<lisi;
	while (1);
	return 0;
}

运行结果:
在这里插入图片描述

四、对象隐式转换

#include <iostream>
using namespace std;
class STU
{
public:
	//类的对象的隐式转换	operator
	operator int()
	{
		return age;
	}
	STU(string name, int age):name(name), age(age){}
protected:
	string name;
	int age;
};

int main()
{
	STU lisi("李四", 18);
	int lisiage = lisi;
	cout << lisiage << endl;
	while (1);
	return 0;
}

运行结果:

![
在这里插入图片描述](https://img-blog.csdnimg.cn/4fc44c73b0ef4c9dafef44a71f970ff3.png)

五、重载综合案例

#include <iostream>
#include <string>
#include <functional>
#include <memory>
using namespace std;
class Int
{
public:
	Int(int num) :num(num) {}
	int& data() 
	{
		return num;
	}
	string tostr() 
	{
		return to_string(num);
	}
	//算术运算符重载
	Int operator+(const Int& value) 
	{
		return Int(this->num + value.num);
	}
	//友元重载 :操作数-1 等于重载函数的参数个数
	friend Int operator-(const Int& one, const Int& two) 
	{
		//operator-(one,two);
		return Int(one.num - two.num);
	}
	Int operator+=(const int& value) 
	{
		return Int(this->num + value);
	}
	Int operator+=(const Int& value)
	{
		return Int(this->num + value.num);
	}

	Int operator++(int) 
	{
		return Int(this->num++);
	}
	Int operator++() 
	{
		return Int(++this->num);
	}

	Int operator&(const Int& value) 
	{
		return Int(this->num & value.num);
	}
	bool operator!() 
	{
		return !this->num;
	}
	Int operator-() 
	{
		return Int(-this->num);
	}

	friend ostream& operator<<(ostream& out, const Int& object) 
	{
		out << object.num << endl;
		return out;
	}
	friend  istream& operator>>(istream& in, Int& object) 
	{
		in >> object.num;
		return in;
	}
	int* operator&() 
	{
		return &this->num;
	}
	bool operator>(const Int& object) 
	{
		return this->num > object.num;
	}

protected:
	int num;
};
void print(const int& num) 
{
	cout << num << endl;
}

//重载[]
class myvector 
{
public:
	myvector(int size) 
	{
		base = new int[size] {0};
	}
	int& operator[](int index) 
	{
		return base[index];
	}
protected:
	int *base;
};
//重载()运算符
class Function 
{
	typedef void(*PF)();
public:
	Function(PF pf) :pf(pf) {}
	void operator()() 
	{
		pf();
	}
protected:
	PF pf;
};

//->
struct MM
{
	string name;
	int age;
	MM(string name, int age) :name(name), age(age) {}
};
class Auto_ptr 
{
public:
	Auto_ptr(int* ptr) :ptr(ptr) {}
	Auto_ptr(MM* ptr) :ptrMM(ptr) {}
	int& operator*() 
	{
		return *ptr;
	}
	MM* operator->() 
	{
		return ptrMM;
	}

	~Auto_ptr() 
	{
		if (ptrMM) 
		{
			delete ptr;
			ptr = nullptr;
		}
		if (ptrMM)
		{
			delete ptrMM;
			ptrMM = nullptr;
		}
	}
protected:
	int* ptr;
	MM* ptrMM;
};
void testAuto_ptr() 
{
	//int* pInt = new int(18);
	//Auto_ptr ptr(pInt);
	//上面两行等效下面一行
	Auto_ptr ptr(new int(18));
	cout << *ptr << endl;
	Auto_ptr ptrMM(new MM("mm", 19));
	cout << ptrMM->name<< endl;
	cout << ptrMM->age << endl;

	auto_ptr<int> autoPtr(new int(18));
	cout << *autoPtr << endl;
	auto_ptr<MM> autoPtrMM(new MM("mm", 19));
	cout << autoPtrMM->name << endl;
	cout << autoPtrMM->age << endl;
}
void print() 
{
	cout << "测试函数" << endl;
}
int max(int a, int b) 
{
	return a + b;
}




int main() 
{
	print(1);
	int a = 1;
	print(a);
	Int num(10);
	cout << num;
	cout << -num;

	myvector vec(5);
#if 0
	for (int i = 0; i < 5; i++) 
	{
		cin >> vec[i];
	}
	for (int i = 0; i < 5; i++) 
	{
		cout << vec[i];
	}
#endif
	Function p(print);
	p();
	//function<int(int,int)> pf(max);
	//cout << pf(1, 2) << endl;
	testAuto_ptr();
	while (1);
	return 0;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值