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

📄 right4-1.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2">&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">第 4 章&nbsp; </span>&gt;<span style="background-color: #CCFF99"> 第 
1 节 </span>&gt;<span style="background-color: #CCFF99">   
 4.1 派生类 </span></font></p>         
<hr color="#008000" size="1">

<p style="line-height: 150%; text-indent: 0; margin: 0" class="右标题" align="left"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">通过继承机制,可以利用已有的类来定义新类。所定义的新类不仅拥有新定义的成员,而且还同时拥有旧类的成员。我们称已存在的用来派生新类的类为基类,又称为父类;由已存在的类派生出的新类称为派生类,又称为子类。从一个基类派生的继承称为单继承;从多个基类派生的继承称为多重继承。</font></p>
<p style="line-height: 200%; text-indent: 0; margin: 0" align="left"><font size="2">1 
<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="example4-1.htm" target="_blank">【例</a></font><a href="example4-1.htm" target="_blank">4-1<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><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"><font FACE="??ì?,SimSun" LANG="ZH-CN">现实世界的事物多种多样,许多事物都存在着层次关系,这种层次关系使我们找到了描述事物的解决方法。如下图</font><font FACE="??ì?,SimSun" LANG="ZH-CN">是关于交通工具的一个分类层次关系图。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font face="??ì?,SimSun" lang="ZH-CN" size="2">&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; 
<img border="0" src="sample1.gif" width="261" height="132"></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></p>
</font>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">(1)</font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">派生类的定义格式为:</font></p>
<blockquote>
  <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> 
      <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>
        <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>
      </blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font><font color="#008000" size="3">;</font></p>
    </blockquote>
  </blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">继承方式有三种:</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> 
public<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> 
private<font FACE="??ì?,SimSun" LANG="ZH-CN">表示私有继承,特点是:当类的继承方式为私有继承时,基类中的公有成员和保护成员都以私有成员身份出现在派生类中,而基类的私有成员在派生类中不可访问。</font></font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">③</font> 
protected<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 FACE="??ì?,SimSun" LANG="ZH-CN">构造函数</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">构造函数不能够被继承,因此,派生类的构造函数必须通过调用基类的构造函数来初始化基类子对象。</font></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 FACE="??ì?,SimSun" LANG="ZH-CN" size="2">派生类构造函数的一般格式如下:</font></p>
<blockquote>
  <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>(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>1),<font FACE="??ì?,SimSun" LANG="ZH-CN">子对象名</font>(<font FACE="??ì?,SimSun" LANG="ZH-CN">参数表</font>2)</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><font color="#008000" size="3">;</font></p>
    </blockquote>
  </blockquote>
</blockquote>
<font SIZE="3">

⌨️ 快捷键说明

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