c++面向对象编程的一个例子(书店)

这篇博客通过C++ Primer书中第15章的书店案例,详细讲解了面向对象编程的概念,包括类的设计、继承与多态的实现,以及句柄类的应用。读者将能从中了解到如何在实际编程中运用面向对象的思想。
摘要由CSDN通过智能技术生成

本例子是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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值