Skip to content

Commit 4ae761a

Browse files
committed
Updated to Chapter 9, Section 1
1 parent d80eead commit 4ae761a

File tree

8 files changed

+101
-60
lines changed

8 files changed

+101
-60
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
55
### 此项目第一阶段计划(泛讲篇前七章)已经完成!
66

7-
当前进度:泛讲篇第七章完成。参见[Preview](https://github.com/cppHusky/cppHusky-cpp-Tutorial/releases/tag/preview)
7+
当前进度:泛讲篇第八章完成。参见[Preview](https://github.com/cppHusky/cppHusky-cpp-Tutorial/releases/tag/preview)
88

99
我为此规划了一个大致的编章结构,参见[Structure.md](https://github.com/cppHusky/cppHusky-cpp-Tutorial/blob/main/Structure.md)
1010

1111
本书中的每一份正式的示例代码都已打包并存至本项目中!详见[Code in book](https://github.com/cppHusky/cppHusky-cpp-Tutorial/tree/main/code_in_book)
1212

13-
正在疯狂赶进度,还要补充插图和代码。现阶段争取保持每4天更新一章的速度。
13+
正在疯狂赶进度,还要补充插图和代码,以及布署甲辰年礼解谜。现阶段争取保持每4天更新一章的速度。
1414

1515
如果发现内容有任何疏漏和错误,欢迎来作补充和提出修改意见!可以直接提交到[Issues](https://github.com/cppHusky/cppHusky-cpp-Tutorial/issues)当中。
1616

17-
>自24年1月6日起,共编缉此项目时长为 :[![wakatime](https://wakatime.com/badge/user/018cddbf-c102-44d2-a0f3-463bcf2eef39/project/018cddc4-4445-4bc3-8a9d-c6c933978a2b.svg)](https://wakatime.com/badge/user/018cddbf-c102-44d2-a0f3-463bcf2eef39/project/018cddc4-4445-4bc3-8a9d-c6c933978a2b)# 2024-puzzle-for-anniversary-gift
17+
>自24年1月6日起,共编缉此项目时长为 :[![wakatime](https://wakatime.com/badge/user/018cddbf-c102-44d2-a0f3-463bcf2eef39/project/018cddc4-4445-4bc3-8a9d-c6c933978a2b.svg)](https://wakatime.com/badge/user/018cddbf-c102-44d2-a0f3-463bcf2eef39/project/018cddc4-4445-4bc3-8a9d-c6c933978a2b)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
\section{概念介绍}
2+
以下三种关系是C++中常见的,读者务必搞清楚它们之间的区别,切勿混淆。\par
3+
\subsection*{整体与部分(Has-a relationship)}
4+
还记得我们在第六章讲述结构体时如何说吗?
5+
\begin{lstlisting}
6+
int cuboid[3]; //定义一个3长度数组,用来表示一个长方体
7+
\end{lstlisting}
8+
通过这种方式,我们定义了一个数组用以表示长方体,但它终究是三个孤立的值(只是存放在一起方便访问),而不是一个``整体''——比如说,函数的返回值只能是单个数据,而不能是数组。这就好比,我们已经有了发动机、轮胎、蓄电池、保险杠等等一应俱全的零件,但就是没有一辆完整的汽车。\par
9+
所以我们应该把这样一盘散沙的零件封装成一个完整的对象,这样也就有了``整体与部分''的概念。\par
10+
一辆汽车,它的发动机就是它的一部分;一个人,他的大脑和心脏就是他的一部分;一个动物园,它的各个展区——狮子区、老虎区等,都是它的一部分;一个国家,它的各个城市,都是它的一部分。\par
11+
以上所述,都是\textbf{``整体与部分''的关系,用英文可以阐述为``Has-a relationship'',在C++中可以诠释为``对象与成员''}。如果我们定义一个 \lstinline@valarri@ 类的 \lstinline@a@,那么 \lstinline@a@ 就是对象,\lstinline@a._size@, \lstinline@a._cap@ 和 \lstinline@a._arr@ 都是成员。这些成员如果单独存在,它表达的含义就不明显了;但是倘若它们存放在一起,那么我们就可以根据它们各自的值,以及它们之间的相互关系,从而表示更丰富、更明确的含义。\par
12+
我们在第八章曾讲过,一个类的成员同时也是另一个类的对象——这说明整体与部分的关系是有传递性的。作为汽车一部分的发动机,它也是由气缸、活塞、燃油泵等部分构成的。就这样,我们可以把一个对象拆解成若干成员,又把这些作为成员的对象拆解成更小单位的成员,一直拆解到 \lstinline@int@, \lstinline@double@ 等基本类型及它们的指针、数组类型。\par
13+
\subsection*{模版与实例}
14+
我们曾在第四章简单地讲解过泛型编程的概念。我们说,泛型编程并不直接定义具体的函数,而是定义了一个模版(Template)。
15+
\begin{lstlisting}
16+
template<typename T>
17+
T user::max(const T& lhs, const T& rhs) {
18+
return lhs > rhs ? lhs : rhs;
19+
}
20+
\end{lstlisting}
21+
如果我们在代码中需要调用它来进行两个 \lstinline@double@ 数据求最大值,那么编译器就会照此模版生成一个 \lstinline@user::max(const double&,const double)@ 函数,我们把它称为这个模版的实例(Instance)。一个模版可能有千奇百怪的实例,用以处理各种不同的数据,但它们总有很多的相似之处。\par
22+
现在我们把``模版与实例''这个概念扩展到更广义的层面上去。模版就是一个抽象的概念,它不对应着什么实体;而实例正是在这个模版概念之下产生的实体。``人类''就是这样的一个模版,它只是一个抽象概念,不对应任何实体;而``张三''``李四''这样的实体就是这个模版的实例,他们可能有不同的属性,比如身高、体重,但总有一些独特的、能与其它物种分开的属性,比如``读写能力''之类的。\par
23+
这种\textbf{``模版与实例''的关系,用英文可以阐述为``Instance-of relationship'',在C++中可以诠释为``类与对象''}。对于我们定义的 \lstinline@valarri a@ 来说,\lstinline@valarri@ 就是类,而 \lstinline@a@ 就是对象。\par
24+
初学者很容易搞混``Instance-of relationship''``Has-a relationship'',但只要记住一点:``''是一个抽象的概念,不对应实体;而``对象''``成员''都是具体的事物,它们对应着实体\footnote{这不是金科玉律!但是对于日常生活中九成以上的情况来说都足够了。}。也正因为``''``对象''不是同一层次的概念,所以``模版与实例''的关系是没有传递性的。\par
25+
\subsection*{属与种}
26+
在讲述这个关系前我想请读者思考一个问题:我家的哈士奇和李四家的萨摩耶是同类吗?\par
27+
是吗?好像不是。不是吗?好像也是。\par
28+
之所以存在这样的困惑,是因为我们没说清``同类''是什么概念。对于我家的哈士奇来说,它属于``哈士奇类(Husky)'';而对于李四家的萨摩耶来说,它属于``萨摩耶类(Samoyed)''——那么它们当然不是同类了。\par
29+
但是我们还有另一种观察角度:无论我家的哈士奇还是李四家的萨摩耶,它们都属于``家犬亚种(Canis lupus familiaris)''——那么它们当然是同类了。\par
30+
这时你就会发现一个问题:好像我家的哈士奇同时属于``家犬''``哈士奇''哎。其实不只如此,它还属于``狼种(Canis lupus)''``犬属(Canis)''``犬科(Canidae)''``食肉目(Carnivora)''`` 哺乳纲(Mammalia)''``脊索动物门(Chordata)''``动物界(Animalia)''。看上去是不是很复杂?很吓人?\par
31+
问题来了。在我们既有的认知中,一个对象只能是单个类的对象,那么现在它同时是这么多类的对象,这岂不是乱套了?其实并不会,因为这些类之间存在着一种\textbf{``属与种''的关系。用英文可以阐述为``Is-a relationship'',在C++中可以诠释为``基类与派生类''\footnote{值得一提的是,一般我们只用公开继承/受保护继承来表示``Is-a relationship'';至于私有继承,它更适合用来表示``Has-a relationship''。}}\par
32+
``属与种''的关系描述的是两个类的关系,它们有点像数学上的集合间包含关系——如果一个对象是类 \lstinline@A@ 的对象,那么它一定也是类 \lstinline@B@ 的对象。在这个关系当中,\lstinline@A@ 就是基类,而 \lstinline@B@ 是派生类。对于派生类的对象来说,它既有基类的共性——比如我家哈士奇能``汪汪''叫,王五家的泰迪也能,又有派生类的独特性——比如我家哈士奇是拆家小能手,但王五家的泰迪就不能胜任了。\par
33+
\subsection*{继承的优点}
34+
\textbf{继承(Inheritance)}是一种代码重用的好方法。如果我们已经写了一个类 \lstinline@Dog@,而我们想要再写一个 \lstinline@Husky@ 类,那么我们没必要把 \lstinline@Dog@ 已有的那部分功能再写一遍,直接让 \lstinline@Husky@ 类继承 \lstinline@Dog@ 类就足够了——然后我们只需要集中精力去写诸如``拆家''之类的独特功能即可。\par
35+
继承也使得多态变得可能。我们会在第十章讲到这点。\par

main.aux

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,20 @@
185185
\@writefile{toc}{\contentsline {chapter}{\numberline {第九章\hspace {.3em}}类的继承}{241}{chapter.9}\protected@file@percent }
186186
\@writefile{lof}{\addvspace {10.0pt}}
187187
\@writefile{lot}{\addvspace {10.0pt}}
188+
\@writefile{toc}{\contentsline {section}{\numberline {9.1}概念介绍}{241}{section.9.1}\protected@file@percent }
188189
\gdef \LT@ii {\LT@entry
189190
{1}{42.00002pt}\LT@entry
190191
{1}{136.19997pt}\LT@entry
191192
{1}{117.00002pt}\LT@entry
192193
{1}{52.00002pt}\LT@entry
193194
{1}{82.00002pt}}
194-
\@writefile{toc}{\contentsline {part}{精讲篇}{245}{part*.166}\protected@file@percent }
195-
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 A\hspace {.3em}}C++运算符基本属性}{245}{appendix.A}\protected@file@percent }
195+
\@writefile{toc}{\contentsline {part}{精讲篇}{247}{part*.170}\protected@file@percent }
196+
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 A\hspace {.3em}}C++运算符基本属性}{247}{appendix.A}\protected@file@percent }
196197
\@writefile{lof}{\addvspace {10.0pt}}
197198
\@writefile{lot}{\addvspace {10.0pt}}
198-
\newlabel{ch:appendix_A}{{A}{245}{C++运算符基本属性}{appendix.A}{}}
199-
\@writefile{lot}{\contentsline {table}{\numberline {A.1}{截至C++17的所有运算符}}{245}{table.A.1}\protected@file@percent }
200-
\newlabel{tab:A-1}{{A.1}{245}{截至C++17的所有运算符}{table.A.1}{}}
199+
\newlabel{ch:appendix_A}{{A}{247}{C++运算符基本属性}{appendix.A}{}}
200+
\@writefile{lot}{\contentsline {table}{\numberline {A.1}{截至C++17的所有运算符}}{247}{table.A.1}\protected@file@percent }
201+
\newlabel{tab:A-1}{{A.1}{247}{截至C++17的所有运算符}{table.A.1}{}}
201202
\gdef \LT@iii {\LT@entry
202203
{1}{33.67001pt}\LT@entry
203204
{1}{33.81001pt}\LT@entry
@@ -209,11 +210,11 @@
209210
{1}{26.72002pt}\LT@entry
210211
{1}{27.283pt}\LT@entry
211212
{1}{86.44002pt}}
212-
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 B\hspace {.3em}}ASCII码表(0到127)}{247}{appendix.B}\protected@file@percent }
213+
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 B\hspace {.3em}}ASCII码表(0到127)}{249}{appendix.B}\protected@file@percent }
213214
\@writefile{lof}{\addvspace {10.0pt}}
214215
\@writefile{lot}{\addvspace {10.0pt}}
215-
\@writefile{lot}{\contentsline {table}{\numberline {B.1}{33个ASCII控制字符}}{247}{table.B.1}\protected@file@percent }
216-
\newlabel{tab:B-1}{{B.1}{247}{33个ASCII控制字符}{table.B.1}{}}
216+
\@writefile{lot}{\contentsline {table}{\numberline {B.1}{33个ASCII控制字符}}{249}{table.B.1}\protected@file@percent }
217+
\newlabel{tab:B-1}{{B.1}{249}{33个ASCII控制字符}{table.B.1}{}}
217218
\gdef \LT@iv {\LT@entry
218219
{1}{33.67001pt}\LT@entry
219220
{1}{33.81001pt}\LT@entry
@@ -227,19 +228,19 @@
227228
{1}{33.67001pt}\LT@entry
228229
{1}{33.81001pt}\LT@entry
229230
{1}{26.72002pt}}
230-
\@writefile{lot}{\contentsline {table}{\numberline {B.2}{95个ASCII可打印字符}}{248}{table.B.2}\protected@file@percent }
231-
\newlabel{tab:B-2}{{B.2}{248}{95个ASCII可打印字符}{table.B.2}{}}
232-
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 C\hspace {.3em}}相关数学知识}{249}{appendix.C}\protected@file@percent }
231+
\@writefile{lot}{\contentsline {table}{\numberline {B.2}{95个ASCII可打印字符}}{250}{table.B.2}\protected@file@percent }
232+
\newlabel{tab:B-2}{{B.2}{250}{95个ASCII可打印字符}{table.B.2}{}}
233+
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 C\hspace {.3em}}相关数学知识}{251}{appendix.C}\protected@file@percent }
233234
\@writefile{lof}{\addvspace {10.0pt}}
234235
\@writefile{lot}{\addvspace {10.0pt}}
235-
\@writefile{toc}{\contentsline {section}{\numberline {C.1}数制转换}{249}{section.C.1}\protected@file@percent }
236-
\@writefile{lof}{\contentsline {figure}{\numberline {C.1}{\ignorespaces 一个简单的数字时钟}}{249}{figure.C.1}\protected@file@percent }
237-
\@writefile{lof}{\contentsline {figure}{\numberline {C.2}{\ignorespaces 从计数到乘方}}{250}{figure.C.2}\protected@file@percent }
238-
\@writefile{lof}{\contentsline {figure}{\numberline {C.3}{\ignorespaces 一个12进制乘法表}}{251}{figure.C.3}\protected@file@percent }
239-
\@writefile{lof}{\contentsline {figure}{\numberline {C.4}{\ignorespaces $(1a.c3)_{16}$的形式化表示}}{253}{figure.C.4}\protected@file@percent }
240-
\@writefile{lof}{\contentsline {figure}{\numberline {C.5}{\ignorespaces 通过短除法把十进制数转换成$R$进制}}{256}{figure.C.5}\protected@file@percent }
241-
\@writefile{lof}{\contentsline {figure}{\numberline {C.6}{\ignorespaces 二进制与八进制的转换}}{257}{figure.C.6}\protected@file@percent }
242-
\@writefile{lof}{\contentsline {figure}{\numberline {C.7}{\ignorespaces 二进制与十六进制的转换}}{258}{figure.C.7}\protected@file@percent }
243-
\@writefile{toc}{\contentsline {section}{\numberline {C.2}布尔代数基础}{258}{section.C.2}\protected@file@percent }
244-
\@writefile{toc}{\contentsline {chapter}{跋}{259}{appendix*.174}\protected@file@percent }
245-
\gdef \@abspage@last{266}
236+
\@writefile{toc}{\contentsline {section}{\numberline {C.1}数制转换}{251}{section.C.1}\protected@file@percent }
237+
\@writefile{lof}{\contentsline {figure}{\numberline {C.1}{\ignorespaces 一个简单的数字时钟}}{251}{figure.C.1}\protected@file@percent }
238+
\@writefile{lof}{\contentsline {figure}{\numberline {C.2}{\ignorespaces 从计数到乘方}}{252}{figure.C.2}\protected@file@percent }
239+
\@writefile{lof}{\contentsline {figure}{\numberline {C.3}{\ignorespaces 一个12进制乘法表}}{253}{figure.C.3}\protected@file@percent }
240+
\@writefile{lof}{\contentsline {figure}{\numberline {C.4}{\ignorespaces $(1a.c3)_{16}$的形式化表示}}{255}{figure.C.4}\protected@file@percent }
241+
\@writefile{lof}{\contentsline {figure}{\numberline {C.5}{\ignorespaces 通过短除法把十进制数转换成$R$进制}}{258}{figure.C.5}\protected@file@percent }
242+
\@writefile{lof}{\contentsline {figure}{\numberline {C.6}{\ignorespaces 二进制与八进制的转换}}{259}{figure.C.6}\protected@file@percent }
243+
\@writefile{lof}{\contentsline {figure}{\numberline {C.7}{\ignorespaces 二进制与十六进制的转换}}{260}{figure.C.7}\protected@file@percent }
244+
\@writefile{toc}{\contentsline {section}{\numberline {C.2}布尔代数基础}{260}{section.C.2}\protected@file@percent }
245+
\@writefile{toc}{\contentsline {chapter}{跋}{261}{appendix*.178}\protected@file@percent }
246+
\gdef \@abspage@last{268}

