C++中的純虛函數(pure virtual) 和抽象基類(abstract base class)

來源:互聯網
上載者:User

純虛函數(pure virtual), 是一個基類中的方法, 僅僅是聲明, 而不包括定義, 是一個泛化概念(general concept);

是把相應的虛函數, 末尾添加"=0",該虛函數就變為純虛函數, 可以不用添加定義;

如果是其他虛函數, 即使不使用, 也必須定義(define);

包含純虛函數的基類, 是抽象基類(abstract base class),不能定義(define)對象(object), 僅可以作為繼承使用;

代碼:

/*  * CppPrimer.cpp  *  *  Created on: 2013.11.12  *      Author: Caroline  */      /*eclipse cdt*//*更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/*/#include <iostream>  #include <string>  #include <cstddef>        using namespace std;        class Quote {  public:      Quote() = default;      Quote (const std::string& book, double sales_price) :          bookNo (book), price (sales_price) {}      std::string isbn() const { return bookNo; }      virtual double net_price (std::size_t n) const { return n* price; } //虛函數      virtual ~Quote() = default; //動態綁定析構器  private:      std::string bookNo;  protected: //受保護類型      double price = 0.0;  };        class Disc_quote : public Quote { //抽象基類  public:      Disc_quote() = default;      Disc_quote (const std::string& book, double price, std::size_t qty, double disc) :          Quote(book, price), quantity (qty), discount (disc) {}      double net_price (std::size_t) const = 0; //純虛函數  protected:          std::size_t quantity = 0;          double discount = 0.0;  };        class Bulk_quote final : public Disc_quote { //final限定詞, 無法被繼承  public:      Bulk_quote() = default;      Bulk_quote(const std::string&book, double p, std::size_t qty, double disc) :          Disc_quote(book, p, qty, disc) {} //使用基類的構造器      double net_price(std::size_t cnt) const override;  };        double Bulk_quote::net_price(std::size_t cnt) const{      if (cnt >= quantity)          return cnt * (1-discount) * price;      else        return cnt * price;  }        double print_total(std::ostream &os, const Quote& item, std::size_t n)  {      double ret = item.net_price(n);      os << "ISBN: " << item.isbn() << " # sold: " << n << " total due: " << ret << std::endl;      return ret;  }        int main (void) {            Quote q("CppPrimer", 99);      Bulk_quote bq("CppPrimer", 99, 10, 0.5);      Quote& rq = bq;      std::size_t n = 20;      std::cout << "We want to buy " << n << " books. " << std::endl;      std::cout << "Quote net_price : " << q.net_price(n) << std::endl;      std::cout << "Bulk_quote net_price : " << bq.net_price(n) << std::endl;      std::cout << "R Quote net_price : " << rq.net_price(n) << std::endl;            //動態綁定      Quote base("EffectiveCpp", 50);      print_total(cout, base, 10);      Bulk_quote derived("EffectiveCpp", 50, 5, .19);      print_total(cout, derived, 10);            //複製-賦值操作, 衍生類別->基類轉換      base = derived;      std::cout << "net_price : " << base.net_price(20) << std::endl;            double undiscounted = derived.Quote::net_price(20); //衍生類別使用基類的虛函數      std::cout << "undiscounted : " << undiscounted << std::endl;      return 0;        }

作者:csdn部落格 Spike_King

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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