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()