侯捷老师C++大系之C++面向对象开发:(一)不带指针的类:Complex复数类的实现过程

时间:2020-12-19 19:49:23

 

一、笔记
1.C++编程简介

侯捷老师C++大系之C++面向对象开发:(一)不带指针的类:Complex复数类的实现过程

侯捷老师C++大系之C++面向对象开发:(一)不带指针的类:Complex复数类的实现过程

侯捷老师C++大系之C++面向对象开发:(一)不带指针的类:Complex复数类的实现过程

2.头文件与类的声明

防卫式声明
#ifndef __COMPLEX__
#define __COMPLEX__

……

#endif
头文件的布局
模板简介
template<typename T>
3.构造函数

inline函数:函数若在class body内定义完成,便自动成为inline候选人

访问级别:
public private
被外部访问的函数设为public

构造函数
complex (doble r=0,double i=0)
:re(r),im(i)
{ }
先初始化,后赋值
这种写法效率高

构造函数可以有很多个——重载

单例模式,构造函数放在private里,不允许外部创

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.操作符重载与临时对象

操作符重载之一,成员函数
操作符重载之二,非成员函数
临时对象

重载<<

二、代码
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 }