⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right6-2.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 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>第 6 章</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"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                    
&nbsp;&nbsp;&nbsp; <span style="background-color: #CCFF99">第 6 章</span><span style="background-color: #CCFF99">&nbsp;                               
</span>&gt;<span style="background-color: #CCFF99"> 第 2 节 </span>&gt;<span style="background-color: #CCFF99">    
 6.2 </span><span style="background-color: #CCFF99">虚函数</span></p>            
<hr color="#008000" size="1">

<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">虚函数是动态联编的基础。虚函数是成员函数,而且是非</font>static<font FACE="??ì?,SimSun" LANG="ZH-CN">的成员函数。</font></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">1.虚函数</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">virtual     
    <font FACE="??ì?,SimSun" LANG="ZH-CN">类型说明符</font> <font FACE="??ì?,SimSun" LANG="ZH-CN">函数名</font>(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>)</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><font color="#008000">virtual</font><font FACE="??ì?,SimSun" LANG="ZH-CN">说明的函数称为虚函数。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </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: 0; margin-bottom: 0"> </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="example6-2.htm" target="_blank">【例</a></font><a href="example6-2.htm" target="_blank">6-2<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">通过基类指针可以访问派生类对象成员函数</font><font FACE="??ì?,SimSun" LANG="ZH-CN">。</font></p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>

<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">派生类中对基类的虚函数进行替换时,要求派生类中说明的虚函数与基类中的被替换的虚函数之间满足如下条件:</font></p>
<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> 
<font FACE="??ì?,SimSun" LANG="ZH-CN">与基类的虚函数有相同的名称。</font></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>
<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> 
<font FACE="??ì?,SimSun" LANG="ZH-CN">与基类的虚函数有相同的返回值。</font></font></p>   
<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>virtual<font FACE="??ì?,SimSun" LANG="ZH-CN">显式说明。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example6-3.htm" target="_blank">【例</a></font><a href="example6-3.htm" target="_blank">6-3<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">动态多态举例。</font></font></p>

<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example6-4.htm" target="_blank">【例</a></font><a href="example6-4.htm" target="_blank">6-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 size="2">2.     
</font><font FACE="??ì?,SimSun" LANG="ZH-CN"><font size="2">纯虚函数</font></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     
    &lt;<font FACE="??ì?,SimSun" LANG="ZH-CN">类名</font>&gt;</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">virtual     
    <font FACE="??ì?,SimSun" LANG="ZH-CN">类型</font> <font FACE="??ì?,SimSun" LANG="ZH-CN">函数名</font>(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>)</font><b><font size="2" color="#FF6600">=0</font><font color="#FF6600" size="3">;</font></b></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></p>
  </blockquote>
</blockquote>
<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: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example6-5.htm" target="_blank">【例</a></font><a href="example6-5.htm" target="_blank">6-5<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 style="line-height: 100%; margin-top: 0; margin-bottom: 10"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">带有纯虚函数的类是抽象类。抽象类的主要作用是通过它为一个类族建立一个公共的接口,使它们能够更有效地发挥多态特性。</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 10"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出。如果派生类没有给出全部纯虚函数的实现,只是继承基类的纯虚函数,则这个派生类仍然还是一个抽象类。如果派生类中给出了基类所有纯虚函数的实现,则该派生类就不再是抽象类了,而是一个可以创建对象的具体类了。</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 10"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">抽象类不能实例化,即不能声明一个抽象类的对象。但是,我们可以声明一个抽象类的指针和引用。通过指针或引用,我们就可以指向并访问派生类对象,进而访问派生类的成员,这种访问是具有多态特征的。</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 10"><font size="2">4.     
<font FACE="??ì?,SimSun" LANG="ZH-CN">虚析构函数</font></font></p>
<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>virtual<font FACE="??ì?,SimSun" LANG="ZH-CN">进行说明,称该析构函数为虚析构函数。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">如果一个基类的析构函数被说明为虚析构函数,则它的派生类中的析构函数也是虚析构函数,不管它是否使用了关键字</font>virtual<font FACE="??ì?,SimSun" LANG="ZH-CN">进行说明。</font></font></p>
<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>delete<font FACE="??ì?,SimSun" LANG="ZH-CN">运算符删除一个对象时,能保证析构函数被正确地执行。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example6-6.htm" target="_blank">【例</a></font><a href="example6-6.htm" target="_blank">6-6<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">虚析构函数举例。</font></font></p>

<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>

<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;&nbsp; <span style="position: absolute; left: 33; top: 988"><a href="right6-1.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="position: absolute; left: 503; top: 992"><a href="right6-3.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>          

</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -