-
Notifications
You must be signed in to change notification settings - Fork 33
/
15.11.cpp
117 lines (105 loc) · 3.16 KB
/
15.11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Exercise 15.11: Add a virtual debug function to your Quote class hierarchy
* that displays the data members of the respective classes.
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <string>
class Quote {
friend double print_total(std::ostream &, const Quote &, std::size_t);
public:
Quote() = default;
Quote(const std::string &book, double sales_price) :
bookNo(book), price(sales_price) { }
virtual ~Quote() = default;
std::string isbn() const { return bookNo; }
virtual double net_price(std::size_t n) const { return n * price; }
virtual std::ostream& debug(std::ostream &os = std::cout, const std::string &step = ", ") const;
private:
std::string bookNo;
protected:
double price = 0.0;
};
inline
double print_total(std::ostream &os, const Quote &item, std::size_t n)
{
double ret = item.net_price(n);
os << "ISBN: " << item.isbn()
<< " # sold: " << n << " total due: " << ret << std::endl;
return ret;
}
inline
std::ostream&
Quote::debug(std::ostream &os, const std::string &step) const
{
return os << "{\'bookNo\': \'" << bookNo << "\'" << step << "\'price\': \'" << price << "\'}";
};
class Bulk_quote : public Quote {
public:
Bulk_quote() = default;
Bulk_quote(const std::string &, double, std::size_t, double);
double net_price(std::size_t) const override;
std::ostream& debug(std::ostream &os = std::cout, const std::string &step = ", ") const override;
private:
std::size_t min_qty = 0;
double discount = 0.0;
};
inline
Bulk_quote::Bulk_quote(const std::string &book, double p,
std::size_t qty, double disc) :
Quote(book, p), min_qty(qty), discount(disc) {}
inline
double
Bulk_quote::net_price(std::size_t cnt) const
{
if (cnt >= min_qty)
return cnt * (1 - discount) * price;
return cnt * price;
}
inline
std::ostream&
Bulk_quote::debug(std::ostream &os, const std::string &step) const
{
return Quote::debug(os, step) << '\b' << step << "\'min_qt\': " << min_qty
<< step << "\'discount\': " << discount << '}';
}
class Limited_discount : public Quote {
public:
Limited_discount() = default;
Limited_discount(const std::string &book, double p, std::size_t lim, double disc)
: Quote(book, p), limit(lim), discount(disc) { }
double net_price(std::size_t) const override;
std::ostream& debug(std::ostream &os = std::cout, const std::string &step = ", ") const override;
private:
std::size_t limit = 0;
double discount = 0.0;
};
inline
double
Limited_discount::net_price(std::size_t cnt) const
{
if (cnt > limit) {
auto discounted = limit * (1 - discount) * price;
auto regularPrice = (cnt - limit) * price;
return discounted + regularPrice;
}
return cnt * (1 - discount) * price;
}
inline
std::ostream&
Limited_discount::debug(std::ostream &os, const std::string &step) const
{
return Quote::debug(os, step) << '\b' << step << "\'limit\': " << limit
<< step << "\'discount\': " << discount << '}';
}
int main()
{
Quote base("2-222222-2", 25);
base.debug() << '\n';
Bulk_quote derived1("3-333333-3", 35, 10, 0.25);
derived1.debug(std::cout) << '\n';
Limited_discount derived2("4-444444-4", 42, 10, 0.5);
derived2.debug(std::cerr) << '\n';
return 0;
}