c++学习笔记 小白入门重载运算符,

运算符重载:对已有的运算符重新定义,赋予其扩展的功能,来适合不同的数据类型。

语法:和定义函数相同,函数名字是operator#这里的#就是被重载的运算符。

注意:

1,弄懂运算符运算对象的个数->重载函数的参数。

2,识别运算符左边的运算对象,是类的对象还是其他。

类的对象:全局函数实现(不推荐)     成员函数实现(推荐,少一个参数)

:成员函数 就是 用 this 指针 少实例化一个对象 ,例如 operator+ (a+b)

可以用this指针,在a 下使用成员函数

operator (b)

{

this -> 成员数据 + b->成员数据;

}
优点:1,不用设置友元2,少调用一个参数。

其他:只能是全局函数实现。(使用全局函数必须将全局函数设置成友元)。

如果重载运算符计算的不再是类的成员时候,这时只能用全局函数。


<<重载 全局函数。

#include<iostream>
#include<string >
using namespace  std;
class Person
{
	friend void  operator<<(ostream & out ,Person &ob);
	private:
		int num;
		string name;
	    float score;
 	public:
 		Person();//无参构造
		 Person(int num ,string name,float score):num(num),name(name),score(score){ }
		  
};
void  operator<<(ostream & out ,Person &ob)
{
	out<<ob.num<<" "<<ob.name<<" "<<ob.score;
}
int main()
{
	Person finley (100," finley",99.8f);
	cout<<finley;//operator(cout,finley)
	return 0;
}


>>重载运算符

#include<iostream>
#include<string >
using namespace  std;
class Person
{
	friend void  operator<<(ostream & out ,Person &ob);
	friend void  operator>>(istream &in ,Person &ob );
	private:
		int num;
		string name;
	    float score;
 	public:
 		Person(){
		 }
		 Person(int num ,string name,float score):num(num),name(name),score(score){ }
		 
		  
};
void  operator<<(ostream & out ,Person &ob)
{
	out<<ob.num<<" "<<ob.name<<" "<<ob.score;
}
//全局重载输入运算符
void  operator>>(istream &in ,Person &ob )
{
	cin>>ob.num>>ob.name>>ob.score;
 } 
int main()
{
 	Person finley;
	cin>>finley;
	cout<<finley;
//	Person finley (100," finley",99.8f);
//	cout<<finley;//operator(cout,finley)
	return 0;
}

 


+法重载符

#include<iostream>
#include<string >
using namespace  std;
class Person
{
	friend void  operator<<(ostream & out ,Person ob);
	friend void  operator>>(istream &in ,Person &ob );
	friend Person    operator+( Person &ob1, Person &ob2);
	private:
		int num;
		string name;
	    float score;
 	public:
 		Person(){
		 }
		 Person(int num ,string name,float score):num(num),name(name),score(score){ }
		 
		  
};
void  operator<<(ostream & out ,Person ob)
{
	out<<ob.num<<" "<<ob.name<<" "<<ob.score<<endl;
}
//全局重载输入运算符
void  operator>>(istream &in ,Person &ob )
{
	cin>>ob.num>>ob.name>>ob.score;
 } 
 Person   operator+( Person &ob1, Person &ob2)
 {
 	Person tmp;
 	tmp.num=ob1.num+ob2.num;
 	tmp.name=ob1.name+ob2.name;
 	tmp.score=ob1.score+ob2.score;
 	return tmp;
 }
int main()
{
 	Person finley,jory;
	cin>>finley;
	cin>>jory;
	cout<<finley;
	cout<<endl;
	cout<<jory;
	cout<<endl;
	cout<<finley+jory;
//	Person finley (100," finley",99.8f);
//	cout<<finley;//operator(cout,finley)
	return 0;
}


==重载(成员函数 引用)

	 bool operator==( Person &ob)
		 {
		 	if(num==ob.num && name==ob.name && score==ob.score )
		 	{
		 		return true;
			 }
			 else
			 return false;
		  } 

 

 重载++ —运算符:

这两个符号比较特殊,特殊在于++a,和a++功能不一样,那在重载的过程中,需要函数重载。这时需要用到站位参数。

