C++ 运算符重载

运算符重载概述

简单的说运算符重载只有两种方式

  1. 在成员函数中重载
class 类
{
	返回类型 operator重载运算符(参数1);
}
//调用
void test()
{
	类  类名;
	返回类型 类型名 = 类名 重载运算符 参数1 ;	
}
  1. 在全局函数中重载
返回类型 operator重载运算符(参数1,参数2);
//调用
void test()
{
	返回类型 类型名 = 参数1 重载运算符 参数2 ;	
}
  1. 递增运算符与函数调用运算符重载比较特殊
  2. 运算符重载需要掌握的是每一类的运算符的重载应用

加号运算符重载

#include<iostream>
using namespace std;
//加号运算符重载 
class Add
{
	public :
	Add operator+(int a) //一.定义加号运算符重载 
	{
		Add temp ;//创建一个对象 
		temp.a=a+this->a;//进行加法操作。也可以进行减法操作,但重载的目的就是使代码清晰易读,何必呢? 
		return temp;//返回temp 
	}
	
	Add operator+(Add& ad)//二.定义加号运算符重载 
	{
		Add temp ;
		temp.a= ad.a+this->a; 
		return temp;
	}
	public :
		int a;
}; 
Add operator+(int a,Add b) //三.定义加号运算符重载 
{
	Add temp;
	temp.a=a+b.a;
	return temp;
}
int main()
{
	Add a1;
	a1.a=10;
	
	Add a2;
	a2.a=20;
	
	Add a3=a1+20;//使用第一个运算符重载 函数的返回值相当于 a3=temp。 
	cout<<a3.a<<endl;
	
	a3=a1+a2;//使用第二个运算符重载  
	cout<<a3.a<<endl; 
	
	a3=a1+20;//使用的三个运算符重载 
	cout<<a3.a<<endl;
}

左移运算符重载

#include<iostream>
using namespace std;
//左移运算符重载
//cout的返回值是ostream,cout<<相当于cout.operator(); 
//ostream在类中很少重新建立这个对象 
class P
{
	friend ostream& operator<<(ostream& out,P& p) ;//定义全局函数为这个类的友元,就是说可以任意对这个类参数操作。 
	public:
		P()//初始化a,b的值。原谅我的懒惰。 
		{
			a=10;
			b=10;
		}
	
	private:
		int a;
		int b;
} ;
//为了实现cout<<P的操作让代码易读性增加。只能定义全局函数,并且ostream为第一个参数 
ostream& operator<<(ostream& out,P& p)
{
	out<<"a="<<p.a<<" b="<<p.b;//打印a,b; 
	return out;//返回这个对象,编程语言的链式思想,不理解可以把返回类型改为void 
}
int main() 
{
	P p;
	cout<<p<<endl;//打印p,并且打印换行; 
}

递增运算符重载

用于实现对自定义类型的输出

  1. 成员函数实现
#include<iostream>
using namespace std;
//递增运算符重载。
class P
{
	friend ostream& operator<<(ostream& out,const P& p);
	public :
		P(int a) : a(a){}//对a进行初始化 
		
		P& operator++()//前置++ 返回类型是P&是为了可以执行++(++p)类似的操作 
		{
			a++;
			return *this;
		}
		
		P operator++(int)//后置++,这是和编译器约定好的,只能写int,写代码不就是能让编译器看懂吗。 
		{
			P temp=*this;
			a++;
			return temp;//为了实现cout<<p++操作;
		}
	private :
		int a;	
};
ostream& operator<<(ostream& out,const P& p)
{
	cout<<p.a;
	return out;
}
int main() 
{
	P p(10);
	cout<<p<<endl;
	++p;
	cout<<p<<endl;
	++(++p);
	cout<<p++<<endl;
	cout<<p<<endl;
}
  1. 全局函数实现
#include<iostream>
using namespace std;
//递增运算符重载。
class P
{
	friend ostream& operator<<(ostream& out,const P& p);//加const是个好习惯,这样可以直接传值。(可以去掉试试)
	friend P& operator++(P& p);
	friend P operator++(P& p,int);
	public :
		P(int a) : a(a){}//对a进行初始化 
	private :
		int a;	
};
ostream& operator<<(ostream& out,const P& p)
{
	cout<<p.a;
	return out;
}
P& operator++(P& p)//前置++ 返回类型是P&是为了可以执行++(++p)类似的操作 
{
	p.a++;
	return p;
}

P operator++(P& p,int)//后置++,这是和编译器约定好的,只能写int,写代码不就是能让编译器看懂吗。 
{
	P temp=p;
	p.a++;
	return temp;//为了可以cout<<p 
}
int main() 
{
	P p(10);
	cout<<p<<endl;
	++p;
	cout<<p<<endl;
	++(++p);
	cout<<p++<<endl;
	cout<<p<<endl;
}

赋值运算符重载

用于深拷贝。

#include<iostream>
using namespace std;
//赋值运算符重载 
//比较懒,全局实现就不实现了。
class P
{
	friend ostream& operator<<(ostream& out,P& p); 
	public:
		P()
		{
			a=NULL; 
		}
		P(int a)
		{
			this->a=new int(a);
		}
		~P()
		{
			if(a!=NULL)
			{
				delete a;
				a=NULL;
			}
		}
		P& operator=(P& p)
		{
			a=new int(*p.a);//重新获取一块内存 
			return *this;
		}
	private:
		int *a; 
};
ostream& operator<<(ostream& out,P& p)
{
	cout<<*p.a;
	return out;
}
int main()
{
	P p(10);
	cout<<p<<endl;
	P p1;
	p1=p;
	cout<<p1<<endl;
}

关系运算符重载

#include<iostream>
using namespace std;
//关系运算符重载 
//比较懒,全局实现就不实现了,
class P
{
	friend ostream& operator<<(ostream& out,P& p);
	public:
		P(int a) : a(a) {
		}
		bool operator>(P& p) //返回值类型是bool 
		{
			return this->a>p.a;
		}
	private:
		int a; 
};
ostream& operator<<(ostream& out,P& p)
{
	cout<<p.a;
	return out;
}
int main()
{
	P p(10);
	P p1(11);
	if(p>p1){
		cout<<"p>p1";
	}else{
		cout<<"p<p1";
	}
}

函数调用运算符重载–伪函数

#include<iostream> 
using namespace std;
//函数调用运算符重载--伪函数
class P
{
	public :
		int operator()(int a,int b,int c)
		{
			return a+b+c;
		}
} ;
int main()
{
	P p;
	cout<<p(2,2,2)<<endl;//普通调用,是不是很像函数 
	cout<<P()(2,3,3)<<endl;//建立P的对象并调用 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值