C++ Primer 第五版 第十五章 练习题编程题目答案

https://github.com/jzplp/Cpp-Primer-Answer

  1. 把一个基类对象的指针绑定到派生类对象上
  2. 把一个基类对象的引用绑定到派生类的对象上
  3. 用一个派生类对象初始化基类对象
  • 练习15.10
    read函数接受一个动态类型为ifstream的对象,然后将它绑定在它的基类istream的引用上。

  • 练习15.11
    Quote类和print_total函数 书上的版本
    增加了debug虚函数
    15.11 Quote.h程序代码
    15.11 Quote.cpp程序代码
    15.11 测试程序代码

  • 练习15.12
    有必要,不允许后续的派生类再覆盖这个虚函数

  • 练习15.13
    有问题,会造成函数无限递归。
    修改:

derived类中的print函数:
void print(ostream & os) override
{
    this->base::print(os);
    os << " " << i;
}
C:\Users\jz\Desktop\1.cpp(9): error C2259: “Disc_quote”: 不能实例化抽象类
C:\Users\jz\Desktop\1.cpp(9): note: 由于下列成员:
C:\Users\jz\Desktop\1.cpp(9): note: “double Disc_quote::net_price(std::size_t) const”: 是抽象的
c:\users\jz\desktop\Quote.h(32): note: 参见“Disc_quote::net_price”的声明
  • 练习15.18
Base *p = &d1;
合法
p = &d2;
不合法
p = &d3;
不合法
p = &dd1;
合法
p = &dd2;
不合法
p = &dd3;
不合法
class D1 : public Base
{
public:
    using Base::int;
    int fcn(int);
    virtual void f2();
};

本节的调用语句没有变化

(a)
Query(const std::string &);
Query_base();
WordQuery(const std::string &);
BinaryQuery(const Query &l, const Query &r, std::strin s);
AndQuery(const Query &left, const Query &right);
OrQuery(const Query &left, const Query &right);
(b)
Query::rep
WordQuery::rep
BinaryQuery::rep
(c)
Query::eval
WordQuery::eval
AndQuery::eval
OrQuery::eval
BinaryQuery a = Query("fiery") & Query("Brid");
不合法,BinaryQuery是抽象基类,不能定义BinaryQuery类型的对象
AndQuery b = Query("fiery") & Query("Brid");
合法
OrQuery b = Query("fiery") & Query("Brid");
不合法, & 运算符的结果生成一个AndQuery类型对象,与OrQuery没有转换关系
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值