16~C++运算符重载

  • 操作符函数 operator+ operator-
    说明:操作符函数用来解决不同类型或者相同类型之间的加减法问题

示例1、 相同数据类型之间的加减法

#include <iostream>
class Stack
{
public:
	Stack(int a = 0, int b = 0):m_a(a),m_b(b){}
	/*成员函数实现+操作符*/
	Stack const operator+ (Stack const& that) const
	{
		return Stack(m_a+that.m_a, m_b+that.m_b);
	}
int m_a;
int m_b;
};
/*非成员函数实现-操作符*/
Stack const operator- (Stack const& lfs, Stack const& rhs)
{
	return Stack(lfs.m_a - rhs.m_a, lfs.m_b - rhs.m_b);
}

int main(void)
{
	using namespace std;
	
	Stack st1(10,10);
	Stack st2(9,9);
	
	/*+: st1.operator=(st2)*/
	cout << (st1+st2).m_a << " " << (st1+st2).m_b << endl;
	
	/*-: operator-(st1,st2)*/
	cout << (st1 - st2).m_a << " " << (st1-st2).m_b << endl;
}

示例2、不同数据类型之间的加减法

#include <iostream>
class Stack
{
public:
	Stack(int a = 0, int b = 0):m_a(a),m_b(b){}
	/*成员函数实现:Stack类型 + Stack类型,操作符函数*/
	Stack const operator+ (Stack const& that) const
	{
		return Stack(m_a+that.m_a, m_b+that.m_b);
	}
	/*成员函数实现:Stack类型 + int 类型,操作符函数*/
	int operator+(int const& that) const
	{
		return that+m_a;
	}

int m_a;
int m_b;
};
/*非成员函数实现:Stack类型 - Stack类型,操作符函数*/
Stack const operator- (Stack const& lfs, Stack const& rhs)
{
	return Stack(lfs.m_a - rhs.m_a, lfs.m_b - rhs.m_b);
}
/*非成员函数实现:Stack类型 - int类型,操作符函数*/
int operator-(Stack const& lfs, int const& rhs)
{
	return lfs.m_b - rhs; 
}
int main(void)
{
	using namespace std;
	
	Stack st1(10,10);
	Stack st2(9,9);
	cout << st1 + 1 << endl; //11
	cout << st2 - 1 << endl; //8
} 
  • 习题
    分析以下示例代码函数调用过程
#include <iostream>
using namespace std;

class Stack
{
public:
	/*构造函数*/
	Stack(int a = 0, int b = 0)
	{
		m_a = a;
		m_b = b;
		cout << "int int -> Stack" << endl;
	}

	/*成员函数实现:Stack类型 + Stack类型,操作符函数*/
	Stack operator+ (Stack const& that) const
	{
		cout << "Stack + Stack" << endl;
		return Stack(m_a+that.m_a, m_b+that.m_b);
	}

	/*成员函数实现:Stack类型 + int类型,操作符函数*/
	int operator+(int const& that) const
	{
		cout << "Stack + int" << endl;
		return that+m_a;
	}
	/*赋值函数*/
	Stack& operator= (Stack const& that)
	{
		cout << "=" << endl;
		m_a = that.m_a;
		m_b = that.m_b;
		return * this;
	}

	/*拷贝构造函数*/
	Stack (Stack const& that)
	{
		cout << "cp" << endl;
		m_a = that.m_a;
		m_b = that.m_b;
	}

	void print()
	{
		cout << "m_a = " << m_a << " m_b = " << m_b << endl;
	}
int m_a;
int m_b;
};
/*非成员函数实现:Stack类型 - Stack类型,操作符函数*/
Stack const operator- (Stack const& lfs, Stack const& rhs)
{
	cout << "Stack - Stack" << endl;
	return Stack(lfs.m_a - rhs.m_a, lfs.m_b - rhs.m_b);
}

/*非成员函数实现:Stack类型 - int类型,操作符函数*/
int operator-(Stack const& lfs, int const& rhs)
{
	cout << "Stack - int" << endl;
	return lfs.m_b - rhs; 
}

int main(void)
{

	/*构造函数*/
	Stack st1(10,10);
	/*构造函数*/
	Stack st2(9,9);
	/*拷贝构造函数*/
	Stack st3 = st1;
	
	/*Stack::operator+(Stack const& that) 注意:这则语句并不调用拷贝构造函数(即使这则语句有很大的诱导性),因为+运算符的返回值直接赋值给st4*/
	Stack st4(st1 + st2);
	/*同上*/
	Stack st5 = st1 + st2;
	cout << "--------------" << endl;
	/*01 operator- (Stack, Stack) --> 02 Stack operator=(Stack const& that)*/
	st4 = st1 - st2;
	
	/*01  int Stack::operator+(int const& that) -> 02 Stack(int, int)*/
	st4 = st1 + 1;
	/*01 int operator-(Stack const& lfs, Stack const& rhs) -> 02 Stack(int,int)*/
	st4 = st2 -1;
} 
``
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值