本例子是c++ primer书中第15章面向对象编程的一个贯穿全章的例子:
#include <iostream>
#include <stdexcept>
#include <set>
using namespace std;
/*
Item_base类定义了book和net_price函数并且需要存储每本书的ISBN和标准价格
*/
class Item_base {
public:
Item_base(const string &name = "", double normal_price = 0.0):ISBN(name), price(normal_price) {}
string book() const {return ISBN;}
virtual double net_price(size_t n) const{
return n*price;
}
virtual Item_base* clone() const {
return new Item_base(*this);//默认调用拷贝构造函数
}
//调试信息,方便以后调试
virtual void debug(ostream &os = cout) const {
os << ISBN << "\t" << price << endl;
}
virtual ~Item_base() {}
private:
string ISBN;
protected:
double price;
};
//重构
class Disc_item:public Item_base {
public:
Disc_it