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

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

          C++中關于繼承的多態

          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論成敗 以架構論英雄 閱讀(361) 評論(0)  編輯  收藏 所屬分類: Reading

          主站蜘蛛池模板: 宜州市| 读书| 准格尔旗| 佛坪县| 平舆县| 会宁县| 德令哈市| 焦作市| 神池县| 泰州市| 万山特区| 江孜县| 鹤壁市| 庐江县| 施甸县| 钟山县| 常熟市| 司法| 临夏县| 昌黎县| 太保市| 西宁市| 汝州市| 江油市| 临安市| 甘洛县| 仙居县| 凤庆县| 离岛区| 永善县| 旬邑县| 高唐县| 晴隆县| 柏乡县| 安吉县| 深圳市| 云安县| 乐业县| 谷城县| 周宁县| 郎溪县|