C++ Primer(第5版) 练习 15.5
练习 15.5 定义你自己的Bulk_quote类。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex15.3.cpp
> Author:
> Mail:
> Created Time: Fri 12 Jul 2024 09:13:44 AM CST
************************************************************************/
#include<iostream>
using namespace std;
class Quote{
public:
Quote() = default;
Quote(const std::string &book, double sales_price): bookNo(book), price(sales_price) {}
std::string isbn() const { return bookNo; }
virtual double net_price(std::size_t n) const { return n * price; }
virtual ~Quote() = default;
private:
std::string bookNo;
protected:
double price = 0.0;
};
double print_total(ostream &os, const Quote &item, size_t n){
double ret = item::net_price(n);
os<<"ISBN: "<<item.isbn()<<" # sold: "<<n<<" total due: "<<ret<<endl;
return ret;
}
class Bulk_quote: public Quote{
public:
Bulk_quote() = default;
Bulk_quote(const std::string& book, double p, std::size_t qty, double disc): Quote(book, p), min_qty(qty), discount(disc) {}
double net_price(std::size_t) const override;
private:
std::size_t min_qty = 0;
double discount = 0.0;
};
double Bulk_quote::net_price(size_t cnt) const{
if (cnt >= min_qty){
return cnt * (1 - discount) * price;
}
else{
return ret * price;
}
}
标签:std,const,15.5,quote,price,C++,Quote,double,size
From: https://blog.csdn.net/navicheung/article/details/140372468