++a-> 调用operator++(a)

a++ ->调用operator++(a,int)

--相同,语法需要。

部分代码,便于理解,成员函数实现,请勿直接复制。

前置++

 Person  operator++( int )
		  {
		  	//保存旧值
			  Person old=*this;
			  this->num++;
			  this->name=this->name+this->name;
			  this->score++;
			  return old; 
		  }
int main()
{
 	Person finley,jory;
	cin>>finley;
//	cin>>jory;
	cout<<finley;
	cout<<endl;
//	cout<<jory;
	Person lundry;
	lundry=finley++;
	cout<<lundry;
	cout<<endl;
	cout<<finley;
	/*
	if( finley==jory)
	{
		cout<<"相等";
	}
	else
	{
		cout<<" 不相等";
	}*/
//	cout<<endl;
//	cout<<finley+jory;
//	Person finley (100," finley",99.8f);
//	cout<<finley;//operator(cout,finley)
	return 0;
}

 后置++

  Person  operator++( )
		  {
		  	
			  this->num++;
			  this->name=this->name+this->name;
			  this->score++;
			  return *this; 
		  }
int main()
{
 	Person finley,jory;
	cin>>finley;
//	cin>>jory;
	cout<<finley;
	cout<<endl;
//	cout<<jory;
	Person lundry;
//	lundry=finley++;
	lundry =++finley;
	cout<<lundry;
	cout<<endl;
	cout<<finley;
	/*
	if( finley==jory)
	{
		cout<<"相等";
	}
	else
	{
		cout<<" 不相等";
	}*/
//	cout<<endl;
//	cout<<finley+jory;
//	Person finley (100," finley",99.8f);
//	cout<<finley;//operator(cout,finley)
	return 0;
}

 

下面是我的完整代码:

#include<iostream>
#include<string >
using namespace  std;
class Person
{
	friend void  operator<<(ostream & out ,Person ob);
	friend void  operator>>(istream &in ,Person &ob );
	friend Person    operator+( Person &ob1, Person &ob2);
	private:
		int num;
		string name;
	    float score;
 	public:
 		Person(){
		 }
		 Person(int num ,string name,float score):num(num),name(name),score(score){ }
		 //成员函数相等运算;
		 bool operator==( Person &ob)
		 {
		 	if(num==ob.num && name==ob.name && score==ob.score )
		 	{
		 		return true;
			 }
			 else
			 return false;
		  } 
		  Person  operator++( int )
		  {
		  	//保存旧值
			  Person old=*this;
			  this->num++;
			  this->name=this->name+this->name;
			  this->score++;
			  return old; 
		  }
		   Person  operator++( )
		  {
		  	
			  this->num++;
			  this->name=this->name+this->name;
			  this->score++;
			  return *this; 
		  }
		 
		  
};
void  operator<<(ostream & out ,Person ob)
{
	out<<ob.num<<" "<<ob.name<<" "<<ob.score<<endl;
}
//全局重载输入运算符
void  operator>>(istream &in ,Person &ob )
{
	cin>>ob.num>>ob.name>>ob.score;
 } 
 Person   operator+( Person &ob1, Person &ob2)
 {
 	Person tmp;
 	tmp.num=ob1.num+ob2.num;
 	tmp.name=ob1.name+ob2.name;
 	tmp.score=ob1.score+ob2.score;
 	return tmp;
 }
int main()
{
 	Person finley,jory;
	cin>>finley;
//	cin>>jory;
	cout<<finley;
	cout<<endl;
//	cout<<jory;
	Person lundry;
//	lundry=finley++;
	lundry =++finley;
	cout<<lundry;
	cout<<endl;
	cout<<finley;
	/*
	if( finley==jory)
	{
		cout<<"相等";
	}
	else
	{
		cout<<" 不相等";
	}*/
//	cout<<endl;
//	cout<<finley+jory;
//	Person finley (100," finley",99.8f);
//	cout<<finley;//operator(cout,finley)
	return 0;
}

 注意:尽量不要去重载两个运算符& & 和||  无法结绝他们的短路特性;

不能重载的运算符:.  :: .*   ?:   sizeof  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值