c++_primer_exercise_1504_1505_1506_1507

Exercise 15.4:

(a) incorrect. it is impossible to derive a class from itself. in other words, a class must be defined, not just declared, before we can use it as a base class.


Exercise 15.5_15.6_15.7

/*****************************************************
* 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 ~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;
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; // 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; } } // use discounted price for up to a specified number of items // additional items priced at normal, undiscounted price double Lim_quote::net_price(size_t cnt) const { size_t discounted = min(cnt, min_qty); size_t undiscounted = cnt - discounted; return discounted * price * (1 - discount) + undiscounted * price; } // calculate and print the price for the given number of copies // apply any discounts double print_total(ostream &os, const Quote &item, size_t n) { double ret = item.net_price(n); os << "ISBN: " << item.isbn() // calls Quote::isbn << " # sold: " << n << " totol due: " << ret << endl; return ret; } /***************************************************** * IDE: VS2010 * FILE: use_Quote.cpp *****************************************************/ #include "Quote.h" #include 
          
            using std::cout; int main() { Quote base("0-201-82470-1", 50); print_total(cout, base, 10); // calls Quote::net_price Bulk_quote derived("0-201-82470-1", 50, 5, 0.19); print_total(cout, derived, 10); Lim_quote lim("0-201-82470-1", 50, 5, 0.19); print_total(cout, lim, 10); return 0; } 
           
          
         
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值