C++ primer 第五版 第十五章练习答案

第15章 面向对象程序设计

15.3

#include <iostream>
#include <string>
using namespace std;
class Quote {
public:
    Quote() = default;
    Quote(const string &book,double sales_price):bookNo(book),price(sales_price){}
    string isbn()const { return bookNo; }
    virtual double net_price(size_t n)const { return n*price; }
    virtual ~Quote() = default;
private:
    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;
}

15.5定义你自己的Bulk_Quoto类

#include <iostream>
#include <string>
using namespace std;
class Quote {
public:
    Quote() = default;
    Quote(const string &book,double sales_price):bookNo(book),price(sales_price){}
    string isbn()const { return bookNo; }
    virtual double net_price(size_t n)const { return n*price; }
    virtual ~Quote() = default;
private:
    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 string&, double, size_t, double);
    double net_price(size_t n)const override;
private:
    size_t min_qty = 0;
    double discount = 0.0;
};
Bulk_Quote::Bulk_Quote(const string& book, double p, size_t qty, double disc):
    Quote(book,p),min_qty(qty),discount(disc){}//Constructor
double Bulk_Quote::net_price(size_t Cnt)const {
    if (Cnt >= min_qty)
        return Cnt*(1 - discount)*price;
    else return Cnt*price;
}

15.7

#include <iostream>
#include <string>
using namespace std;
class Quote {
public:
    Quote() = default;
    Quote(const string &book,double sales_price):bookNo(book),price(sales_price){}
    string isbn()const { return bookNo; }
    virtual double net_price(size_t n)const { return n*price; }
    virtual double more_price(size_t n)const { return n*price; }
    virtual ~Quote() = default;
private:
    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;
}
double print_other(ostream &os, const Quote &item, size_t n) {
    double ret = item.more_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 string&, double, size_t, double);
    double net_price(size_t n)const override;
    double more_price(size_t n)const override;
private:
    size_t min_qty = 0;
    double discount = 0.0;
};
Bulk_Quote::Bulk_Quote(const string& book, double p, size_t qty, double disc):
    Quote(book,p),min_qty(qty),discount(disc){}//Constructor
double Bulk_Quote::more_price(size_t Cnt)const {
    if (Cnt <= min_qty)
        return Cnt*(1 - discount)*price;
    else
        return min_qty*(1 - discount)*price + (Cnt - min_qty)*price;
}
double Bulk_Quote::net_price(size_t Cnt)const {
    if (Cnt >= min_qty)
        return Cnt*(1 - discount)*price;
    else return Cnt*price;
}
int main() {
    Quote boss("11", 20);
    Bulk_Quote mm("sd", 30, 2, 0.2);
    //print_total(cout, mm, 2);
    print_other(cout,mm,4);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值