main.log

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex 2023.12.3) 18 JAN 2024 18:24
1+
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex 2023.12.3) 19 JAN 2024 22:17
22
entering extended mode
33
\write18 enabled.
44
file:line:error style messages enabled.
@@ -1576,48 +1576,51 @@ Underfull \vbox (badness 10000) has occurred while \output is active []
15761576

15771577
]
15781578
第九章
1579-
(./generalized_parts/09_class_inheritance/01_the_concepts_of_inheritance.tex) (./generalized_parts/09_class_inheritance/02_public_inheritance_and_protected_members.tex) (./generalized_parts/09_class_inheritance/03_private_inheritance.tex) (./generalized_parts/09_class_inheritance/04_sequential_inheriance.tex))) (././intensive_part.tex [241] [242
1579+
(./generalized_parts/09_class_inheritance/01_the_concepts_of_inheritance.tex
1580+
Underfull \vbox (badness 1210) has occurred while \output is active []
15801581

1581-
] [243] [244]) (./other_parts/appendix.tex (./other_parts/appendices/A.tex
1582+
[241] [242]) (./generalized_parts/09_class_inheritance/02_public_inheritance_and_protected_members.tex) (./generalized_parts/09_class_inheritance/03_private_inheritance.tex) (./generalized_parts/09_class_inheritance/04_sequential_inheriance.tex))) (././intensive_part.tex [243] [244
1583+
1584+
] [245] [246]) (./other_parts/appendix.tex (./other_parts/appendices/A.tex
15821585
附录 A
15831586

15841587
Underfull \hbox (badness 6063) in paragraph at lines 16--16
15851588
[][][]\TU/SimSun(0)/m/n/8.33344 对 于 内 置 类 型 数 组 [][][] 的 下 标 运 算,| [][][][][][] |(和 [][][][][][])| 都 会 解 析 为 [][][][][][][]
15861589
[]
15871590

1588-
[245
1591+
[247
15891592

1590-
]) (./other_parts/appendices/B.tex [246]
1593+
]) (./other_parts/appendices/B.tex [248]
15911594
附录 B
1592-
[247
1595+
[249
15931596

1594-
]) (./other_parts/appendices/C.tex [248]
1597+
]) (./other_parts/appendices/C.tex [250]
15951598
附录 C
15961599
File: appendices/../images/other_parts/C_digital_clock.png Graphic file (type bmp)
15971600
<appendices/../images/other_parts/C_digital_clock.png>
1598-
[249
1601+
[251
15991602

16001603
]
16011604
File: appendices/../images/other_parts/C_from_counting_to_exponentiation_300.png Graphic file (type bmp)
16021605
<appendices/../images/other_parts/C_from_counting_to_exponentiation_300.png>
16031606
File: appendices/../images/other_parts/C_duodecimal_multiplication_table.png Graphic file (type bmp)
16041607
<appendices/../images/other_parts/C_duodecimal_multiplication_table.png>
1605-
[250]
1608+
[252]
16061609
Underfull \vbox (badness 2753) has occurred while \output is active []
16071610

1608-
[251] [252]
1611+
[253] [254]
16091612
File: appendices/../images/other_parts/C_formatted_representation_of_a_number_300.png Graphic file (type bmp)
16101613
<appendices/../images/other_parts/C_formatted_representation_of_a_number_300.png>
1611-
[253] [254]
1614+
[255] [256]
16121615
File: appendices/../images/other_parts/C_short_division_300.png Graphic file (type bmp)
16131616
<appendices/../images/other_parts/C_short_division_300.png>
1614-
[255] [256]
1617+
[257] [258]
16151618
File: appendices/../images/other_parts/C_bin_to_oct_300.png Graphic file (type bmp)
16161619
<appendices/../images/other_parts/C_bin_to_oct_300.png>
1617-
[257]
1620+
[259]
16181621
File: appendices/../images/other_parts/C_bin_to_hex_300.png Graphic file (type bmp)
16191622
<appendices/../images/other_parts/C_bin_to_hex_300.png>
1620-
)) (./other_parts/afterword.tex [258])
1623+
)) (./other_parts/afterword.tex [260])
16211624
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
16221625
<use images/other_parts/back_cover.pdf>
16231626
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
@@ -1626,7 +1629,7 @@ File: images/other_parts/back_cover.pdf Graphic file (type pdf)
16261629
<use images/other_parts/back_cover.pdf, page 1>
16271630
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
16281631
<use images/other_parts/back_cover.pdf, page 1>
1629-
[259
1632+
[261
16301633

16311634
]
16321635
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
@@ -1635,21 +1638,21 @@ File: images/other_parts/back_cover.pdf Graphic file (type pdf)
16351638
<use images/other_parts/back_cover.pdf, page 1>
16361639
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
16371640
<use images/other_parts/back_cover.pdf, page 1>
1638-
[260] (./main.aux)
1641+
[262] (./main.aux)
16391642
***********
16401643
LaTeX2e <2023-11-01>
16411644
L3 programming layer <2022/08/05>
16421645
***********
16431646
Package rerunfilecheck Info: File `main.out' has not changed.
1644-
(rerunfilecheck) Checksum: C5012F664BF5810FC516C1487B3A614E;6440.
1647+
(rerunfilecheck) Checksum: 7651E92CF4ABCE9CFB27EDD37B38DBA2;6527.
16451648
)
16461649
Here is how much of TeX's memory you used:
1647-
26943 strings out of 474924
1648-
680115 string characters out of 5765037
1650+
26950 strings out of 474924
1651+
680180 string characters out of 5765037
16491652
2050189 words of memory out of 5000000
1650-
48016 multiletter control sequences out of 15000+600000
1653+
48023 multiletter control sequences out of 15000+600000
16511654
571829 words of font info for 166 fonts, out of 8000000 for 9000
16521655
1348 hyphenation exceptions out of 8191
16531656
93i,17n,111p,1239b,1918s stack positions out of 10000i,1000n,20000p,200000b,200000s
16541657

1655-
Output written on main.pdf (266 pages).
1658+
Output written on main.pdf (268 pages).

0 commit comments

Comments
 (0)