未调试运行,待完善
1.OOP的基础
- 继承
- 动态绑定
- 数据抽象
2.书店应用程序案列
1.基类的定义
Exercise 15.4
哪些应该设计为virtual。
class Library{
public:
bool check_out(const LibMember&);
bool check_in(const LibMember&);
bool is_late(const Date& today);
double apply_fine();
ostream& print(ostream& = cout);
Date due_date() const;
Date date_borrowed() const;
string title() const;
const LibMember& member() const;
};
class Item_base{
public:
Item_base(const std::string &book = "",double sales_price = 0.0):
isbn(book),price(sales_price){ }
std::string book( ) const {return isbn;}
virtual double net_price(std::size_t n) const
{ return n*price;}
virtual ~Item_base() { }
private:
std::string isbn;
protected:
double price;
};
派生类只能通过派生类对象访问其基类的protected成员
void Bulk_item::memfcn(const Bulk_item &d,const Item_base &b)
{
double ret = price;
ret = d.price;
ret = b.price;//error
}
2.定义派生类
class Bulk_item:public Item_base
{
public:
double net_price(std::size_t) const;
private:
std::size_t min_qty;
double discount;
};