【C++學習】多態——解析樹執行個體分析

來源:互聯網
上載者:User

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

情境:

 

分析: 每個類的節點都必須提供它自己的Calc方法,所以採用多態進行操作。我們將Node節點和BinNode節點設計為抽象類別,抽象類別是不能執行個體化的類,它們只能作為其他類的父類。一個包含一個或多個純虛函數的類稱為抽象類別,一個沒有具體實現的函數稱為純虛函數,只有從一個抽象類別派生的類以及為所有純虛函數提供了實現代碼的類才能被執行個體化。一般來說,若一個類有一個純虛函數,它可能同時需要一個虛解構函式。所以類設計層次如下:

 

 

 

 

代碼:

 

Tree.h

 

#if !defined (TREE_H)
#define TREE_H
// (c) Bartosz Milewski 2000

class Node
{
public:
    virtual ~Node () {}//設定一個虛解構函式
    virtual double Calc () const = 0;//設定一個純虛函數,也就是=0的意義所在。
};

class NumNode: public Node//繼承了Node
{
public:
    NumNode (double num) : _num (num ) {}
    double Calc () const;
private:
    const double _num;
};

class BinNode: public Node
{
public:
    BinNode (Node * pLeft, Node * pRight)
        : _pLeft (pLeft), _pRight (pRight) {}
    ~BinNode ();
protected: //使用protected的原因是它可以被衍生類別直接使用,這樣我們不用提供setter和getter來進行訪問了。
    Node * const _pLeft;
    Node * const _pRight;
};

class AddNode: public BinNode
{
public:
    AddNode (Node * pLeft, Node * pRight)
        : BinNode (pLeft, pRight) {}
    double Calc () const;
};

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

#endif

 

Tree.cpp

 

// (c) Bartosz Milewski 2000
#include "Tree.h"
#include <iostream>

double NumNode::Calc () const
{
    std::cout << "Numeric node " << _num << std::endl;
    return _num;
}

BinNode::~BinNode ()
{
    delete _pLeft;
    delete _pRight;
}

double AddNode::Calc () const
{
    std::cout << "Adding/n";
    return _pLeft->Calc () + _pRight->Calc (); //直接存取父類protected成員
}

double MultNode::Calc () const
{
    std::cout << "Multiplying/n";
    return _pLeft->Calc () * _pRight->Calc () ;//直接存取父類protected成員
}

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);
    std::cout << "Calculating the tree/n";
    // tell the root to calculate itself ,從根開始計算
    double x = pNode5->Calc (); 
    std::cout << "Result: " << x << std::endl;
    delete pNode5; // and all children
}

 

註:要是用C語言進行實現的時候我們不可以避免使用Switch語句,這個例子也說明在C++中僅在不能使用多態的時候使用Switch語句。

 

參考文獻:http://www.informit.com/articles/article.aspx?p=21484

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.