天天看點

面向對象程式設計與泛型程式設計---C++primer讀書筆記1.OOP的基礎2.書店應用程式案列

未調試運作,待完善

1.OOP的基礎

  1. 繼承
  2. 動态綁定
  3. 資料抽象

2.書店應用程式案列

1.基類的定義

Exercise 15.4

哪些應該設計為virtual。

class Library{
public:
    bool check_out(const LibMember&);
    bool check_in(const LibMember&);
    bool is_late(const Date& today);
    double apply_fine();
    ostream& print(ostream& = cout);
    Date due_date() const;
    Date date_borrowed() const;
    string title() const;
    const LibMember& member() const;
};
           
class Item_base{
public:
    Item_base(const std::string &book = "",double sales_price = ):
    isbn(book),price(sales_price){ }

    std::string book( ) const {return isbn;}

    virtual double net_price(std::size_t n) const
    { return n*price;}
    virtual ~Item_base() { }
private:
    std::string isbn;
protected:
    double price;   
};
           
派生類隻能通過派生類對象通路其基類的protected成員
void Bulk_item::memfcn(const Bulk_item &d,const Item_base &b)
{
    double ret = price;
    ret = d.price;
    ret = b.price;//error
}
           

2.定義派生類

class Bulk_item:public Item_base
{
public:
    double net_price(std::size_t) const;
private:
    std::size_t min_qty;
    double discount;
};