📄 right4-2.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 4 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><font size="2"><img border="0" src="1.gif" width="63" height="70"></font></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"> </font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2">
<span style="background-color: #CCFF99">第 4 章 </span>><span style="background-color: #CCFF99"> 第
2 节 </span>><span style="background-color: #CCFF99">
4.2 多重继承</span></font></p>
<hr color="#008000" size="1">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0" class="右标题"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">多重继承可以看作是单继承的扩展。所谓多重继承是指派生类具有多个基类,派生类与每个基类之间的关系仍可看作是一个单继承。</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">1.
多重继承的定义</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">多重继承下派生类的定义格式如下:</font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">class
<font FACE="??ì?,SimSun" LANG="ZH-CN">派生类名</font>: <font FACE="??ì?,SimSun" LANG="ZH-CN">继承方式</font>1
<font FACE="??ì?,SimSun" LANG="ZH-CN">基类名</font>1, <font FACE="??ì?,SimSun" LANG="ZH-CN">继承方式</font>2
<font FACE="??ì?,SimSun" LANG="ZH-CN">基类名</font>2, <font FACE="??ì?,SimSun" LANG="ZH-CN">…</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font FACE="??ì?,SimSun" LANG="ZH-CN">派生类类体</font>;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">};</font></p>
</blockquote>
</blockquote>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">2.
多重继承的构造函数</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">在多继承的情况下,派生类的构造函数格式如下:</font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font FACE="??ì?,SimSun" LANG="ZH-CN">派生类名</font>(<font FACE="??ì?,SimSun" LANG="ZH-CN">总参数表</font>):
<font FACE="??ì?,SimSun" LANG="ZH-CN">基类名</font>1(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>1),
<font FACE="??ì?,SimSun" LANG="ZH-CN">基类名</font>2(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>2),
<font FACE="??ì?,SimSun" LANG="ZH-CN">…</font>,<font FACE="??ì?,SimSun" LANG="ZH-CN">子对象名</font>(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>n+1),
<font FACE="??ì?,SimSun" LANG="ZH-CN">…</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font FACE="??ì?,SimSun" LANG="ZH-CN">派生类构造函数体</font>;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2" color="#008000">其中,“总参数表”中各个参数包含了其后的各个分参数表。</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example4-4.htm" target="_blank">【例</a></font><a href="example4-4.htm" target="_blank">4-4<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">派生类构造函数的构成及其执行顺序。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">3.
二义性问题</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">一般说来,在派生类中对基类成员的访问应该是唯一的,但是,由于多继承情况下,可能造成对基类中某成员的访问出现不唯一的情况,这称为对基类成员访问的二义性问题。</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 10; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" SIZE="3"><font size="2">①</font>
</font><font size="2">情况1:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">【例</font>4-5<font FACE="??ì?,SimSun" LANG="ZH-CN">】二义性问题。</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class
A</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
f( );</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class
B</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
f( );</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
g( );</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class
C : public A, public B</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
g( );</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
h( );</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">如果定义一个类</font>C<font FACE="??ì?,SimSun" LANG="ZH-CN">的对象</font>c1<font FACE="??ì?,SimSun" LANG="ZH-CN">,则对函数</font>f(
)<font FACE="??ì?,SimSun" LANG="ZH-CN">的访问便具有二义性:</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.f(
);</font></p>
</blockquote>
</blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -