#include <iostream> #include<vector> #include<string> using namespace std; class Quote { public: Quote() = default; Quote(const string &book,double sale_price):booNo(book),price(sale_price){} const string& isbn()const { return booNo; } virtual double net_price(size_t n)const { return n * price; } virtual ~Quote() = default; private: string booNo; protected: double price = 0.0; }; class Dis_quote :public Quote { public: Dis_quote() = default; Dis_quote(const string& book, double price, size_t qty, double disc): Quote(book,price),quantity(qty),discount(disc) { } //纯虚函数 double net_price(size_t)const = 0; protected: size_t quantity = 0;//折扣适用的购买量 double discount = 0.0; //表示折扣的小数值 }; //当同一书籍的销售量超过某个值启动折扣 //折扣的值是一个小于1的正的小数值,以此来降低正常销售价格 class Bulk_quote : public Dis_quote { public: using Dis_quote::Dis_quote;//继承Disc_quote的构造函数 //覆盖基类的函数版本以实现一种新的折扣策略 double net_price(size_t n)const override { if (n >= quantity) { return n * (1 - discount) * price; } else { return n * price; } } }; double print_total(ostream& os, const Quote& item, size_t n) { double ret = item.net_price(n); os << "ISBM: " << item.isbn() << "# sold: " << n << "total due: " << ret << endl; return ret; } int main() { Bulk_quote a("asb-11-3", 50, 10, 0.25); print_total(cout, a, 15); vector<Quote>basket; basket.push_back(Quote("0-201-82470-1", 50)); basket.push_back(Bulk_quote("0-201-54848-1", 50,10,0.25)); cout << basket.back().net_price(15) << endl; vector<shared_ptr<Quote>>baskett; baskett.push_back(make_shared<Quote>("0-201-6412321=1", 50)); baskett.push_back(make_shared<Bulk_quote>("0-201-6412321=1", 50,10,0.25)); cout << baskett.back()->net_price(15) << endl; }
vector<Quote>basket; basket.push_back(Quote("0-201-82470-1", 50)); basket.push_back(Bulk_quote("0-201-54848-1", 50,10,0.25)); cout << basket.back().net_price(15) << endl; 为什么打印的是750,首先我们知道Bulk_quote对象有三个部分,一部分是Quote,一部分是DIs,一部分是Bulk 前面说过,Quote对象不能转换成Bulk_quote,而Bulk调用的是一部分Quote的函数,而Bulk的派生类部分直接给**忽略掉**
注意:派生类对象被赋值给基类对象时,其中的派生类部分将被切掉,容器和存在继承关系的类型无法兼容
vector<shared_ptr<Quote>>baskett; baskett.push_back(make_shared<Bulk_quote>("0-201-6412321=1", 50,10,0.25)); cout << baskett.back()->net_price(15) << endl; 可以将派生类的普通指针转换成基类指针,也可以将派生类的智能指针转换成基类的智能指针 mak_shared<Bulk_quote>返回一个shared_prt<Bulk_quote>对象,当调用是被转换成shared_prt<Quote>
15.8节练习
Bulk_quote a("asb-11-3", 50, 10, 0.25); print_total(cout, a, 20); vector<Quote>basket; basket.push_back(Quote("0-201-82470-1", 50)); basket.push_back(Bulk_quote("0-201-54848-1", 50,10,0.25)); double item = 0; for (auto &p : basket) { item += p.net_price(20); } cout << item << endl; // cout << basket.back().net_price(15) << endl; vector<shared_ptr<Quote>>baskett; baskett.push_back(make_shared<Quote>("0-201-6412321=1", 50)); baskett.push_back(make_shared<Bulk_quote>("0-201-6412321=1", 50,10,0.25)); //cout << baskett.back()->net_price(15) << endl; double itemm = 0; for (auto &p : baskett) { itemm += p->net_price(20); } cout << itemm << endl;
前者不一致,basket的元素是Quote对象,当向它添加一个Bulk_quote对象时,它的派生类将被忽略掉,所以它是用Quote的
net_price函数使用,baskett是智能指针,可以把派生类智能指针转换成基类智能指针,使用它用的是Bulk_quote的net_price函数