(原創) 如何使用Operator Overloading? (C/C++)

Abstract
Operator Overloading讓我們可以自己定義Operator的功能,讓程式可以更精簡,C#也有,不過不是很強調,但C++非常強調Operator Overloading,這是C++的一大特色。

Introduction

以下程式我們試著實做一個『複數』型別,複數由實部,有虛部,複數的加法為(a + bi) + (c +di) = (a +c) + (b+d)i,乘法則是(a + bi) * (c+di) = (ac - bd) + (ad + bc) i。

C++

ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
InBlock.gif(C) OOMusou 2006 
http://oomusou.cnblogs.com
InBlock.gif
InBlock.gifFilename    : OperatorOverloading.cpp
InBlock.gifCompiler    : Visual C++ 8.0 / ISO C++
InBlock.gifDescription : Demo how to use use Operator Overloading
InBlock.gifRelease     : 10/16/2007 2.0
ExpandedBlockEnd.gif
*/

None.gif#include 
< iostream >
None.gif
None.gif
using   namespace  std;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
class  Complex  dot.gif {
InBlock.gif
public:
ExpandedSubBlockStart.gifContractedSubBlock.gif  Complex(
double re = 0.0double im = 0.0) : re(re), im(im) dot.gif{}
InBlock.gif
public:
InBlock.gif  Complex 
operator+(const Complex&); // use member function
InBlock.gif
  friend Complex operator*(const Complex&const Complex&); // use global function
InBlock.gif
  friend ostream& operator<<(ostream&const Complex&); // use global function
InBlock.gif

InBlock.gif
private:
InBlock.gif  
double re; // real part
InBlock.gif
  double im; // imagenary part
ExpandedBlockEnd.gif
}
;
None.gif
ExpandedBlockStart.gifContractedBlock.gifComplex Complex::
operator + ( const  Complex &  complex)  dot.gif {
InBlock.gif  Complex _complex;
InBlock.gif  _complex.re 
= this->re + complex.re;
InBlock.gif  _complex.im 
= this->im + complex.im;
InBlock.gif
InBlock.gif  
return _complex;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifComplex 
operator * ( const  Complex &  complex1,  const  Complex &  complex2)  dot.gif {
InBlock.gif  Complex _complex;
InBlock.gif  _complex.re 
= complex1.re * complex2.re - complex1.im * complex2.im;
InBlock.gif  _complex.im 
= complex1.re * complex2.im + complex1.im * complex2.re;
InBlock.gif
InBlock.gif  
return _complex;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifostream
&   operator << (ostream &   out const  Complex &  complex)  dot.gif {
InBlock.gif  
out << "real:" << complex.re << " imagenary:" << complex.im;
InBlock.gif
InBlock.gif  
return out;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
int  main()  dot.gif {
InBlock.gif  Complex complex1(
2,2);
InBlock.gif  Complex complex2(
3,3);
InBlock.gif  Complex complex3 
= complex1 + complex2;
InBlock.gif  Complex complex4 
= complex1 * complex2;
InBlock.gif
InBlock.gif  cout 
<< complex3 << endl;
InBlock.gif  cout 
<< complex4 << endl;
ExpandedBlockEnd.gif}


執行結果

None.gif real: 5  imagenary: 5
None.gifreal:
0  imagenary: 12


第17行使用member function的方式overload + operator,18行使用global function的方式overload * operator,這兩種寫法都可以,惟若使用global function,由於要存取data menber,所以要宣告該function為friend,這樣才能存取data member。

19行我們overload了<< operator,由於也是global function,所以也要宣告friend。

最後49行和55行的user code,直接用+和*就可以計算複數,而且cout也直接支援Complex物件,非常清楚,這就是operator overloading的威力,不過,在class implementation時,operator overloading的語法不是很好寫,雖然語法很有邏輯很有道理,但就是太臭太長了,還真的得時間熟悉才行。

See Also
(原創) 如何對複數進行運算 ? (C/C++)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值