第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){}
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){}
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_other(cout,mm,4);
}