C++运算符号重载

重载运算符
重载运算符用“operator 运算符”来实现

1.重载=运算符。
    (1)赋值与初始化并不一样。
    int a = 3;这个是创建变量并初始化
    a = b 这个是赋值
    创建对象并初始化的时候,才会调用赋值构造函数。
    有时候仅仅是默认的赋值,也会产生各自问题,比如指针指向同一块地址。

    (2)operator =
    
        这里的调用,相当于this=right
    
    (3)重载运算符相当于重新定义了运算符的功能,根据需要来合理设置,否则会有不同的效果。
        比如,你可以把=重载为一种输出显示,这也是可以的。
    (4)重载运算符的另一个问题是,不能运算符的操作数,比如=是二元运算符,++和--是一元运算符
        大多数运算符号都可以重载。比如>,<,<=,&,/,[],new,delete。。。。。
        无法重载的运算符有  ?:  .   .*   ::   sizeof

2.重载运算符的方法
    (1)使重载运算符成为该类的成员函数
    (2)作为友元函数,比如<<和>>

#include <iostream>

using namespace std;
class Length{
	private:
		int len_inches;
	public:
		Length(int feet,int inches){
			setLength(feet,inches);
		}
		Length(int inches){ len_inches = inches;}
		int getFeet() const{ return len_inches/12;}
		int getInches() const{ return len_inches%12;}
		void setLength(int feet,int inches){
			len_inches = 12*feet + inches;
		}
		/**reload**/
		friend Length operator+(Length a,Length b);
		friend Length operator-(Length a,Length b);
		friend bool operator<(Length a,Length b);
		friend bool operator==(Length a,Length b);
		Length operator++();//前缀++
		Length operator++(int);//后缀++ 
		
};
//对于成员函数实现运算符重载,利用的是this指针,但是对于友元函数的重载,就会用到两个对象
Length operator+(Length a,Length b){
	return Length(a.len_inches+b.len_inches);
} 

Length operator-(Length a,Length b){
	return Length(a.len_inches - b.len_inches);
} 

bool operator<(Length a,Length b){
	return a.len_inches < b.len_inches;
} 

bool operator==(Length a,Length b){
	return a.len_inches == b.len_inches;
} 
//前缀++ 
Length Length::operator++(){
	this->len_inches ++;
	return *this;
}
//后缀++
/*1.要有虚拟形参*/
/*2.用零食变量存放自加之前的状态,进行自加后返回之前的状态*/
Length Length::operator++(int){
	Length temp = *this;
	this->len_inches ++;
	return temp;	
}

int main(int argc, char** argv) {
	Length one(6),two(10);
	Length three(0);
	three = one + two;
	cout<<three.getFeet()<<" "<<three.getInches()<<endl;	
	
	
	return 0;
}

3.在独立函数和成员函数运算符之间选择
    class Length{
        private:
            int len;
        public:
            Length operator+(length b)//第一种
            friend operator+(Length a,length b)//第二张
    };
    
    
    作为成员函数来重载与作为独立函数来重载有所区别
    Length a,b,c;
    作为成员函数来重载 a = b + c; 解释为 a = b.operator+(c)
    作为独立函数来重载 a = b + c; 解释为 a = operator(b,c)
    但是如果 a = 2 + b来讲
    作为成员函数来重载显然是行不通的。
    成员函数重载,会有隐式的类型转换。

4.重载流插入和提取运算符
    (1).对于流插入运算符,就是将对象的值转化为文本输出到cout文件对象
    函数声名为 ostream& operator<<(ostream& out,Length a)
    输入流对象应该以引用对方返回
    对于流输出也一样。

#include <iostream>

using namespace std;
class Length{
	private:
		int len_inches;
	public:
		Length(int feet,int inches){
			setLength(feet,inches);
		}
		Length(int inches){ len_inches = inches;}
		int getFeet() const{ return len_inches/12;}
		int getInches() const{ return len_inches%12;}
		void setLength(int feet,int inches){
			len_inches = 12*feet + inches;
		}
		/**reload**/
		friend Length operator+(Length& a,Length& b);
		friend Length operator-(Length a,Length b);
		friend bool operator<(Length a,Length b);
		friend bool operator==(Length a,Length b);
		Length operator++();//前缀++
		Length operator++(int);//后缀++ 
		friend ostream& operator<<(ostream& out,Length a);//重载输出流
		friend istream& operator>>(istream& in,Length& a) ;
		 
		
};
//对于成员函数实现运算符重载,利用的是this指针,但是对于友元函数的重载,就会用到两个对象
Length operator+(Length& a,Length& b){
	return Length(a.len_inches+b.len_inches);
} 

Length operator-(Length a,Length b){
	return Length(a.len_inches - b.len_inches);
} 

bool operator<(Length a,Length b){
	return a.len_inches < b.len_inches;
} 

bool operator==(Length a,Length b){
	return a.len_inches == b.len_inches;
} 
//前缀++ 
Length Length::operator++(){
	this->len_inches ++;
	return *this;
}
//后缀++
/*1.要有虚拟形参*/
/*2.用零食变量存放自加之前的状态,进行自加后返回之前的状态*/
Length Length::operator++(int){
	Length temp = *this;
	this->len_inches ++;
	return temp;	
}
/*重载输出流*/
ostream& operator<<(ostream& out,Length a){
	out<<a.getFeet()<<" feet,"<<a.getInches()<<" inches";
	
	return out;
} 

istream& operator>>(istream& in,Length& a){
	int feet,inches;
	in>>feet;
	in>>inches;
	a.setLength(feet,inches);
	return in;
}
int main(int argc, char** argv) {
	Length one(6),two(10);
	Length three(0);
	three = one + two;
	cout<<three.getFeet()<<" "<<three.getInches()<<endl;	
	cout<<one + two;//要在重载运算符种相加,需要按值传递 
	cin>>three;
	cout<<three;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值