练习15.28
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() : bookNo(""), price(0.0) { cout << "默认构造函数" << endl; } 12 Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) { cout << "构造函数"<< endl; } 13 Quote(const Quote& q) : bookNo(q.bookNo), price(q.price) { cout << "拷贝构造函数" << endl; } 14 Quote(Quote &&q) noexcept : bookNo(q.bookNo), price(q.price) { cout << "移动构造函数" << endl; } 15 Quote& operator=(const Quote& rhs); 16 Quote& operator=(Quote&& rhs) noexcept; 17 const string& isbn() const { return bookNo; } 18 virtual double net_price(size_t n) const { return n * price; } 19 virtual ~Quote() = default; 20 private: 21 string bookNo; 22 protected: 23 double price = 0.0; 24 }; 25 26 class Disc_quote : public Quote { 27 public: 28 Disc_quote() : Quote() { cout << "派生类默认构造函数" << endl; } 29 Disc_quote(const string &book, double sales_price, size_t quan, double dis) : Quote(book, sales_price), quantity(quan), discount(dis) { cout << "派生类构造函数" <<endl; } 30 Disc_quote(const Disc_quote& d) : Quote(d), quantity(d.quantity), discount(d.discount) { cout << "派生类拷贝构造函数" <<endl; } 31 Disc_quote(Disc_quote&& d) : Quote(std::move(d)), quantity(d.quantity), discount(d.discount) { cout << "派生类移动构造函数" << endl; } 32 Disc_quote& operator=(const Disc_quote& rhs); 33 Disc_quote& operator=(Disc_quote&& rhs); 34 double net_price(size_t n) const override; 35 ~Disc_quote() { cout << "派生类析构函数" << endl; } 36 protected: 37 size_t quantity = 0; 38 double discount = 0.0; 39 }; 40 41 class Bulk_quote : public Disc_quote { 42 public: 43 Bulk_quote() = default; 44 Bulk_quote(const string& book, double p, size_t qty, double disc) : Disc_quote(book, p, qty, disc) {} 45 double net_price(size_t n) const override; 46 }; 47 48 class lim_discount : public Disc_quote { 49 public: 50 using Disc_quote::Disc_quote; 51 double net_price(size_t n) const override; 52 }; 53 54 double print_total(ostream& os, const Quote& item, size_t t); 55 56 int main() 57 { 58 vector<shared_ptr<Quote>> basket; 59 basket.push_back(make_shared<Quote>("C++ primer", 128.00)); 60 basket.push_back(make_shared<Bulk_quote>("C++ plus", 135.2, 15, 0.6)); 61 Bulk_quote b1("C plus", 102.3, 10, 0.9); 62 basket.push_back(make_shared<Bulk_quote>(b1)); 63 cout << basket.back()->net_price(15) << endl; 64 system("pause"); 65 return 0; 66 } 67 68 double print_total(ostream & os, const Quote &item, size_t t) 69 { 70 double ret = item.net_price(t); 71 os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl; 72 return ret; 73 } 74 75 double Bulk_quote::net_price(size_t n) const 76 { 77 if (n >= quantity) 78 return n * (1 - discount) * price; 79 else 80 return n * price; 81 } 82 83 double lim_discount::net_price(size_t n) const 84 { 85 if (n <= quantity) 86 return n * (1 - discount) * price; 87 else 88 return Quote::net_price(n); 89 } 90 91 Quote & Quote::operator=(const Quote & rhs) 92 { 93 bookNo = rhs.bookNo; 94 price = rhs.price; 95 cout << "拷贝赋值运算符" << endl; 96 return *this; 97 // TODO: 在此处插入 return 语句 98 } 99 100 Quote & Quote::operator=(Quote && rhs) noexcept 101 { 102 bookNo = rhs.bookNo; 103 price = rhs.price; 104 cout << "移动赋值运算符" << endl; 105 return *this; 106 // TODO: 在此处插入 return 语句 107 } 108 109 Disc_quote & Disc_quote::operator=(const Disc_quote & rhs) 110 { 111 Quote::operator=(rhs); 112 quantity = rhs.quantity; 113 discount = rhs.discount; 114 cout << "派生类的拷贝构造运算符" << endl; 115 return *this; 116 // TODO: 在此处插入 return 语句 117 } 118 119 Disc_quote & Disc_quote::operator=(Disc_quote && rhs) 120 { 121 Quote::operator=(rhs); 122 quantity = rhs.quantity; 123 discount = rhs.discount; 124 cout << "派生类移动构造运算符" << endl; 125 return *this; 126 // TODO: 在此处插入 return 语句 127 } 128 129 double Disc_quote::net_price(size_t n) const 130 { 131 if (n >= quantity) 132 return n * (1 - discount) * price; 133 else 134 return n * price; 135 }
练习15.29
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() : bookNo(""), price(0.0) { cout << "默认构造函数" << endl; } 12 Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) { cout << "构造函数"<< endl; } 13 Quote(const Quote& q) : bookNo(q.bookNo), price(q.price) { cout << "拷贝构造函数" << endl; } 14 Quote(Quote &&q) noexcept : bookNo(q.bookNo), price(q.price) { cout << "移动构造函数" << endl; } 15 Quote& operator=(const Quote& rhs); 16 Quote& operator=(Quote&& rhs) noexcept; 17 const string& isbn() const { return bookNo; } 18 virtual double net_price(size_t n) const { return n * price; } 19 virtual ~Quote() = default; 20 private: 21 string bookNo; 22 protected: 23 double price = 0.0; 24 }; 25 26 class Disc_quote : public Quote { 27 public: 28 Disc_quote() : Quote() { cout << "派生类默认构造函数" << endl; } 29 Disc_quote(const string &book, double sales_price, size_t quan, double dis) : Quote(book, sales_price), quantity(quan), discount(dis) { cout << "派生类构造函数" <<endl; } 30 Disc_quote(const Disc_quote& d) : Quote(d), quantity(d.quantity), discount(d.discount) { cout << "派生类拷贝构造函数" <<endl; } 31 Disc_quote(Disc_quote&& d) : Quote(std::move(d)), quantity(d.quantity), discount(d.discount) { cout << "派生类移动构造函数" << endl; } 32 Disc_quote& operator=(const Disc_quote& rhs); 33 Disc_quote& operator=(Disc_quote&& rhs); 34 double net_price(size_t n) const override; 35 ~Disc_quote() { cout << "派生类析构函数" << endl; } 36 protected: 37 size_t quantity = 0; 38 double discount = 0.0; 39 }; 40 41 class Bulk_quote : public Disc_quote { 42 public: 43 Bulk_quote() = default; 44 Bulk_quote(const string& book, double p, size_t qty, double disc) : Disc_quote(book, p, qty, disc) {} 45 double net_price(size_t n) const override; 46 }; 47 48 class lim_discount : public Disc_quote { 49 public: 50 using Disc_quote::Disc_quote; 51 double net_price(size_t n) const override; 52 }; 53 54 double print_total(ostream& os, const Quote& item, size_t t); 55 56 int main() 57 { 58 /*vector<shared_ptr<Quote>> basket; 59 basket.push_back(make_shared<Quote>("C++ primer", 128.00)); 60 basket.push_back(make_shared<Bulk_quote>("C++ plus", 135.2, 15, 0.6)); 61 Bulk_quote b1("C plus", 102.3, 10, 0.9); 62 basket.push_back(make_shared<Bulk_quote>(b1)); 63 cout << basket.back()->net_price(15) << endl;*/ 64 vector<Quote> basket; 65 basket.push_back(Quote("C++ primer", 128.2)); 66 basket.push_back(Bulk_quote("C plus", 102.3, 10, 0.9)); 67 cout << basket.back().net_price(15) << endl; 68 system("pause"); 69 return 0; 70 } 71 72 double print_total(ostream & os, const Quote &item, size_t t) 73 { 74 double ret = item.net_price(t); 75 os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl; 76 return ret; 77 } 78 79 double Bulk_quote::net_price(size_t n) const 80 { 81 if (n >= quantity) 82 return n * (1 - discount) * price; 83 else 84 return n * price; 85 } 86 87 double lim_discount::net_price(size_t n) const 88 { 89 if (n <= quantity) 90 return n * (1 - discount) * price; 91 else 92 return Quote::net_price(n); 93 } 94 95 Quote & Quote::operator=(const Quote & rhs) 96 { 97 bookNo = rhs.bookNo; 98 price = rhs.price; 99 cout << "拷贝赋值运算符" << endl; 100 return *this; 101 // TODO: 在此处插入 return 语句 102 } 103 104 Quote & Quote::operator=(Quote && rhs) noexcept 105 { 106 bookNo = rhs.bookNo; 107 price = rhs.price; 108 cout << "移动赋值运算符" << endl; 109 return *this; 110 // TODO: 在此处插入 return 语句 111 } 112 113 Disc_quote & Disc_quote::operator=(const Disc_quote & rhs) 114 { 115 Quote::operator=(rhs); 116 quantity = rhs.quantity; 117 discount = rhs.discount; 118 cout << "派生类的拷贝构造运算符" << endl; 119 return *this; 120 // TODO: 在此处插入 return 语句 121 } 122 123 Disc_quote & Disc_quote::operator=(Disc_quote && rhs) 124 { 125 Quote::operator=(rhs); 126 quantity = rhs.quantity; 127 discount = rhs.discount; 128 cout << "派生类移动构造运算符" << endl; 129 return *this; 130 // TODO: 在此处插入 return 语句 131 } 132 133 double Disc_quote::net_price(size_t n) const 134 { 135 if (n >= quantity) 136 return n * (1 - discount) * price; 137 else 138 return n * price; 139 }
两者不一致,basket的元素是Quote对象,因此我们像该vector中添加一个Bulk_quote对象时,他的派生类部分将被忽略掉,所以当我们使用容器存放继承体系中的对象时,通常必须采取间接存储的方式。因为不允许在容器中保存不同类型的元素,所以我们不能把具有继承关系的多种类型的对象直接存放在容器之中。
原文:http://www.cnblogs.com/wuyinfenghappy/p/7542718.html