class Complex
C语言&C++语言
- C语言
Data(global)+Functions=>create_variables
全局变量+全局方法构建全局变量实现功能,没有对象概念,功能复杂情况下变量和方法区分不开 - C++语言
class,struct(Data_Members+Functions_Member)=>create_objects
变量和方法封装在class/struct构建对象,对象使用自己的变量和方法来实现功能
Notes
- Class without pointer
- 头文件防卫式声明,防止重复包含
- Complex类实部和虚部类型不确定,设置为模板
- inline(内联函数) 定义在类本体内部,是否是内联函数由编译器决定
- access level 成员函数和成员变量设置不同访问级别
- 构造函数
- 构造对象时自动调用
- 没有返回值
- 初始参数可以设置默认值
- 初始化阶段利用初始化列表初始化
- Private中不允许构造 单例模式
- 函数重载
- 编译时编译器会对函数名重新编码,其实不同
- 函数名相同,语义上不允许相同
- 操作符重载
- 成员函数隐含了一个this指针
- 全局函数,无this指针
- 常量成员函数,不会改变成员变量,const对象调用非const成员函数出现不兼容的类型限定符
- 传值和传引用
- 参数传递尽量传引用相当于传指针(底层为指针)
- 传递者无需知道接收者是以引用形式接收
- 参数返回可以的情况下,尽量传引用相当于传指针(底层为指针)
- 返回值为传入参数
- 临时变量不能传引用
- 有元函数
- 自由取得friend的private成员
- 相同class的各个Object互为有元
代码示例
https://gitee.com/NiMiKiss/learning-notes.git
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207#ifndef _CLASS_COMPLEX_ #define _CLASS_COMPLEX_ #include <iostream> #include <math.h> class Complex; Complex& __doapl(Complex* ths, const Complex& r); Complex& __doami(Complex* ths, const Complex& r); Complex& __doaml(Complex* ths, const Complex& r); Complex& __doapi(Complex* ths, const Complex& r); class Complex { public: Complex(double r = 0, double i = 0) : re(r), im(i) { } Complex& operator += (const Complex&); Complex& operator -= (const Complex&); Complex& operator *= (const Complex&); Complex& operator /= (const Complex&); double real() const { return re; } double imag() const { return im; } private: double re, im; friend Complex& __doapl(Complex*, const Complex&); friend Complex& __doami(Complex*, const Complex&); friend Complex& __doaml(Complex*, const Complex&); friend Complex& __doapi(Complex*, const Complex&); }; inline Complex& __doapl(Complex* ths, const Complex& r) { ths->re += r.re; ths->im += r.im; return *ths; } inline Complex& Complex::operator += (const Complex& r) { return __doapl(this, r); } inline Complex& __doami(Complex* ths, const Complex& r) { ths->re -= r.re; ths->im -= r.im; return *ths; } inline Complex& Complex::operator -= (const Complex& r) { return __doami(this, r); } inline Complex& __doaml(Complex* ths, const Complex& r) { ths->re = ths->re * r.re - ths->im * r.im; ths->im = ths->im * r.re + ths->re * r.im; return *ths; } inline Complex& Complex::operator *= (const Complex& r) { return __doaml(this, r); } inline Complex& __doapi(Complex* ths, const Complex& r) { ths->re = (ths->re * r.re + ths->im * r.im) / (pow(r.re, 2) + pow(r.im, 2)); ths->im = (ths->im * r.re - ths->re * r.im) / (pow(r.re, 2) + pow(r.im, 2)); return *ths; } inline Complex& Complex::operator /= (const Complex& r) { return __doapi(this, r); } inline double imag(const Complex& x) { return x.imag(); } inline double real(const Complex& x) { return x.real(); } inline Complex operator + (const Complex& x, const Complex& y) { return Complex(real(x) + real(y), imag(x) + imag(y)); } inline Complex operator + (const Complex& x, double y) { return Complex(real(x) + y, imag(x)); } inline Complex operator + (double x, const Complex& y) { return Complex(x + real(y), imag(y)); } inline Complex operator - (const Complex& x, const Complex& y) { return Complex(real(x) - real(y), imag(x) - imag(y)); } inline Complex operator - (const Complex& x, double y) { return Complex(real(x) - y, imag(x)); } inline Complex operator - (double x, const Complex& y) { return Complex(x - real(y), -imag(y)); } inline Complex operator * (const Complex& x, const Complex& y) { return Complex(real(x) * real(y) - imag(x) * imag(y), imag(x) * real(y) + real(x) * imag(y)); } inline Complex operator * (const Complex& x, double y) { return Complex(real(x) * y, imag(x) * y); } inline Complex operator * (double x, const Complex& y) { return Complex(x * real(y), x * imag(y)); } inline Complex operator / (const Complex& x, double y) { return Complex(real(x) / y, imag(x) / y); } inline Complex operator / (const Complex& x, const Complex& y) { return Complex((real(x) * real(y) + imag(x) * imag(y)) / (pow(real(y), 2) + pow(imag(y), 2)), (imag(x) * real(y) - real(x) * imag(y)) / (pow(real(y), 2) + pow(imag(y), 2))); } inline Complex operator + (const Complex& x) { return x; } inline Complex operator - (const Complex& x) { return Complex(-real(x), -imag(x)); } inline bool operator == (const Complex& x, const Complex& y) { return real(x) == real(y) && imag(x) == imag(y); } inline bool operator == (const Complex& x, double y) { return real(x) == y && imag(x) == 0; } inline bool operator == (double x, const Complex& y) { return x == real(y) && imag(y) == 0; } inline bool operator != (const Complex& x, const Complex& y) { return real(x) != real(y) || imag(x) != imag(y); } inline bool operator != (const Complex& x, double y) { return real(x) != y || imag(x) != 0; } inline bool operator != (double x, const Complex& y) { return x != real(y) || imag(y) != 0; } inline Complex polar(double r, double t) { return Complex(r * cos(t), r * sin(t)); } inline Complex conj(const Complex& x) { return Complex(real(x), -imag(x)); } inline double norm(const Complex& x) { return real(x) * real(x) + imag(x) * imag(x); } std::ostream& operator << (std::ostream& os, const Complex& x) { return os << '(' << real(x) << ',' << imag(x) << ')'; } #endif // !_CLASS_COMPLEX_
客户端调用
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "Class_Complex.h" #include <iostream> int main() { Complex c1(2, 1); Complex c2(4, 0); std::cout << c1 << std::endl; std::cout << c2 << std::endl; std::cout << c1 + c2 << std::endl; std::cout << c1 - c2 << std::endl; std::cout << c1 * c2 << std::endl; std::cout << c1 / c2 << std::endl; std::cout << conj(c1) << std::endl; std::cout << norm(c1) << std::endl; std::cout << polar(10, 4) << std::endl; std::cout << (c1 == c2) << std::endl; std::cout << (c1 != c2) << std::endl; std::cout << +c2 << std::endl; std::cout << -c2 << std::endl; std::cout << (c2 - 2) << std::endl; std::cout << (5 + c2) << std::endl; return EXIT_SUCCESS; }
最后
以上就是疯狂人生最近收集整理的关于c++ 学习笔记--class Complex(01)class Complex的全部内容,更多相关c++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复