C++ 运算符重载operator(看完就会)

成员函数,重载+号

实现两个对象属性相加

#include <iostream>
using namespace std;

class Person
{
public:
	// 成员函数重载+
	// operator 是关键字  
	// + 号是你要重载的运算符,想重载-就写-
	Person operator+(Person &p)
	{
		Person temp;
		temp.m_a = this->m_a + p.m_a;	// this->m_a 表示当前调用的对象本身
		temp.m_b = this->m_b + p.m_b;
		return temp;
	}
	int m_a;
	int m_b;
};

int main()
{
	Person p1;
	p1.m_a = 10; p1.m_b = 20;
	Person p2;
	p2.m_a = 11; p2.m_b = 21;
	// Person p3 = p1.operator+(p2); // 本质调用
	Person p3 = p1 + p2;	// 简写
	cout<<p3.a<<p3.b<<endl;
	return 0;
}

全局函数重载+ 号

通过全局函数实现重载 +

#include <iostream>
using namespace std;

class Persons
{
public:
	int m_a;
	int m_b;
};

// 全局函数重载+
Persons operator+(Persons& p1, Persons& p2)
{
	Persons temp;
	temp.m_a = p1.m_a + p2.m_a;
	temp.m_b = p2.m_b + p2.m_b;
	return temp;
}

int main()
{
	Persons p1;
	p1.m_a = 10; p1.m_b = 20;
	Persons p2;
	p2.m_a = 11; p2.m_b = 21;
	// Persons p3 = operator+(p1, p2);	// 本质写法
	Persons p3 = operator+(p1, p2); // 简写
	cout << p3.m_a <<" " << p3.m_b;
	return 0;
}

重载<<操作符

实现cout<<s1; 输出s1对象的值

#include <iostream>
using namespace std;

class stu1
{	// friend m_a 和m_b 都是私有成员,需要是朋友才能访问,所以加上友元(friend)
	friend ostream& operator<<(ostream& cout, stu1& s);
public:
	stu1(int a,int b)
	{
		m_a = a;
		m_b = b;
	}
private:
	int m_a;
	int m_b;
};

// 重载<<操作符
// 把cout引用返回去,要是不返回引用,main内不能进行添加输出(<<endl)
// 俗称链式编程
ostream& operator<<(ostream& cout, stu1 &s)
{
	cout << s.m_a << s.m_b;
	return cout;
}
int main()
{
	stu1 s1(10,10);
	cout << s1<<endl;
	return 0;
}

重载++ 递增运算符和<< 配合使用

#include <iostream>
using namespace std;

class stu2
{
	friend ostream& operator<<(ostream& cout, stu2 s2);
public:
	stu2()
	{
		m_a = 0;
	}
	stu2& operator++()	// 前置++
	{
		m_a++;	// 先++,在返回,所以是前置
		return *this; // 放回自身
	}
	stu2 operator++(int) // 占位参数 区分是后置++
	{ 
		stu2 ss = *this;  // 创建临时对象记录当前对象
		m_a++;
		return ss; // 返回临时对象,不返回引用,因为局部对象不能返回
	}

private:
	int m_a;
};

ostream& operator<<(ostream& cout, stu2 s2)
{
	cout << s2.m_a;
	return cout;
}

int main()
{
	stu2 s2;
	cout << ++s2<<endl;	
	cout <<s2 << endl;
	cout << s2++ << endl;
	return 0;
}

= 赋值运算符重载

因为默认的赋值运算符是浅拷贝,使用析构函数delete的时候会出现报错,重载赋值,使用深拷贝

#include <iostream>
using namespace std;

class stu3
{
public:
	stu3(int age)
	{
		cout << "构造函数调用"<<endl;
		m_age = new int(age);
	}
	~stu3()
	{
		cout << "析构函数调用"<<endl;
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
	}
	
	// 重载
	stu3& operator=(stu3 &s)
	{
		// 先判断属性堆区是否为空,不为空释放干净,然后深拷贝
		if (this->m_age != NULL)
		{
			cout << "不干净"<<endl;
			delete this->m_age;
			this->m_age = NULL;
		}
		// 深拷贝:
		this->m_age = new int(*s.m_age);
		return *this;
			
	}
	int* m_age;
};

int main()
{
	stu3 s3(12);
	stu3 s33(11);
	s3.operator=(s33);
	//s3 = s33;
	cout << *s3.m_age<<endl;
	return 0;
}

关系运算符重载 ==

自定义比较方式

#include <iostream>
using namespace std;

class stu4
{
public:
	stu4(string name,int age)
	{
		m_name = name;
		m_age = age;
	}
	string m_name;
	int m_age;
	bool operator==(stu4& s4)
	{
		if (this->m_name == s4.m_name && this->m_age == s4.m_age)
		{
			return true;
		}
		else
			return false;
	}
};

int main()
{
	stu4 s4("zhu", 12);
	stu4 s44("zhu1", 14);
	if (s4.operator==(s44))
		cout << "相等" << endl;
	else
		cout << "不相等" << endl;
	return 0;
}

函数调用符重载(也称仿函数)

#include <iostream>
using namespace std;

class stu5
{
public:
	void operator()(string test)
	{
		cout << test << endl;
	}
};

int main()
{
	stu5 s5;
	s5("hello world");
	return 0;
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讳疾忌医丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值