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

📄 4inherit.html

📁 Visual C++ has been one of most effective tool for the large industrial applications. This book is t
💻 HTML
字号:
<html>
<head>
    <title>Inheritance</title>
    <meta  name="description" content="Public inheritance in C++">
    <meta name="keywords" content="inheritance, class, constructor, base, is-a">
    <link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>

<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">
	&nbsp;
    <td>
<h3>Inheritance</h3>

<p class=topics>
Public inheritance, initialization of the base class, order of construction/destruction, type double.
<p>
Another important relationship between objects is the "is-a" relationship. When we say that a star is a celestial body, we mean that a star has all the properties of a celestial body, and maybe some others, specific to a star. In C++ this relationship is expressed using inheritance. We can say: class <var>Star</var> inherits from class <var>CelestialBody</var>, or that <var>CelestialBody</var> is the base class of <var>Star</var>.

<p><img src="is-a.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/is-a.gif" width=120 height=114 border=0 alt="is-a">
<p class=caption>Figure 5 Graphical representation the is-a relationships between objects.

<p>
Here's an example:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>#include &lt;iostream&gt;

class <span class=method>CelestialBody</span>
{
public:
    CelestialBody (double mass)
        : _mass (mass)
    {
        std::cout &lt;&lt; "Creating celestial body of mass " &lt;&lt; _mass &lt;&lt; "\n";
    }

    ~CelestialBody ()
    {
        std::cout &lt;&lt; "Destroying celestial body of mass " &lt;&lt; 
            _mass &lt;&lt; "\n";
    }

private:
    const double _mass;
};

class <span class=method>Star</span>: public <span class=method>CelestialBody  </span>// Star is a CelestialBody
{
public:
    Star (double mass, double brightness)
        : CelestialBody (mass), _brightness (brightness)
    {
        std::cout &lt;&lt; "Creating a star of brightness " &lt;&lt; 
              _brightness &lt;&lt; "\n";
    }

    ~Star ()
    {
        std::cout &lt;&lt; "Destroying a star of brightness " &lt;&lt; 
            _brightness &lt;&lt; "\n";
    }

private:
    const double _brightness;
};

int main ()
{
    std::cout &lt;&lt; "    Entering main.\n";
    Star aStar ( 1234.5, 0.1 );
    std::cout &lt;&lt; "    Exiting main.\n";
}</pre>
</table>
<!-- End Code -->

<p>
The line
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>class Star: public CelestialBody  // Star is a CelestialBody</pre>
</table>
<!-- End Code -->

tells the compiler that <var>Star</var> inherits everything from <var>CelestialBody</var>. In particular, since <var>CelestialBody</var> has <var>_mass</var>, <var>Star</var> will have <var>_mass</var>, too. 
<p>
<var>CelestialBody</var> has a constructor that requires an argument of the type <var>double</var>. Double is a built in type corresponding to double precision floating point numbers. Its single precision counterpart is called a <var>float</var>. Notice that our clever object <var>std::cout</var> has no problem printing <var>double</var>s. 
<p>
In the preamble to the constructor of <var>Star</var> we have to pass the argument to the constructor of <var>CelestialBody</var>. Here's how we do it-we invoke the base constructor using the name of its class:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>    Star (double mass, double brightness)
        : CelestialBody (mass), _brightness (brightness)
</pre>
</table>
<!-- End Code -->

<p>
The order of construction is very important. 
<!-- Definition -->
<p>
<table border=4 cellpadding=10><tr>
    <td bgcolor="#ffffff" class=defTable>

First the base class is fully constructed, then the derived class.

</table>
<!-- End Definition -->
<p>
The construction of the derived class proceeds in the usual order: first the embeddings, then the code in the constructor. Again, the order in the preamble is meaningless, and if there are no explicit initializations the whole thing may be omitted. The order of destruction is again the reverse of the order of construction. In particular, the derived class destructor is called first, the destructor of the base class follows.


</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

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