运算符重载实例

心得:

  1. 必须要掌握好运算符重载,充分利用重载运算符的特点,有意识的去运用到程序中去优化程序。
  2. 运算符重载><等这类比较运算符时,要注意所重载的运算符所包含的内容不能相矛盾。
  3. 重载某个运算符时,具体代码在花括号内实现,所以不要固有思维,要灵活运用。

运算符重载的实例:

1.模拟秒表:(重载自增运算符)

class Time
{
public:
		Time( ){minute=0;sec=0;}
		Time(int m,int s):minute(m),sec(s){ }
		Time operator++( );     //声明前置自增运算符“++”重载函数
		Time operator++(int);   //声明后置自增运算符“++”重载函数 
private:
		int minute;
		int sec;
};
Time Time∷operator++( )    //定义前置自增运算符“++”重载函数
{
	if(++sec>=60)	
        {	
		sec-=60;         //满60秒进1分钟
		++minute;
	}
	return *this;          //返回当前对象值
}
Time Time∷operator++(int)    //定义后置自增运算符“++”重载函数
{
	Time temp(*this);
	sec++;
	if(sec>=60)	
        {
		sec-=60;
		++minute;
	}
	return temp;         //返回的是自加前的对象
}

注意:为区分前置和后置自增运算符,后置的++--需要增加一个形参。

设 A  Aobject ;
运算符 ++和 - - 有两种方式:
前置方式: ++Aobject    --Aobject
  成员函数  重载	A :: A operator++ () ; 	
    解释为:	Aobject . operator ++( ) ;
  友元函数  重载	friend A operator++ (A &) ; 
    解释为: 	operator ++( Aobject ) ;		
后置方式:  Aobject ++	 Aobject --
  成员函数  重载	A :: A  operator++ (int) ;   //注意形参;
	解释为: Aobject . operator ++( 0 ) ;   //0为伪参数;
  友元函数  重载:friend A operator++ (A &, int) ; 	
	解释为: operator++(Aobject, 0) 

2.复数运算(用友元运算符函数重载)

include<iostream>
using namespace std;
class Complex
{ public:
      Complex( double r =0, double i =0 ) { Real = r ;   Image = i ; }   
      Complex(int a) { Real = a ;  Image = 0 ; } 
      void print() const ;
   friend Complex operator+ ( const Complex & c1, const Complex & c2 ) ;    //学习在类内声明方式;
   friend Complex operator- ( const Complex & c1, const Complex & c2 ) ;
   friend Complex operator- ( const Complex & c ) ;
  private:  
      double  Real, Image ;
};
Complex operator + ( const Complex & c1, const Complex & c2 )
  { double r = c1.Real + c2.Real ;  double i = c1.Image+c2.Image ;     //注意运算的表达方式;
     return Complex ( r,  i ) ;
  }
Complex operator - ( const Complex & c1, const Complex & c2 )
  { double r = c1.Real - c2.Real ;  double i = c1.Image - c2.Image ;
     return Complex ( r,  i ) ;
  }
Complex operator- ( const Complex & c )
  { return Complex ( -c.Real, - c.Image ) ; }
void Complex :: print() const
  { cout << '(' << Real << " , " << Image << ')' << endl ; }

注意:

  • friend 在类内声明时写,在类外定义时不带friend关键字。
  • 参数都要放到形参表内

3.举例:定义Name类的重载赋值函数       重点实例

#include<iostream>
#include<cstring>
using namespace std;
class  Name
{ public :
     Name ( char  *pN ) ;
     Name( const Name & ) ;		    //复制构造函数
     Name& operator=( const Name& ) ;      // 重载赋值运算符,这两点都很重要,必须掌握。
     ~ Name() ;
  protected : 
     char  *pName ;
     int size ;
} ;
Name::Name ( char  *pN )
 { cout <<" Constructing " << pN << endl ;
    pName = new char[ strlen( pN ) + 1 ] ;      //学会这种表达方式;
    if( pName != 0 ) strcpy( pName,pN ) ;
    size = strlen( pN ) ;
 }
Name::Name( const Name & Obj )	 //复制构造函数
{ cout << " Copying " << Obj.pName << " into its own block\n";
   pName = new char[strlen( Obj.pName ) + 1 ] ;
   if ( pName != 0 ) strcpy( pName, Obj.pName ) ;
   size = Obj.size;
}
Name & Name::operator= ( const Name & Obj )	 // 重载赋值运算符
{ delete  []pName ;
   pName = new char[ strlen( Obj.pName ) + 1 ] ;
   if ( pName != 0 ) strcpy( pName , Obj.pName ) ;
   size = Obj.size ;
   return *this ;
}
Name::~ Name()
{ cout << " Destructing " << pName << endl ;
   delete  []pName ;
   size = 0;
}
int main()
{ Name Obj1( "ZhangSan" ) ;
   Name Obj2 = Obj1 ;		// 调用复制构造函数 
   Name Obj3( "NoName" ) ;
   Obj3 = Obj2 = Obj1 ;		// 调用重载赋值运算符函数 
}                           // 注意:修改对象时调用重载赋值运算符函数;    

编译结果:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值