C++——运算符重载相关知识

c++允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载或者运算符重载

程序员可以重定义或重载大部分 C++ 内置的运算符。这样,他就能使用自定义类型的运算符。

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

//运算符重载
/*= + - ++ -- << >> new delete ()
运算符重载的规定:
1.不可以新造运算符
不能扭曲 如加法实现减法的功能
*/

可重载运算符

下面是可重载的运算符列表:

双目算术运算符+ (加),-(减),*(乘),/(除),% (取模)
关系运算符==(等于),!= (不等于),< (小于),> (大于>,<=(小于等于),>=(大于等于)
逻辑运算符||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符+ (正),-(负),*(指针),&(取地址)
自增自减运算符++(自增),--(自减)
位运算符| (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符=, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放new, delete, new[ ] , delete[]
其他运算符()(函数调用),->(成员访问),,(逗号),[](下标)
//下列代码通过使用成员函数演示运算符重载的概念,在这里,对象作为参数进行传递,对象的属性使用this运算符进行访问。

#include <iostream>
using namespace std;

class compex //创建一个复数类 如3+5i
{
private:
	int _real;
	int _image;

public:
	compex(int real, int image)  //构造函数
		:_real(real), _image(image)
	{
		cout<<"构造函数"<<endl;
		_real = real;
		_image = image;
	}

	compex(const compex& src)    //拷贝构造函数
		:_real(src._real),_image(src._image)
	{
		cout<<"拷贝构造函数"<<endl;
		_real = src._real;
		_image = src._image;
	}

	compex operator=(const compex& src)//等号运算符重载
	{
		cout<<"等号运算符重载函数"<<endl;
		if(&src == this)
		{
			return *this;
		}
		_real = src._real;
		_image = src._image;
	}

	~compex() //析构函数
	{
		cout<<"析构函数"<<endl;
	}

	compex operator+(/*this,*/const compex& src)  //+运算符的重载
	{
		return compex(_real+src._real, _image+src._image);
	}

	compex operator+(/*this,*/int src)     //+
	{
		return compex(_real+src,_image);
	}

	compex operator-(/*this,*/const compex& src)  //-运算符的重载
	{
		return compex(_real-src._real, _image-src._image);
	}

	ostream& operator<<(ostream& out)   //<<运算符的重载
	{
		out<<"_real:"<<_real;
		out<<"_image:"<<_image;
		out<<endl;
		return out;
	}

	istream& operator>>(istream& in)    //>>运算符的重载
	{
		in>>_real;
		in>>_image;
		return in;
	}

	compex operator++(/*this*/) //前置++
	{
		return compex(++_real, _image);
	}

	compex operator++(int)  //后置++
	{
		return compex(_real++, _image);
	}

	compex operator--(/*this*/)       //前置--
	{
		return compex(--_real, _image);
	}

	compex operator--(int)         //后置--
	{
		return compex(_real--,_image);
	}

	friend ostream& operator<<(ostream& out,const compex& src);
	friend istream& operator>>(istream& in, compex& src);
	friend compex operator+(int src, const compex& c);
	friend compex operator-(int src, const compex& c);
};

ostream& operator<<(ostream& out,const compex& src)  //类外实现<<的重载
{
	out<<"src._real:"<<src._real;
	out<<"src._image:"<<src._image;
	out<<endl;

	return out;
}

istream& operator>>(istream& in, compex& src)  //类外实现>>的重载,不允许加const
{
	in>>src._real;
	in>>src._image;

	return in;
}

compex operator+(int src, const compex& c)  //类外实现+
{
	return compex(src+c._real,c._image);
}

compex operator-(int src, const compex& c)  //类外实现-
{
	return compex(src-c._real,c._image);
}

//测试
int main()
{
	compex c1(2,3);
	compex c2(4,5);
	compex c3 = c1 + c2;   //+
	c3.operator<<(cout);   //<<

	cout<< c3 <<endl;

	compex c4 = ++c3;      //前置++
	cout<< c4 <<endl;
	cout<< c3 <<endl;

	compex c5 = c3++;      //后置++
	cout<< c5 <<endl;

	cout<< c3+4 <<endl;    //+
	cout<< 5+c3 <<endl;    //+

	c3.operator>>(cin);    //>>
	c3.operator<<(cout);   //<<

	cin>>c3;
	cout<< c3 <<endl;

	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值