侯捷老師C++大系之C++面向對象開發:(一)不帶指針的類:Complex複數類的實現過程

 

1、筆記
1.C++編程簡介ios

2.頭文件與類的聲明編程

防衛式聲明
#ifndef __COMPLEX__
#define __COMPLEX__函數

……佈局

#endif
頭文件的佈局
模板簡介
template<typename T>
3.構造函數this

inline函數:函數若在class body內定義完成,便自動成爲inline候選人spa

訪問級別:
public private
被外部訪問的函數設爲publiccode

構造函數
complex (doble r=0,double i=0)
:re(r),im(i)
{ }
先初始化,後賦值
這種寫法效率高orm

構造函數能夠有不少個——重載對象

單例模式,構造函數放在private裏,不容許外部創blog

4.參數傳遞與返回值

在函數後頭加const
double real () const{return re;}

參數傳遞: pass by value vs. pass by reference(to const)
參數儘可能傳引用,若是不但願對方改加const
內化成習慣
返回值傳遞:return by value vs. return by reference(to const)
返回值也儘可能傳引用

friend(友元)
自由取得friend的private成員
相同class的各個objects互爲friends(友元)

class body外的各類定義

什麼狀況能夠傳和返回引用,什麼狀況不能夠:
兩個參數相加,獲得一個新的結果時不能傳引用

5.操做符重載與臨時對象

操做符重載之一,成員函數
操做符重載之二,非成員函數
臨時對象

重載<<

2、代碼
1.complex.h

 1 #ifndef __MYCOMPLEX__  2 #define __MYCOMPLEX__
 3 
 4 class complex;  5 complex&
 6   __doapl (complex* ths, const complex& r);  7 complex&
 8   __doami (complex* ths, const complex& r);  9 complex&
 10   __doaml (complex* ths, const complex& r);  11 
 12 
 13 class complex  14 {  15 public:  16   complex (double r = 0, double i = 0): re (r), im (i) { }  17   complex& operator += (const complex&);  18   complex& operator -= (const complex&);  19   complex& operator *= (const complex&);  20   complex& operator /= (const complex&);  21   double real () const { return re; }  22   double imag () const { return im; }  23 private:  24   double re, im;  25 
 26   friend complex& __doapl (complex *, const complex&);  27   friend complex& __doami (complex *, const complex&);  28   friend complex& __doaml (complex *, const complex&);  29 };  30 
 31 
 32 inline complex&
 33 __doapl (complex* ths, const complex& r)  34 {  35   ths->re += r.re;  36   ths->im += r.im;  37   return *ths;  38 }  39  
 40 inline complex&
 41 complex::operator += (const complex& r)  42 {  43   return __doapl (this, r);  44 }  45 
 46 inline complex&
 47 __doami (complex* ths, const complex& r)  48 {  49   ths->re -= r.re;  50   ths->im -= r.im;  51   return *ths;  52 }  53  
 54 inline complex&
 55 complex::operator -= (const complex& r)  56 {  57   return __doami (this, r);  58 }  59  
 60 inline complex&
 61 __doaml (complex* ths, const complex& r)  62 {  63   double f = ths->re * r.re - ths->im * r.im;  64   ths->im = ths->re * r.im + ths->im * r.re;  65   ths->re = f;  66   return *ths;  67 }  68 
 69 inline complex&
 70 complex::operator *= (const complex& r)  71 {  72   return __doaml (this, r);  73 }  74  
 75 inline double
 76 imag (const complex& x)  77 {  78   return x.imag ();  79 }  80 
 81 inline double
 82 real (const complex& x)  83 {  84   return x.real ();  85 }  86 
 87 inline complex  88 operator + (const complex& x, const complex& y)  89 {  90   return complex (real (x) + real (y), imag (x) + imag (y));  91 }  92 
 93 inline complex  94 operator + (const complex& x, double y)  95 {  96   return complex (real (x) + y, imag (x));  97 }  98 
 99 inline complex 100 operator + (double x, const complex& y) 101 { 102   return complex (x + real (y), imag (y)); 103 } 104 
