侯捷《c++物件導向開發》——動手實現自己的複數類
#ifndef __complex__
#define __complex__
...#endif
一般情況下,建構函式是要被外界呼叫的,因此不能放在private中,但是有一種設計模式叫做singleton,這個模式將夠著函式放在private中,只允許獲得乙個類例項
class a ;
a& a::getinstance()
不改變資料的內建函式,一定要加const
相同的class的各個objects互為friends(友元),可以直接取得資料
class complex
int func(const complex& param)
private:
double re,im;
};...
tricks
資料在private
傳入資料和返回值reference
類的本體中的這些函式應該要加const的就要加
盡量用建構函式
臨時物件
下面這些函式一定不能以reference作為返回值,因為是全域性函式,而且是生成乙個新的物件,函式結束後物件刪除,若以reference傳遞會找不到這個物件。所以只能以value傳遞,慢點就慢點吧~
inline complex
operator + (const complex& x,const complex& y)
complex& operator +=(
const complex&);
complex& operator -=(
const complex&);
complex& operator *=(
const complex&);
complex& operator /=(
const complex&);
double real (
)const
double imag (
)const
private:
double re, im;
friend complex& __doapl (complex *
,const complex&);
friend complex& __doami (complex *
,const complex&);
friend complex& __doaml (complex *
,const complex&);
friend ostream&
operator <<
(ostream& os,
const complex& x);}
;inline complex&
__doapl (complex* ths,
const complex& r)
inline complex&
complex:
:operator +=(
const complex& r)
inline complex&
__doami (complex* ths,
const complex& r)
inline complex&
complex:
:operator -=(
const complex& r)
inline complex&
__doaml (complex* ths,
const complex& r)
inline complex&
complex:
:operator *=(
const complex& r)
inline
double
imag (
const complex& x)
inline
double
real (
const complex& x)
inline complex
operator +
(const complex& x,
const complex& y)
inline complex
operator +
(const complex& x,
double y)
inline complex
operator +
(double x,
const complex& y)
inline complex
operator -
(const complex& x,
const complex& y)
inline complex
operator -
(const complex& x,
double y)
inline complex
operator -
(double x,
const complex& y)
inline complex
operator *
(const complex& x,
const complex& y)
inline complex
operator *
(const complex& x,
double y)
inline complex
operator *
(double x,
const complex& y)
complex
operator /
(const complex& x,
double y)
inline complex //reference傳遞
operator +
(const complex& x)
inline complex
operator -
(const complex& x)
inline bool
operator ==
(const complex& x,
const complex& y)
inline bool
operator ==
(const complex& x,
double y)
inline bool
operator ==
(double x,
const complex& y)
inline bool
operator !=
(const complex& x,
const complex& y)
inline bool
operator !=
(const complex& x,
double y)
inline bool
operator !=
(double x,
const complex& y)
#include
inline complex
polar (
double r,
double t)
inline complex
conj (
const complex& x)
inline
double
norm (
const complex& x)
inline ostream&
operator <<
(ostream& os,
const complex& x)
#endif
//__mycomplex__
main.cpp
#include
#include
"complex.h"
using namespace std;
intmain()
C 物件導向高階程式設計 侯捷
1 防禦式宣告 ifndef complex define complex endif 作用 保證只include一次 2 inline function 在body內進行定義 更快,但最終能否inline由編譯器決定 3 為什麼應該用初始化列表 complex double r 0,double ...
侯捷 C 物件導向高階開發(上)筆記整理
c 物件導向高階開發 上 一 c 程式設計簡介 1 基於物件 只有乙個class的程式設計 object based 物件導向 幾個class的程式設計 object oriented 2 class的經典分類 3 class之間的關係 繼承inheritance 復合composition 委託d...
侯捷c 物件導向(上)
一 c 程式設計簡介 1 基於物件 只有乙個class的程式設計 object based 物件導向 幾個class的程式設計 object oriented 2 class的經典分類 class without pointer members e.g complex 複數 class with p...