练习15.4
a)错误,一个类不能派生它本身
b)正确,Derived从他的基类Base派生,且规定派生类从基类继承的数据成员对于派生类的用户是不可见。
c)错误,派生类的声明与其他类相差不大,声明中包含类名但是不包含他的派生列表;
练习15.5
1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() = default; 12 Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) {} 13 string isbn() { return bookNo; } 14 virtual double net_price(size_t n) const{ return n * price; } 15 virtual ~Quote() = default; 16 private: 17 string bookNo; 18 protected: 19 double price = 0.0; 20 }; 21 22 class Bulk_quote : public Quote { 23 public: 24 Bulk_quote() = default; 25 Bulk_quote(const string& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {} 26 double net_price(size_t n) const; 27 private: 28 size_t min_qty = 0; 29 double discount = 0.0; 30 }; 31 32 double print_total(ostream& os, const Quote& item, size_t t); 33 34 int main() 35 { 36 37 system("pause"); 38 return 0; 39 } 40 41 double print_total(ostream & os, const Quote &item, size_t t) 42 { 43 double ret = item.net_price(t); 44 os << "ISBN: " << item.isbn << " # sold: " << t << " total due: " << ret << endl; 45 return ret; 46 } 47 48 double Bulk_quote::net_price(size_t n) const 49 { 50 if (n >= min_qty) 51 return n * (1 - discount) * price; 52 else 53 return n * price; 54 }
练习15.6
1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() = default; 12 Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) {} 13 const string& isbn() const { return bookNo; } 14 virtual double net_price(size_t n) const{ return n * price; } 15 virtual ~Quote() = default; 16 private: 17 string bookNo; 18 protected: 19 double price = 0.0; 20 }; 21 22 class Bulk_quote : public Quote { 23 public: 24 Bulk_quote() = default; 25 Bulk_quote(const string& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {} 26 double net_price(size_t n) const; 27 private: 28 size_t min_qty = 0; 29 double discount = 0.0; 30 }; 31 32 double print_total(ostream& os, const Quote& item, size_t t); 33 34 int main() 35 { 36 Quote q1; 37 Quote q2("C++ primer", 128.00); 38 Bulk_quote b1; 39 Bulk_quote b2("C++ primer", 128.00, 5, 0.4); 40 print_total(cout, q1, 6); 41 print_total(cout, q2, 6); 42 print_total(cout, b1, 6); 43 print_total(cout, b2, 6); 44 system("pause"); 45 return 0; 46 } 47 48 double print_total(ostream & os, const Quote &item, size_t t) 49 { 50 double ret = item.net_price(t); 51 os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl; 52 return ret; 53 } 54 55 double Bulk_quote::net_price(size_t n) const 56 { 57 if (n >= min_qty) 58 return n * (1 - discount) * price; 59 else 60 return n * price; 61 }
练习15.7
1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() = default; 12 Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) {} 13 const string& isbn() const { return bookNo; } 14 virtual double net_price(size_t n) const{ return n * price; } 15 virtual ~Quote() = default; 16 private: 17 string bookNo; 18 protected: 19 double price = 0.0; 20 }; 21 22 class Bulk_quote : public Quote { 23 public: 24 Bulk_quote() = default; 25 Bulk_quote(const string& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {} 26 double net_price(size_t n) const; 27 private: 28 size_t min_qty = 0; 29 double discount = 0.0; 30 }; 31 32 class lim_discount : public Quote { 33 public: 34 lim_discount() = default; 35 lim_discount(const string& book, double p, size_t lcount, double ldis) : Quote(book, p), lim_count(lcount), lim_dis(ldis) {} 36 double net_price(size_t n) const override; 37 private: 38 size_t lim_count = 0; 39 double lim_dis = 0.0; 40 }; 41 double print_total(ostream& os, const Quote& item, size_t t); 42 43 int main() 44 { 45 Quote q1; 46 Quote q2("C++ primer", 128.00); 47 Bulk_quote b1; 48 Bulk_quote b2("C++ primer", 128.00, 5, 0.4); 49 print_total(cout, q1, 6); 50 print_total(cout, q2, 6); 51 print_total(cout, b1, 6); 52 print_total(cout, b2, 6); 53 lim_discount l1("C++ primer", 128.00, 10, 0.5); 54 print_total(cout, l1, 9); 55 print_total(cout, l1, 11); 56 system("pause"); 57 return 0; 58 } 59 60 double print_total(ostream & os, const Quote &item, size_t t) 61 { 62 double ret = item.net_price(t); 63 os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl; 64 return ret; 65 } 66 67 double Bulk_quote::net_price(size_t n) const 68 { 69 if (n >= min_qty) 70 return n * (1 - discount) * price; 71 else 72 return n * price; 73 } 74 75 double lim_discount::net_price(size_t n) const 76 { 77 if (n <= lim_count) 78 return n * (1 - lim_dis) * price; 79 else 80 return Quote::net_price(n); 81 }
时间: 2024-10-05 04:40:36