105 inline complex 106 operator - (const complex& x, const complex& y) 107 { 108   return complex (real (x) - real (y), imag (x) - imag (y)); 109 } 110 
111 inline complex 112 operator - (const complex& x, double y) 113 { 114   return complex (real (x) - y, imag (x)); 115 } 116 
117 inline complex 118 operator - (double x, const complex& y) 119 { 120   return complex (x - real (y), - imag (y)); 121 } 122 
123 inline complex 124 operator * (const complex& x, const complex& y) 125 { 126   return complex (real (x) * real (y) - imag (x) * imag (y), 127                real (x) * imag (y) + imag (x) * real (y)); 128 } 129 
130 inline complex 131 operator * (const complex& x, double y) 132 { 133   return complex (real (x) * y, imag (x) * y); 134 } 135 
136 inline complex 137 operator * (double x, const complex& y) 138 { 139   return complex (x * real (y), x * imag (y)); 140 } 141 
142 complex 143 operator / (const complex& x, double y) 144 { 145   return complex (real (x) / y, imag (x) / y); 146 } 147 
148 inline complex 149 operator + (const complex& x) 150 { 151   return x; 152 } 153 
154 inline complex 155 operator - (const complex& x) 156 { 157   return complex (-real (x), -imag (x)); 158 } 159 
160 inline bool
161 operator == (const complex& x, const complex& y) 162 { 163   return real (x) == real (y) && imag (x) == imag (y); 164 } 165 
166 inline bool
167 operator == (const complex& x, double y) 168 { 169   return real (x) == y && imag (x) == 0; 170 } 171 
172 inline bool
173 operator == (double x, const complex& y) 174 { 175   return x == real (y) && imag (y) == 0; 176 } 177 
178 inline bool
179 operator != (const complex& x, const complex& y) 180 { 181   return real (x) != real (y) || imag (x) != imag (y); 182 } 183 
184 inline bool
185 operator != (const complex& x, double y) 186 { 187   return real (x) != y || imag (x) != 0; 188 } 189 
190 inline bool
191 operator != (double x, const complex& y) 192 { 193   return x != real (y) || imag (y) != 0; 194 } 195 
196 #include <cmath>
197 
198 inline complex 199 polar (double r, double t) 200 { 201   return complex (r * cos (t), r * sin (t)); 202 } 203 
204 inline complex 205 conj (const complex& x) 206 { 207   return complex (real (x), -imag (x)); 208 } 209 
210 inline double
211 norm (const complex& x) 212 { 213   return real (x) * real (x) + imag (x) * imag (x); 214 } 215 
216 #endif   //__MYCOMPLEX__

 

2.complex_test.cpp

 1 #include <iostream>
 2 #include "complex.h"
 3 
 4 using namespace std;  5 
 6 ostream&
 7 operator << (ostream& os, const complex& x)  8 {  9   return os << '(' << real (x) << ',' << imag (x) << ')'; 10 } 11 
12 int main() 13 { 14   complex c1(2, 1); 15   complex c2(4, 0); 16 
17   cout << c1 << endl; 18   cout << c2 << endl; 19   
20   cout << c1+c2 << endl; 21   cout << c1-c2 << endl; 22   cout << c1*c2 << endl; 23   cout << c1 / 2 << endl; 24   
25   cout << conj(c1) << endl; 26   cout << norm(c1) << endl; 27   cout << polar(10,4) << endl; 28   
29   cout << (c1 += c2) << endl; 30   
31   cout << (c1 == c2) << endl; 32   cout << (c1 != c2) << endl; 33   cout << +c2 << endl; 34   cout << -c2 << endl; 35   
36   cout << (c2 - 2) << endl; 37   cout << (5 + c2) << endl; 38   
39   return 0; 40 }
相關文章
相關標籤/搜索