類模板實現複數運算的基本操作

2021-06-08 08:13:34 字數 3078 閱讀 9470

本例主要對類模板(第一次用,呵呵!), 操作符的過載, 友元(真心不好用,慎用,雖然形式簡單,但會給你帶來不少的麻煩)等知識的複習。除錯該程式花了不少時間,也讓我學了很多東西,也深知很多東西雖然知道,但是去實踐了你才感覺這些東西你是否深刻理解。學東西還是」深拷貝」的好!該程式做完花了3個多小時,其中有絕大多數時間用於除錯,期間遇到的錯誤有vc版本本身的問題,也有各種其他的問題。本程式未參照任何複數相關的任何程式,難免有疏忽和不恰當的地方,請指正!

**如下:

在complex.h主要實現類的定義及友元的實現, 因為成員函式比較短所以在類中一併定義了。

#ifndef complex_h_

#define complex_h_

#include #include template class complex

complex(t real, t img)

complex getcomplexconjugate()

t getreal()

t getimg()

/* overloading the arithmetic operators */

friend complex operator+ (const complex & ccomplexfirst, const complex & ccomplexsecond);

friend complex operator- (const complex & ccomplexfirst, const complex & ccomplexsecond);

friend complex operator* (const complex & ccomplexfirst, const complex & ccomplexsecond);

friend complex operator/ (complex & ccomplexfirst, complex & ccomplexsecond);

/****************************************/

/* overloading the i/o operators */

friend std::ostream& operator<< (std::ostream &os, const complex & ccomplexresult); // for output

friend std::istream& operator>> (std::istream &is, complex & ccomplexinput); // for input

/* overloading the comparison operators */

friend bool operator >= (const complex & ccomplexfirst, const complex & ccomplexsecond);

friend bool operator <= (const complex & ccomplexfirst, const complex & ccomplexsecond);

friend bool operator == (const complex & ccomplexfirst, const complex & ccomplexsecond);

};#endif

template complexoperator+ (const complex& ccomplexfirst, const complex& ccomplexsecond)

template complexoperator- (const complex& ccomplexfirst, const complex& ccomplexsecond)

template complexoperator* (const complex& ccomplexfirst, const complex& ccomplexsecond)

template complexoperator/ (complex& ccomplexfirst, complex& ccomplexsecond)

else if (ccomplexfirst.m_real == 0 && ccomplexsecond.m_img == 0)

return complex(0, 0);

else

}templatestd::ostream& operator<< (std::ostream & os, const complex& ccomplexresult)

templatestd::istream& operator>> (std::istream &is, complex& ccomplexinput)

template bool operator >= (const complex& ccomplexfirst, const complex& ccomplexsecond)

else if (ccomplexfirst.m_img == 0 && ccomplexsecond.m_img == 0)

else }

template bool operator <= (const complex& ccomplexfirst, const complex& ccomplexsecond)

template bool operator == (const complex& ccomplexfirst, const complex& ccomplexsecond)

在testmain.cpp中主要進行測試工作!

**如下:

#include "complex.h"

#include int main()

else

std::cout << "divde reult: " << ccomplexresult / ccomplextemp; // divide

if (ccomplexfirst >= ccomplexsecond)

else

return 0;

}

測試結果:

MyArray類模板實現

define crt secure no warnings include include using namespace std template class myarray myarray const myarray arr myarray t operator int index myarra...

模板實現雙向鍊錶基本操作

1 template為小寫 2 模板程式設計用的不熟練,尤其是表現在模板符號格式的應用上 3 如果引數為物件,用引用格式,並考慮是否為常引用 ifndef test hpp define test hpp templateclass dllnode dllnode const t i,dllnode...

C 類模板 實現運算子過載

include using namespace std template class t classaa t value a t geta 定義友元函式 實現運算子 的過載 template class t1 friend a operator a a,t1 value 非友元函式 a operat...