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

📄 3tree.html

📁 C ++ in action
💻 HTML
字号:
<html>
<head>
	<title>Parse Tree</title>
    <meta  name="description" content="Implementation of a parse tree using polymorphism in C++">
    <meta name="keywords" content="virtual, member function, destructor, pure virtual, protected, data member">
	<link rel="stylesheet" href="../../rs.css">
</head>

<body background="../../images/margin.gif" bgcolor="#FFFFDC">

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

<h3>Parse Tree</h3>

<p class=topics>Virtual member functions, virtual destructors, pure virtual functions, protected data members.

<p>I will demonstrate the use of polymorphism in an example of a data structure梩he arithmetic tree. An arithmetic expression can be converted into a tree structure whose nodes are arithmetic operators and leaf nodes are numbers. Figure 2-3 shows the example of a tree that corresponds to the expression 2 * (3 + 4) + 5. Analyzing it from the root towards the leaves we first encounter the plus node, whose children are the two terms that are to be added. The left child is a product of two factors. The left factor is number 2 and the right factor is the sum of 3 and 4. The right child of the top level plus node is number 5. Notice that the tree representation doesn抰 require any parentheses or the knowledge of operator precedence. It uniquely describes the calculation to be performed.

<p align="CENTER"><img src="images/Image38.gif" width=282 height=264>

<p class=caption>Figure 2-3 The arithmetic tree corresponding to the expression 2 * (3 + 4) + 5.


<p>We will represent the nodes of the arithmetic tree as objects inheriting from a single class <var>Node</var>. The direct descendants of the Node are <var>NumNode</var> representing a number and <var>BinNode</var> representing a binary operator. For simplicity, we will restrict ourselves to only two classes derived from <var>BinNode</var>, the <var>AddNode</var> and the <var>MultNode</var>. Figure 2-4 shows the class hierarchy I have just described. Abstract classes are the classes that cannot be instantiated, they only serve as parents for other classes. I抣l explain this term in a moment

<p align="CENTER"><img src="images/Image39.gif" width=426 height=150>

<p class=caption>Figure 2-4 The class hierarchy of nodes.

<p>What are the operations we would like to perform on a node? We would like to be able to calculate its value and, at some point, destroy it. The <var>Calc</var> method returns a double as the result of the calculation of the node抯 value. Of course, for some nodes the calculation may involve the recursive calculations of its children. The method is <var>const</var> since it doesn抰 change the node itself. Since each type of node has to provide its own implementation of the <var>Calc</var> method, we make this function virtual. However, there is no &quot;default&quot; implementation of <var>Calc</var> for an arbitrary <var>Node</var>. The function that has no implementation (inherited or otheriwise) is called <b><i>pure virtual</i></b>. That抯 the meaning of <var>= 0</var> in the declaration of <var>Calc</var>.

<p>A class that has one or more pure virtual functions is called an <b><i>abstract class</i></b> and it cannot be instantiated (no object of this class can be created). Only classes that are derived from it, and which provide their own implementations of all the pure virtual functions, can be instantiated. Notice that our sample arithmetic tree has instances of <var>AddNodes</var>, <var>MultNodes</var> and <var>NumNodes</var>, but no instances of <var>Nodes</var> or <var>BinNodes</var>.

<p>A rule of thumb is that, if a class has a virtual function, it probably needs a virtual destructor as well--and once we decide to pay the overhead of a vtable pointer, all subsequent virtual functions don抰 increase the size of the object. So, in such a case, adding a virtual destructor doesn't add any significant overhead.

<p>In our case we can anticipate that some of the descendant nodes will have to destroy their children in their destructors, so we really need a virtual destructor. A destructor cannot be made pure virtual, because it is actually called by the destructors of the derived classes. That's why I gave it an empty body. (Even though I made it inline, the compiler will create a function body for it, because it needs to stick a pointer to it into the virtual table).
<tr>
<td class=margin valign=top>

<br>
<a href="source/tree.zip">
<img src="Images/brace.gif" width=16 height=16 border=1 alt="Download!"><br>source</a>
<td>
<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>
<pre>class Node
{
public:
    virtual ~Node () {}
    virtual double Calc () const = 0;
};</pre>
</table><!-- End Code -->

<var><p>NumNode</var> stores a <var>double</var> value that is initialized in its constructor. It also overrides the <var>Calc</var> virtual function. In this case, <var>Calc</var> simply returns the value stored in the node.

<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>

<pre>class NumNode: public Node
{
public:
    NumNode (double num) : _num (num ) {}
    double Calc () const;
private:
    const double _num;
};

double NumNode::Calc () const
{
    cout &lt;&lt; "Numeric node " &lt;&lt; _num &lt;&lt; endl;
    return _num;
}</pre>
</table><!-- End Code -->


<var><p>BinNode</var> has two children that are pointers to nodes. They are initialized in the constructor and deleted in the destructor梩his is why I could make them <var>const</var> pointers (but not <i>pointers to</i> <var>const</var>, since I have to call the non-<var>const</var> method on them梩he destructor). The <var>Calc</var> method is still pure virtual, inherited from <var>Node</var>, only the descendants of <var>BinNode</var> will know how to implement it.

