【C++】运算符重载——复数类

复数CComplex类

class CComplex
{
public:
	CComplex(int real, int image)
		:mreal(real), mimage(image)
	{}
	
private:
	int mreal;//实部
	int mimage;//虚部
};

int main()
{
	CComplex c1(10, 20);
	CComplex c2 = c1 + 10;
	CComplex c3 = 10 + c1;
	CComplex c4 = c1 + c2;

	if (c1 > c3)
	{
		std::cout << c1 << std::endl;
	}
	if (c2 == c3)
	{
		std::cout << c2 << std::endl;
	}

	std::cin >> c1;
	std::cout << c1 << std::endl;
	return 0;
}

在这个代码中,不能执行的有

  • CComplex c2 = c1 + 10;对象+常量
  • CComplex c3 = 10 + c1;常量+对象
  • CComplex c4 = c1 + c2;对象+对象
  • (c1 > c3)类与类比较
  • (c2 == c3)等于运算符
  • std::cout << c1 << std::endl;类的输出流运算符
  • std::cin >> c1;类的输入流运算符

运算符重载

加号运算符重载

1、对象+常量
这是双目运算符,左操作数是类类型,可以用this指针接收,所以可以在类中实现。返回值以类类型方式返回,有临时对象的生成

	const CComplex operator+(int val)
	{
		return CComplex(mreal + val, mimage);//显示生成临时对象
	}

2、常量+对象
this指针接收的是左操作数,并且要指向对象,但是现在左操作数是一个常量,this指针无法接收,所以在类中无法处理。只能在类外处理。
写在类外,就不是thiscall调用约定了,而是_cdecl 调用约定,没有this指针。

//CComplex + int
//内置类型生成的临时量  常量
const CComplex operator+(int lhs, const CComplex& rhs)//_cdecl  没有this  
{
	return CComplex(lhs + rhs.mreal, rhs.mimage);
}

但是要在类的私有中把整个函数设为友元,不然在内外无法使用私有下的变量。

friend const CComplex operator+(int, const CComplex&);

3、对象+对象

	const CComplex operator+(const CComplex& rhs)
	{
		return CComplex(mreal + rhs.mreal,mimage + rhs.mimage);
	}

比较类运算符重载

返回的都是逻辑值,所以用bool类型

	bool  operator > (const CComplex& c2)//复数不能比较大小,当没有虚部时才可以比较大小。(这里认为可以)
	{
		return (mreal > c2.mreal || ((mreal == c2.mreal) && (mimage > c2.mimage)));
	}

==等于运算符重载

	bool  operator == (const CComplex& c2)
	{
		if (mreal == c2.mreal && mimage == c2.mimage)return true;
		return false;
	}

输出流运算符重载

可以理解为双目运算符,左操作数为输出缓冲区,右操作数为类类型。因为类类型在右侧,所以只能在类外实现

std::ostream& operator<<(std::ostream& out,const CComplex& rhs)
{
	out << rhs.mreal << "-";
	out << rhs.mimage;
	return out;
}
  • 返回值用&的原因是输出流会和输出缓冲区绑定,并且系统只会分配一个输出缓冲区,如果没有&,就会生成一个临时对象,会有两个缓冲区。与系统相悖。
  • 在类中设友元
friend std::ostream& operator<<(std::ostream& , const CComplex& );

输入流运算符重载

同理,在类外实现。

std::istream& operator >>(std::istream& in, CComplex& rhs)
{
	in >> rhs.mreal;
	in >> rhs.mimage;
	return in;
}
  • 因为输入流会改变右操作数的值,所以右操作数的处理不能加const。
  • 在类中设友元
friend std::istream& operator >>(std::istream&, CComplex&);

总结

1、运算符重载自定义类型满足和内置类型相同的逻辑
2、单目运算符

  • 类中
    this接收操作数,形参不用处理
  • 类外
    设置一个形参来接收 唯一操作数

3、双目运算符

  • 类中
    this接收左操作数,形参设置一个接收右操作数
  • 类外
    设置两个参数,第一个形参接收左操作,第二个形参接收右操作数

整体代码

#include<iostream>

class CComplex
{
public:
	CComplex(int real, int image)
		:mreal(real), mimage(image)
	{}
	const CComplex operator+(int val)
	{
		return CComplex(mreal + val, mimage);
	}
	const CComplex operator+(const CComplex& rhs)
	{
		return CComplex(mreal + rhs.mreal,
			mimage + rhs.mimage);
	}
	bool  operator > (const CComplex& c2)//复数不能比较大小,当没有虚部时才可以比较大小。(这里认为可以)
	{
		return (mreal > c2.mreal || ((mreal == c2.mreal) && (mimage > c2.mimage)));
	}
	bool  operator == (const CComplex& c2)
	{
		if (mreal == c2.mreal && mimage == c2.mimage)return true;
		return false;
	}
private:
	int mreal;//实部
	int mimage;//虚部

	friend const CComplex operator+(int, const CComplex&);
	friend std::ostream& operator<<(std::ostream& , const CComplex& );
	friend std::istream& operator >>(std::istream&, CComplex&);
};
//CComplex + int
//内置类型生成的临时量  常量
const CComplex operator+(int lhs, const CComplex& rhs)//_cdecl  没有this  
{
	return CComplex(lhs + rhs.mreal, rhs.mimage);
}

std::ostream& operator<<(std::ostream& out,const CComplex& rhs)
{
	out << rhs.mreal << "-";
	out << rhs.mimage;
	return out;
}
std::istream& operator >>(std::istream& in, CComplex& rhs)
{
	in >> rhs.mreal;
	in >> rhs.mimage;
	return in;
}
int main()
{
	CComplex c1(10, 20);
	CComplex c2 = c1 + 10;
	CComplex c3 = 10 + c1;
	CComplex c4 = c1 + c2;

	if (c1 > c3)
	{
		std::cout << c1 << std::endl;
	}
	if (c2 == c3)
	{
		std::cout << c2 << std::endl;
	}

	std::cin >> c1;
	std::cout << c1 << std::endl;
	return 0;
}

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值