c++面向对象编程——运算符重载

c++面向对象编程——>>>运算符重载

1.什么是运算符重载

#include <iostream>
using namespace std;
class Complex{
private:
	double r;
	double i;
public:
	Complex(double r, double i){
		this->r = r;
		this->i = i;
	}
	void print(void){
		cout << r << " + " << i << "i" << endl;
	}
};
int main(void){
	Complex a(1,2);
	Complex b(3,4);
	a.print();
	b.print();
	int x = 10;
	int y = 20;
	int z = x + y;
//	a+b; //想像x+y 实现Complex对象加运算 +运算符新的逻辑功能 要实现+运算符的重载
	return 0;
}

2.双目运算符重载

**双目运算符:**有左右两个操作数的操作符 L#R

  • 算术运算:*、/、%、+、-

  • 关系运算:>、>=、<、<=、==、!=

  • 逻辑运算:&&、||

  • 位运算:&、|、^、<< 、>>

  • 赋值与复合赋值:=、+=、-=、*=、/=、%=、&=、|=、^=、<<= 、>>=

  • … …

注意:

  • 表达式结果是右值

  • 左右操作数既可以是左值也可以是右值

实现方式

  • 成员函数形式:L.operator#®

  • 友元函数形式:operator#(L,R)

#include <iostream>
using namespace std;
class Complex{
private:
	double r;
	double i;
public:
	Complex(double r, double i){
		this->r = r;
		this->i = i;
	}
	void print(void){
		cout << r << " + " << i << "i" << endl;
	}
	const Complex operator+(const Complex& c)
	{
		Complex tmp(r+c.r, i+c.i);
		return tmp;
	}
	friend const Complex operator-(const Complex& l, const Complex& r);
};

const Complex operator-(const Complex& l, const Complex& r)
{
	Complex tmp(l.r - r.r, l.i - r.i);
	return tmp;
}

int main(void){
	Complex a(1,2);
	Complex b(3,4);
	a.print();
	b.print();

	Complex c = a + b;
	c.print();

	Complex d = c - b;// operator-(c, a);
	d.print();
	return 0;
}

赋值类双目运算符重载时需要注意的事项

  • 表达式的结果是左值,就是左操作数的自身

  • 左操作数必须是左值,右操作数可以是左值也可以是右值

Complex & operator+=(const Complex &c){
	r = r + c.r;
	i = i + c.i;
	return *this;
}
*******************************************
Complex & operator-=(Complex &L, const Complex &R){
	L.r -= R.r;
	L.i -= R.i;
	return L;
}

3.单目运算符重载

单目运算符: 只有一个操作数的运算符 #O

  • 相反数:-

  • 位反:~

  • 逻辑非:!

  • 自增:++

  • 自减:–

  • … …

3.1 计算类单目运算符

注意:

  • 表达式结果是右值

  • 操作数可以是左值也可以是右值

实现方式:

  • 成员函数形式:O.operator#();

  • 友元函数形式:operator#(O);

#include <iostream>
using namespace std;
class Integer{
private:
	int i ;
public:
	Integer(int m=0):i(m){}
	void print(void){
		cout << i << endl;
	}
	const Integer operator-(){
	return Integer(-i);
	}
};
int main(void){
	Integer a(10);
	Integer b = -a;
	b.print();
	return 0;
}

3.2 前缀自增减单目运算符

注意

  • 表达式结果是左值, 而且是自身

  • 操作数必须是左值

实现方式:

  • 成员函数形式:O.operator#();

  • 友元函数形式:operator#(O);

#include <iostream>
using namespace std;
class Integer{
private:
	int i ;
public:
	Integer(int m=0):i(m){}
	void print(void){
		cout << i << endl;
	}
	const Integer operator-(){
	return Integer(-i);
	}

	Integer& operator++()
	{
		++i;
		return *this;
	}
	friend Integer& operator--(Integer &o)
	{
		--o.i;
		return o;
	}
};
int main(void){
	Integer a(10);
	Integer b = -a;
	b.print();

	(++a).print();
	(--a).print();
	return 0;
}

3.3 后缀自增减单目运算符

注意:

  • 表达式结果是右值,是操作数自增减前的副本

  • 操作数必须是左值

实现方式:

  • 成员函数形式:O.operator#(哑元);

  • 友元函数形式:operator#(O,哑元);