<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>

<pre>class BinNode: public Node
{
public:
    BinNode (Node * pLeft, Node * pRight)
      : _pLeft (pLeft), _pRight (pRight) {}
    ~BinNode ();
protected: 
    Node * const _pLeft;
    Node * const _pRight;
};

BinNode::~BinNode ()
{
    delete _pLeft;
    delete _pRight;
}</pre>
</table><!-- End Code -->

<p>This is where you first see the advantage of polymorphism. A binary node can have children which are arbitrary nodes. Each of them can be a number node, an addition node, or a multiplication node. There are nine possible combinations of children梚t would be silly to make separate classes for each of them (consider, for instance, <var>AddNodeWithLeftMultNodeAndRightNumberNode</var>). We had no choice but to accept and store pointers to children as more general pointers to <var>Nodes</var>. Yet, when we call destructors through them, we need to call different functions to destroy different nodes. For instance, <var>AddNode</var> has a different destructor than a <var>NumNode</var> (which has an empty one), and so on. This is why we had to make the destructors of <var>Nodes</var> virtual.
<p>Notice that the two data members of <var>BinNode</var> are not <var>private</var>梩hey are <var>protected</var>. This qualification is slightly weaker than <var>private</var>. A private data member or method cannot be accessed from any code outside of the implementation of the given class (or its friends). Not even from the code of the <i>derived</i> class. Had we made <var>_pLeft</var> and <var>_pRight</var> private, we抎 have to provide public methods to set and get them. That would be tantamount to exposing them to everybody. By making them <var>protected</var>  we are letting classes <i>derived</i> from <var>BinNode</var> manipulate them, but, at the same time, bar anybody else from doing so.

<b><p>Table 1</b>
<table cellspacing=0 border=1 cellpadding=7 width=510>
<tr><td width="33%" valign="TOP">
<b>Access specifier</b></td>
<td width="67%" valign="TOP">
<b>Who can access such member?</b></td>
</tr>
<tr><td width="32%" valign="TOP">
<var>public</var></td>
<td width="68%" valign="TOP">
<font size="+1">anybody</font></td>
</tr>
<tr><td width="32%" valign="TOP">
<var>protected</var></td>
<td width="68%" valign="TOP">
<font size="+1">the class itself, its friends and derived classes</font></td>
</tr>
<tr><td width="32%" valign="TOP">
<var>private</var></td>
<td width="68%" valign="TOP">
<font size="+1">only the class itself and its friends</font></td>
</tr>
</table>


<p>The class <var>AddNode</var> is derived from <var>BinNode</var>.
<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>

<pre>class AddNode: public BinNode
{
public:
    AddNode (Node * pLeft, Node * pRight)
        : BinNode (pLeft, pRight) {}
    double Calc () const;
};</pre>
</table><!-- End Code -->

<p>It provides its own implementation of <var>Calc</var>. This is where you see the advantages of polymorphism again. We let the child nodes calculate themselves. Since the <var>Calc</var> method is virtual, they will do the right thing based on their actual class, and not on the class of the pointer (<var>Node *</var>). The two results of calling <var>Calc</var> are added and the sum returned.

<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>

<pre>double AddNode::Calc () const
{
    cout &lt;&lt; "Adding\n";
    return _pLeft-&gt;Calc () + _pRight-&gt;Calc ();
}</pre>
</table><!-- End Code -->

<p>Notice how the method of <var>AddNode</var> directly accesses its parent抯 data members <var>_pLeft</var> and <var>_pRight</var>. Were they declared private, such access would be flagged as an error by the compiler.
<p>For completeness, here抯 the implementation of the <var>MultNode</var> and a simple test program.

<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>

<pre>class MultNode: public BinNode
{
public:
    MultNode (Node * pLeft, Node * pRight)
        : BinNode (pLeft, pRight) {}
    double Calc () const;
};

double MultNode::Calc () const
{
    cout &lt;&lt; "Multiplying\n";
    return _pLeft-&gt;Calc () * _pRight-&gt;Calc ();
}

int main ()
{
    // ( 20.0 + (-10.0) ) * 0.1
    Node * pNode1 = new NumNode (20.0);
    Node * pNode2 = new NumNode (-10.0);
    Node * pNode3 = new AddNode (pNode1, pNode2);
    Node * pNode4 = new NumNode (0.1);
    Node * pNode5 = new MultNode (pNode3, pNode4);
    cout &lt;&lt; "Calculating the tree\n";
    // tell the root to calculate itself
    double x = pNode5-&gt;Calc ();
    cout &lt;&lt; x &lt;&lt; endl;
    delete pNode5; // and all children
}</pre>
</table>
<!-- End Definition -->
<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10>
<td bgcolor="#cccccc" class=sidebar>
Do you think you can write more efficient code by not using polymorphism? Think twice! If you're still not convinced, go on a little <a href="c_digr.html">sidetrip into the alternative universe of C</a>.
</table>
<!-- End Sidebar -->

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

⌨️ 快捷键说明

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