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

📄 example4-1.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
字号:
<html>

<head>
<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-1</title>
</head>

<body>

<blockquote>
  <blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class 
    Vehicle</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      wheels; //<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">float 
      weight; //<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">public:</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void 
      initialize(int in_wheels, float in_weight);</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      get_wheels( ) {return wheels;}</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">float 
      get_weight( ) {return weight;}</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">float 
      wheel_loading( ) {return weight/wheels;}</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 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">//<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">class 
    Car : public Vehicle</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      passenger_load; //<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">public:</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void 
      initialize(int in_wheels, float in_weight, int people = 4);</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      passengers( ) {return passenger_load;}</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">//<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">class 
    Truck : public Vehicle</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      passenger_load; //<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">float 
      payload; //<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">public:</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void 
      init_truck(int how_many = 2, float max_load = 24000.0);</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">float 
      efficiency( ); //<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">int 
      passengers( ) {return passenger_load;}</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>Vehicle<font FACE="??ì?,SimSun" LANG="ZH-CN">中派生出了</font>Car<font FACE="??ì?,SimSun" LANG="ZH-CN">和</font>Truck<font FACE="??ì?,SimSun" LANG="ZH-CN">两个类。我们把</font>Vehicle<font FACE="??ì?,SimSun" LANG="ZH-CN">称为基类,把</font>Car<font FACE="??ì?,SimSun" LANG="ZH-CN">和</font>Truck<font FACE="??ì?,SimSun" LANG="ZH-CN">称为派生类或子类。</font></font></p>

</body>

</html>

⌨️ 快捷键说明

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