C++primer 15.2.1节练习

练习15.1

基类将类型相关的函数与派生类不做改变直接继承的函数区别对待,对于某些函数,基类希望他的派生类个自定义适合自身的版本,此时基类就将这些函数声明成虚函数。

练习15.2

protected:允许类的派生类访问其成员,而不允许其他用户访问

private:禁止所有用户包括其派生类访问其私有成员

练习15.3

 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <memory>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 class Quote {
10 public:
11     Quote() = default;
12     Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) {}
13     string isbn() { return bookNo; }
14     virtual double net_price(size_t n) const{ return n * price; }
15     virtual ~Quote() = default;
16 private:
17     string bookNo;
18 protected:
19     double price = 0.0;
20 };
21 
22 double print_total(ostream& os, const Quote& item, size_t t);
23 
24 int main()
25 {
26     
27     system("pause");
28     return 0;
29 }
30 
31 double print_total(ostream & os, const Quote &item, size_t t)
32 {
33     double ret = item.net_price(t);
34     os << "ISBN: " << item.isbn << " # sold: " << t << " total due: " << ret << endl;
35     return ret;
36 }

 

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7517575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值