c++_primer_exercise_1511_1512_1513_1514

Exercise 15.11:

/*****************************************************
* IDE:  VS2010
* FILE: Quote.h
*****************************************************/
#ifndef QUOTE_H
#define QUOTE_H

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

// item sold at an undiscounted price
// derived classes will define various discount strategies
class Quote
{
public:
	Quote() : price(0.0) { }
	Quote(const std::string &book, double sales_price) :
		book_no(book), price(sales_price) { }
	std::string isbn() const { return book_no; }
	// returns the total sales price for the specified number of items
	// derived classes will override and apply different discount algorithms
	virtual double net_price(std::size_t n) const
		{ return n * price; }
	virtual void debug(std::ostream &os) const;

	virtual ~Quote() { }
private:
	std::string book_no;	// ISBN number of this item
protected:
	double price;
};


// the discount kicks in when a specified number of copies of the same book are sold
// the discount is expressed as a fraction used to reduce the normal price
class Bulk_quote : public Quote	// Bulk_quote inherits from Quote
{
public:
	Bulk_quote() : min_qty(0), discount(0.0) { }
	Bulk_quote(const std::string &book, double price,
		std::size_t qty, double disc) :
		Quote(book, price), min_qty(qty), discount(disc) { }
	// overrides the base version in order to implement the bulk purchase discount policy
	double net_price(std::size_t cnt) const override;
	void debug(std::ostream &os) const override;
protected:
	std::size_t min_qty;	// minimum purchase for the discount to apply
	double discount;		// fractional discount to apply
};


// discount (a fraction off list) for only a specified number of copies
// additional copies sold at standard price
class Lim_quote : public Bulk_quote
{
public:
	Lim_quote(const std::string &book, double price,
		std::size_t qty, double disc) :
			Bulk_quote(book, price, qty, disc) { }
	double net_price(std::size_t cnt) const override;
};


double print_total(std::ostream &os, const Quote &item, std::size_t n);

#endif



/*****************************************************
* IDE:  VS2010
* FILE: Quote.cpp
*****************************************************/
#include "Quote.h"

#include 
      
      
       
       
using std::size_t;

#include 
       
       
         using std::ostream; using std::cout; using std::endl; #include 
        
          using std::min; #include 
         
           using std::string; // display data member of Quote void Quote::debug(std::ostream &os) const { os << "Quote--book_no: " << book_no << ", price: "< 
          
            << endl; } // if the specified number of items are purchased, use the discount price double Bulk_quote::net_price(size_t cnt) const { if (cnt >= min_qty) { return cnt * price * (1 - discount); } else { return cnt * price; } } // display data member of Quote void Bulk_quote::debug(std::ostream &os)const { os << "Bulk_quote--book_no: " << isbn() << ", price: "< 
           
             << ", min_qty: " << min_qty << ", discount: " << discount < 
            
              using std::cout; int main() { Quote base("0-201-82470-1", 50); base.debug(cout); print_total(cout, base, 10); // calls Quote::net_price Bulk_quote derived("0-201-82470-1", 50, 5, 0.19); derived.debug(cout); print_total(cout, derived, 10); Lim_quote lim("0-201-82470-1", 50, 5, 0.19); lim.debug(cout); print_total(cout, lim, 10); return 0; } 
             
            
           
          
         
       
      
      
     
     
    
    
   
   

Exercise 15.22:

useful. The final and override specifiers have different purpose.

The compiler will reject a program if a function marked override does not override an existing virtual function.

When we designate a function as final, any attempt to override a function that has been defined as final will be flagged as an error.


Exercise 15.13:

If a derived virtual function that intended to call its base-class version omits the scope operator, the call will be resolved at run time as a call to the derived version itself, resulting in an infinite recursion.

That's all i want to say about this exercise.


Exercise 15.14:

(a) base::print()    

(b) derived::print

(c) base::name()    

(d) derived::name()

(e) base::print()

(f) derived::print() 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值