選擇java 進入自由開放的國度

          隨筆 - 49, 文章 - 3, 評論 - 154, 引用 - 1
          數(shù)據(jù)加載中……

          C++中關(guān)于繼承的多態(tài)

          LibMat
          Book
          AudioBook

          Class:LibMat
          class LibMat  
          {
          public:
              LibMat();
              virtual 
          ~LibMat();
              virtual 
          void print() const;
          }
          ;
          // LibMat.cpp: implementation of the LibMat class.
          //
          //////////////////////////////////////////////////////////////////////

          #include 
          "LibMat.h"
          #include 
          <iostream>

          using namespace std;

          //////////////////////////////////////////////////////////////////////
          // Construction/Destruction
          //////////////////////////////////////////////////////////////////////

          LibMat::LibMat()
          {
           cout 
          << "LibMat::LibMat() constructor\n";
          }


          LibMat::
          ~LibMat()
          {
            cout 
          << "LibMat :: ~ LibMat() destructor\n";
          }


          void LibMat ::print() const 
          {
            cout 
          << "LibMat::print() -- I am a LibMat object!\n";
          }

          Class:Book
          #include "LibMat.h"
          #include 
          <string>
          using namespace std;

          class Book : public LibMat  
          {
          public:
              Book(
          const string &const string &);
              virtual 
          ~Book();
              virtual 
          void print() const;
              
          const string& title()  const{return _title;};
              
          const string& author() const{return _author;};
          protected:
              string _title;
              string _author;
          }
          ;
          #include "Book.h"
          #include 
          <iostream>
          using namespace std;
          //////////////////////////////////////////////////////////////////////
          // Construction/Destruction
          //////////////////////////////////////////////////////////////////////

          Book::Book(
          const string &title, const string &author)
          : _title(title), _author(author)
          {
            cout 
          << "Book::Book(" << _title << "" << _author << ") constructor\n";
          }


          Book::
          ~Book()
          {
           cout 
          << "Book:~Book destructor!\n";
          }


          void Book::print() const 
          {
            cout 
          << "Book ::print() -- I am a Book object!\n"
                 
          << "My Title is : " << _title << ", My author is : " << _author << endl;
          }
          測試代碼:main.cpp
          #include "AudioBook.h"
          #include 
          <iostream>
          using namespace std;


          void print(const LibMat&);

          void main ()
          {
            
          //LibMat libmat;
            
          //print(libmat);
            
            
          //polymorphism
            
          //Book b("The Castle", "Franz Kafka");
            
          //print(b);

            cout 
          <<"----------------------\n";
            AudioBook a(
          "The Castle""Franz kafka""Hanhongbo");
            print(a);
          }


          void print(const LibMat& mat)
          {
            cout 
          << "in global print():\n";

            mat.print();
          }

          posted on 2006-01-08 20:37 soochow_hhb 以java論成敗 以架構(gòu)論英雄 閱讀(369) 評論(0)  編輯  收藏 所屬分類: Reading

          主站蜘蛛池模板: 登封市| 利辛县| 嵊泗县| 山阳县| 昌图县| 专栏| 陵水| 达州市| 文水县| 湄潭县| 紫云| 济南市| 南丰县| 揭阳市| 江门市| 商丘市| 正安县| 道真| 巧家县| 博罗县| 溧阳市| 太仆寺旗| 苗栗县| 宁强县| 涡阳县| 泾源县| 攀枝花市| 龙山县| 介休市| 茶陵县| 射阳县| 普宁市| 宁蒗| 闵行区| 元朗区| 苗栗县| 怀仁县| 上虞市| 湖口县| 修武县| 商河县|