#include <iostream>
using namespace std;
class Integer{
private:
	int i ;
public:
	Integer(int m=0):i(m){}
	void print(void){
		cout << i << endl;
	}
	const Integer operator-(){
	return Integer(-i);
	}
	/*前++*/
	Integer& operator++()
	{
		++i;
		return *this;
	}
	/*前--*/
	friend Integer& operator--(Integer &o)
	{
		--o.i;
		return o;
	}
	/*后++*/
	const Integer operator++(int)
	{
		Integer old = *this;
		++(*this);//++(this->i)
		return old;
	}
	/*后--*/
	friend const Integer operator--(Integer& o, int)
	{
		Integer old = o;
		--o; // --o.i
		return old;
	}
};
int main(void){
	Integer a(10);
	Integer b = -a;
	b.print();

	(++a).print();
	(--a).print();

	Integer c = a++;
	a.print();
	c.print();

	Integer d = a--;
	a.print();
	d.print();
	return 0;
}

实例:

Integer类以友元形式重载逻辑非运算符!

#include <iostream>
using namespace std;
class Integer{
private:
	int i ;
public:
	Integer(int m=0):i(m){}
	void print(void){
		cout << i << endl;
	}
	friend const Integer operator!(Integer& o){
		if(o.i <= 0)
		{
			return true;
		}
		return false;
	}
};
int main(void){
	Integer a(-1);
	Integer b = !a;
	b.print();
	return 0;
}

4.其他运算符重载

4.1 输入输出运算符重载

friend ostream& operato<<(ostream& os,const RIGHT& right){...}
	ostream, 标准库的类
	cout << a; //operator<<(cout,a)
friend istream& operato>>(istream& is,RIGHT& right){...}
	istream cin;//istream是标准库的类
	cin >> a;//operator>>(cin,a)

注:因为无法向标准库类添加成员函数,所以只能使用全局函数的形式

#include <iostream>
using namespace std;
class Complex{
	private:
		double r;
		double i;
	public:
		Complex(double r, double i){
			this->r = r;
			this->i = i;
		}
		const Complex operator+(const Complex& c){
			Complex tmp(r+c.r, i+c.i);
			return tmp;
		}
		Complex & operator+=(const Complex &c){
			r = r + c.r;
			i = i + c.i;
			return *this;
		}
		friend ostream& operator<<(ostream &os, const Complex& c){
			os << c.r << " + " << c.i << "i" << endl;
			return os; // cout << a << b << c << endl;
		}
		friend istream& operator>>(istream &is, Complex& c){
			is >> c.r >> c.i ;
			return is;
		}
		friend const Complex operator-(const Complex& l , const Complex& r);
		friend Complex & operator-=(Complex &L, const Complex &R);
};
const Complex operator-(const Complex& l, const Complex& r){
	Complex tmp(l.r - r.r, l.i - r.i);
	return tmp;
}
Complex & operator-=(Complex &L, const Complex &R){
	L.r -= R.r;
	L.i -= R.i;
	return L;
}
int main(void){
	Complex c1(2,3);
	cout << c1;
	Complex c2(0,0);
	cin >> c2;
	cout << c2;
	return 0;
}

4.2 new和delete运算符重载

通过new创建该类的对象时,将首先调用该操作符函数分配内存(可重载),然后再调用该类的构造函数

通过delete销毁该类的对象时,将首先调用该类的析构函数,然后再调用该操作符函数释放内存(可重载)

#include <iostream>
#include <cstdlib>
using namespace std;
class A{
public:
	A(void){
		cout << "A construct" << endl;
	}
	~A(void){
		cout << "~A disconstruct" << endl;
	}
	void * operator new(size_t size){
		cout << "A new" << endl;
		return malloc(size);
	}
	void operator delete(void *p){
		cout << "A delete" << endl;
		free(p);
	}
};
int main(void){
	A *p = new A;
	delete p;
	return 0;
}

5.编程综合实例

实现一个3*3的矩阵类,支持如下操作符:

  • 运算类双目操作符:+ - *

  • 赋值类双目操作符:+= -= *=

  • 单目操作符:-(相反数)

  • 输出操作符:<<


注意:不是所有的操作符都能重载,下面操作符不能重载

::	 .	 ?:	 sizeof	 typeid
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值