运算符重载

运算符重载使运算符能够作用于对象。实质就是函数重载,且可以被多次重载。

成员运算符 . 成员指针运算符 .* 作用域运算符 :: 长度运算符 sizeof 以及条件运算符 ?: 不能被重载。

重载之后的运算符不能改变运算符的优先级和结合性,也不能改变运算符操作数的个数及语法结构。

运算符重载不能改变该运算符用于内部类型对象的含义。它只能和用户自定义类型的对象一起使用,或者用于用户自定义类型的对象和内部类型的对象混合使用时。

运算符重载是针对新类型数据的实际需要对原有运算符进行的适当的改造,重载的功能应当与原有功能相类似,避免没有目的地使用重载运算符。

重载运算符的函数不能有默认的参数,否则就改变了运算符的参数个数。

重载的运算符只能是用户自定义类型,用户自定义类的运算符一般都必须重载后方可使用,但两个例外,运算符“=”和“&”不必用户重载。

运算符重载可以通过成员函数的形式,也可是通过友元函数,非成员非友元的普通函数。义一个复数类,那通过对象初始化后的复数,对象间不能直接进行加减乘除的操作,必须要将运算符重载以后才能作用于对象。

一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。

以下一些双目运算符不能重载为类的友元函数:=、()、[]、->。

运算符重载有三种方式,类外定义的*运算符重载代码如下:

其中对象P1 P2完全等价,因为两个对象的运算操作实际就是调用函数。

类外定义运算符重载:
#include<iostream>
using namespace std;
class Complex {
public:
	double real;
	double imag;
	Complex(double r = 0, double i = 0)
	{
		real = r, imag = i;
	}
};
Complex operator*(Complex co1, Complex co2)
{
	Complex temp;
	temp.real = co1.real * co2.real - co1.imag * co2.imag;
	temp.imag = co1.real * co2.imag + co1.imag * co2.real;
	return temp;
}
int main()
{
	
	Complex com1(1,2), com2(3,4), P1, P2;
	P1 = operator*(com1, com2);
	cout << "real=" << P1.real << "imag=" << P1.imag << endl;
	P2 = com1 * com2;
	cout << "real=" << P2.real << "imag=" << P2.imag << endl;
	return 0;
}

第二种友元运算符的重载代码如下:

友元重载相较于类外重载的好处在于:永远重载后重载函数可以访问类的私有成员,不受限制。

友元运算符重载:
#include<iostream>
using namespace std;
class Complex {
public:
	double real;
	double imag;
	Complex(double r = 0, double i = 0)
	{
		real = r, imag = i;
	}
	void print();
	friend Complex operator*(Complex co1,Complex co2);
};
Complex operator*(Complex co1,Complex co2)
{
	Complex temp;
	temp.real = co1.real * co2.real - co1.imag * co2.imag;
	temp.imag = co1.real * co2.imag + co1.imag * co2.real;
	return temp;
}
void Complex::print()
{
	cout << "P real=" << real << "P imag=" << imag << endl;
}
int main()
{
	
	Complex com1(1,2), com2(3,4), P1, P2;
	P1 = operator*(com1,com2);
	P1.print();
	P2 = com1 * com2;
	P2.print();
	return 0;
}

第三种成员运算符重载代码如下:

注意:成员运算符区别于前两种的地方在于:成员运算符调用时要用一个对象去调用,也就是说,成员运算符函数里的变量只有一个,另一个出现在调用的部分,具体可看代码,这使得一个双目运算符在重载时函数里只有一个变量,区别于前两种。

成员运算符重载:
#include<iostream>
using namespace std;
class Complex {
public:
	double real;
	double imag;
	Complex(double r = 0, double i = 0)
	{
		real = r, imag = i;
	}
	void print();
	Complex operator*(Complex co);
};
Complex Complex::operator*(Complex co)
{
	Complex temp;
	temp.real = real * co.real - imag * co.imag;
	temp.imag = real * co.imag + imag * co.real;
	return temp;
}
void Complex::print()
{
	cout << "real=" << real << "imag=" << imag << endl;
}
int main()
{
	
	Complex com1(1,2), com2(3,4), P1, P2;
	P1 = com2.operator*(com1);
	P1.print();
	P2 = com1 * com2;
	P2.print();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值