15.3
1 class Quote 2 { 3 public 4 Quote()=default; 5 Quote(const std::string &book,double sales_price):bookNo(book), 6 price(sales_price){};//类的初始化 7 8 std::string isbn() const {return bookNo;} 9 10 virtual double net_price(std::size_t n) const 11 {return n*price;} 12 13 virtual ~Quote()=default; 14 15 private: 16 std::string bookNo; 17 18 protected: 19 double price; 20 }; 21 22 23 double print_total(ostream &os,const Quote &item,size_t n) 24 { 25 double ret=item.net_price(n); 26 os<<"ISBN: "<<item.isbn<<" #slod: "<<n<<"total due"<<ret<<end1; 27 return ret; 28 }
15.5
1 //定义Bulk_Quote类 2 classBulk_quote:public Quote 3 { 4 public net_price(size_t cnt) const override 5 { 6 if(cnt>=min_qty) 7 return cnt*(1-discount)*price; 8 else 9 return cnt*price; 10 } 11 12 private: 13 size_t cnt; 14 double discount; 15 }