Basket2.h
上传用户:qdkongtiao
上传日期:2022-06-29
资源大小:356k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*
  2.  * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
  3.  * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
  4.  * copyright and warranty notices given in that book:
  5.  * 
  6.  * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
  7.  * 
  8.  * 
  9.  * "The authors and publisher have taken care in the preparation of this book,
  10.  * but make no expressed or implied warranty of any kind and assume no
  11.  * responsibility for errors or omissions. No liability is assumed for
  12.  * incidental or consequential damages in connection with or arising out of the
  13.  * use of the information or programs contained herein."
  14.  * 
  15.  * Permission is granted for this code to be used for educational purposes in
  16.  * association with the book, given proper citation if and when posted or
  17.  * reproduced.Any commercial use of this code requires the explicit written
  18.  * permission of the publisher, Addison-Wesley Professional, a division of
  19.  * Pearson Education, Inc. Send your request for permission, stating clearly
  20.  * what code you would like to use, and in what specific way, to the following
  21.  * address: 
  22.  * 
  23.  *  Pearson Education, Inc.
  24.  *  Rights and Contracts Department
  25.  *  75 Arlington Street, Suite 300
  26.  *  Boston, MA 02216
  27.  *  Fax: (617) 848-7047
  28. */ 
  29. #ifndef BASKET_H
  30. #define BASKET_H
  31. #include <iostream>
  32. #include <string>
  33. #include <set>
  34. #include <map>
  35. #include <utility>
  36. #include <cstddef>
  37. // Item sold at an undiscounted price
  38. // derived classes will define various discount strategies
  39. class Item_base {
  40. friend std::istream& operator>>(std::istream&, Item_base&);
  41. friend std::ostream& operator<<(std::ostream&, const Item_base&);
  42. public:
  43.     virtual Item_base* clone() const 
  44.                        { return new Item_base(*this); }
  45. public:
  46.     Item_base(const std::string &book = "", 
  47.               double sales_price = 0.0):
  48.                      isbn(book), price(sales_price) { }
  49.     std::string book() const { return isbn; }
  50.     // returns total sales price for a specified number of items
  51.     // derived classes will override and apply different discount algorithms
  52.     virtual double net_price(std::size_t n) const 
  53.                                    { return n * price; }
  54.     // no work, but virtual destructor needed 
  55.     // if base pointer that points to a derived object is ever deleted
  56.     virtual ~Item_base() { } 
  57. private:
  58.     std::string isbn;   // identifier for the item
  59. protected:
  60.     double price;       // normal, undiscounted price
  61. };
  62. // class to hold discount rate and quantity 
  63. // derived classes will implement pricing strategies using these data
  64. class Disc_item : public Item_base {
  65. public:
  66.     std::pair<size_t, double> discount_policy() const
  67.         { return std::make_pair(quantity, discount); }
  68.     // other members as before
  69.     double net_price(std::size_t) const = 0;
  70.     Disc_item(const std::string& book = "", 
  71.               double sales_price = 0.0, 
  72.               std::size_t qty = 0, double disc_rate = 0.0):
  73.                  Item_base(book, sales_price), 
  74.                  quantity(qty), discount(disc_rate) { }
  75. protected:
  76.     std::size_t quantity;  // purchase size for discount to apply
  77.     double discount;       // fractional discount to apply
  78. };
  79. // discount kicks in when a specified number of copies of same book are sold
  80. // the discount is expressed as a fraction to use to reduce the normal price
  81. class Bulk_item : public Disc_item {
  82. public:
  83.     std::pair<size_t, double> discount_policy() const
  84.         { return std::make_pair(quantity, discount); }
  85.     // other members as before
  86.     Bulk_item* clone() const 
  87.         { return new Bulk_item(*this); }
  88.     Bulk_item(const std::string& book = "", 
  89.               double sales_price = 0.0, 
  90.               std::size_t qty = 0, double disc_rate = 0.0):
  91.           Disc_item(book, sales_price, qty, disc_rate) { }
  92.     // redefines base version so as to implement bulk purchase discount policy
  93.     double net_price(std::size_t) const;
  94. };
  95. // discount (a fraction off list) for only a specified number of copies, 
  96. // additional copies sold at standard price
  97. class Lim_item : public Disc_item {
  98. public:
  99.     Lim_item(const std::string& book = "", 
  100.              double sales_price = 0.0,
  101.              std::size_t qty = 0, double disc_rate = 0.0):
  102.              Disc_item(book, sales_price, qty, disc_rate) { }
  103.     // redefines base version so as to implement limited discount policy
  104.     double net_price(std::size_t) const;
  105.     Lim_item* clone() const { return new Lim_item(*this); }
  106.     std::pair<size_t, double> discount_policy() const
  107.         { return std::make_pair(quantity, discount); }
  108. };
  109. // use counted handle class for the Item_base hierarchy 
  110. class Sales_item {
  111.     // let compare use the Item_base pointer to use Item_base compare function
  112.     // this friend is needed for the hidden call to print_total, 
  113.     friend class Basket;   
  114. public:
  115.     // default constructor: unbound handle
  116.     Sales_item(): p(0), use(new std::size_t(1)) { }
  117.     // attaches a handle to a copy of the Item_base object
  118.     Sales_item(const Item_base&); 
  119.     // copy control members to manage the use count and pointers
  120.     Sales_item(const Sales_item &i): p(i.p), use(i.use) 
  121.                                      { ++*use; }
  122.     ~Sales_item() { decr_use(); }
  123.     Sales_item& operator=(const Sales_item&);
  124.     // member access operators
  125.     const Item_base *operator->() const { return p; }
  126.     const Item_base &operator*() const { return *p; }
  127. private:
  128.     Item_base *p;         // pointer to shared item
  129.     std::size_t *use;     // pointer to shared use count
  130.     // called by both destructor and assignment operator to free pointers
  131.     void decr_use() 
  132.          { if (--*use == 0) {delete p; delete use;} }
  133. };
  134. bool compare(const Sales_item &, const Sales_item &);
  135. // holds items being purchased
  136. class Basket {
  137.     // type of the comparison function used to order the multiset
  138.     typedef bool (*Comp)(const Sales_item&, const Sales_item&);
  139.     std::multiset<Sales_item, Comp> items;
  140. public:
  141.     // useful typedefs modeled after corresponding container types
  142.     typedef std::multiset<Sales_item, Comp>::size_type size_type;
  143.     typedef std::multiset<Sales_item, Comp>::const_iterator const_iter;
  144.     // workaround MS compiler bug: must explicitly pass function address
  145.     Basket(): items(&compare) { }  // initialze the comparator
  146.     void display(std::ostream&) const;
  147.     void add_item(const Sales_item &item) 
  148.                         { items.insert(item); }
  149.     size_type size(const Sales_item &i) const
  150.                          { return items.count(i); }
  151.     double total() const;  // sum of net prices for all items in the basket
  152. };
  153. inline
  154. Sales_item::Sales_item(const Item_base &item):
  155.             p(item.clone()), use(new std::size_t(1)) { }
  156. // compare defines item ordering for the multiset in Basket
  157. inline bool 
  158. compare(const Sales_item &lhs, const Sales_item &rhs) 
  159. {
  160.     return lhs->book() < rhs->book();
  161. #endif