5.2运算符重载

一、在类外定义的运算符重载函数

#include "iostream"
using namespace std;
class Complex {
public:
	double real;
	double imag;//在类外定义的运算符重载函数,需要保证成员共有

	Complex(double r, double i) {
		real = r;
		imag = i;
	}

	void print() {
		cout << "real:" << real << "imag:" << imag << endl;
	}
};

Complex operator-(Complex a, Complex b) {		//成员运算符重在函数
	return Complex(a.real - b.imag, a.imag - b.imag);
}
int main() {
	Complex c1(1.1, 2.2), c2(3.3, 4.4);

	//两者等价
	Complex c4 = c1 - c2;
	Complex c3 = operator-(c1, c2);


	c3.print();
	c4.print();

	return 0;
}

绝大多数运算符允许重载,但

成员访问运算符.

成员指针访问运算符.*

作用域运算符::

长度运算符sizeof

条件运算符?:

不可重载,且不允许自定义

允许重载:不能改变运算符的操作个数、优先级、结合特性、必须包含一个自定义类型、

二、友元运算符重载函数

1)双目运算符

#include "iostream"
using namespace std;
class Complex {
public:
	double real;
	double imag;//在类外定义的运算符重载函数,需要保证成员共有

	Complex(double r, double i) {
		real = r;
		imag = i;
	}
	friend Complex operator-(Complex a, Complex b) {		//成员运算符重在函数
		return Complex(a.real- b.imag, a.imag - b.imag);
	}
	void print() {
		cout << "real:" << real << "imag:" << imag << endl;
	}
};


int main() {
	Complex c1(1.1, 2.2), c2(3.3, 4.4);

	//两者等价
	Complex c3 = c1 - c2 ;
	Complex c4 = operator-(c1 , c2); 

	c3.print(); 
	c4.print(); 

	return 0;
}

2)单目运算符

1.   -

#include "iostream"
using namespace std;
class Complex {
public:
	double real;
	double imag;//在类外定义的运算符重载函数,需要保证成员共有

	Complex(double r, double i) {
		real = r;
		imag = i;
	}
	friend Complex operator-(Complex a) {		//成员运算符重在函数
		return Complex(-a.real, -a.imag );
	}
	void print() {
		cout << "real:" << real << "imag:" << imag << endl;
	}
};


int main() {
	Complex c1(1.1, 2.2), c2(3.3, 4.4);

	//两者等价
	Complex c3 = -c1  ;
	Complex c4 = operator-(c1); 


	c3.print(); 
	c4.print(); 

	return 0;
}

2.  + +     - -

#include "iostream"
using namespace std;
class Complex {
public:
	double real;
	double imag;//在类外定义的运算符重载函数,需要保证成员共有

	Complex(double r, double i) {
		real = r;
		imag = i;
	}
	friend Complex operator++(Complex &a) {	//chichu不引用则形参不变
		//成员运算符重在函数
		a.real++; a.imag++; 
		return Complex(a.real, a.imag);//这里的a.real++和++a.real不一样
	}
	void print() {
		cout << "real:" << real << "imag:" << imag << endl;
	}
};


int main() {
	Complex c1(1.1, 2.2);

	//两者等价
	Complex c3 = ++c1 ;
	c1.print();
	c3.print();

	return 0;
}

三、成员运算符重载函数( =  [ ]()不能通过友元运算符重载函数实现,这时候可以用成员运算符重载函数实现)

#include "iostream"
using namespace std;
class Complex {
private:
	double real;
	double imag;
public:
	
	Complex(double r, double i) {
		real = r;
		imag = i;
	}
	Complex operator- (Complex a) {		//成员运算符重在函数
		return Complex(real - a.real, imag - a.imag);
	}
	
	void print() {
		cout << "real:" << real << "imag:" << imag << endl;
	}
};
int main() {
	Complex c1(1.1, 2.2), c2(3.3, 4.4);

	//两者等价
	Complex c3 = c1 - c2;
	Complex c4 = c1.operator-(c2);


	c3.print();
	c4.print();
	return 0;
}

这里面隐含了一个操作数,就是调用该函数的对象

因此   成员运算符 重载函数   无论是双目还是单目运算符,都要比友元的方式少一个运算符

1.****双目运算符在使用时,如果第一个操作数是系统内置类型(eg: int) 必须用友元的方式*************一般情况下最好使用成员运算符重载

2.用成员运算符重载函数实现++--

 

#include "iostream"
using namespace std;
class Three {
private:
	int i1, i2, i3;
public:
	Three (int i1,int i2,int i3):i1(i1),i2(i2),i3(i3){}
	void print() {
		cout << "i1:"<< i1;
		cout << "	i2:" << i2;
		cout << "	i3:" << i3<<endl;
	}
	Three operator++() {
		i1++; i2++; i3++;
		return *this;
	}
	Three operator++(int) {
		Three temp(*this);
		++i1; ++i2; ++i3;
		return temp;
	}
};


int main() {
	Three obj1(4, 5, 6) ;
	obj1.print();
	(obj1++).print(); 
	//(obj1++).print();
	//等价于(obj1.operator++(0)).print(); 
	obj1.print();

	cout << endl;

	Three obj2(4,5,6);
	obj2.print();
	(++obj2).print();
	//(++obj2).print();
	//等价于	obj1.operator++();
	obj2.print();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值