文章目录
- 练习15.11
- 练习15.12
- 练习15.13
- 练习15.14
- 练习15.15
-
- 练习15.16
- 练习15.17
- 练习15.18
- 练习15.19
- 练习15.20
练习15.11
为你的 Quote 类体系添加一个名为 debug 的虚函数,令其分别显示每个类的数据成员。
void Quote::debug() const
{
std::cout << "data members of this class:\n"
<< "bookNo= " <<this->bookNo << " "
<< "price= " <<this->price<< " ";
}
练习15.12
有必要将一个成员函数同时声明成 override 和 final 吗?为什么?
有必要。override 的含义是重写基类中相同名称的虚函数,final 是阻止它的派生类重写当前虚函数。
练习15.13
给定下面的类,解释每个 print 函数的机理:
class base {
public:
string name() { return basename;}
virtual void print(ostream &os) { os << basename; }
private:
string basename;
};
class derived : public base {
public:
void print(ostream &os) { print(os); os << " " << i; }
private:
int i;
};
在上述代码中存在问题吗?如果有,你该如何修改它?
有问题。应该改为:
void print(ostream &os) override { base::print(os); os << " derived\n " << i; }
练习15.14
给定上一题中的类以及下面这些对象,说明在运行时调用哪个函数:
base bobj; base *bp1 = &bobj; base &br1 = bobj;
derived dobj; base *bp2 = &dobj; base &br2 = dobj;
(a) bobj.print(); (b)dobj.print(); (c)bp1->name();
(d)bp2->name(); (e)br1.print(); (f)br2.print();
- (a) 编译时。
- (b) 编译时。
- © 编译时。
- (d) 编译时。
- (e) 运行时。base::print()
- (f) 运行时。derived::print()
练习15.15
定义你自己的 Disc_quote 和 Bulk_quote。
Disc_quote.h
#ifndef DISC_QUOTE_H
#define DISC_QUOTE_H
#include "exercise15_5.h"
class Disc_quote : public Quote
{
public:
Disc_quote();
Disc_quote(const std::string& b, double p, std::size_t q, double d) :
Quote(b, p), quantity(q), discount(d)
{}
virtual double net_price(std::size_t n) const override = 0;
protected:
std::size_t quantity;
double discount;
};
#endif // DISC_QUOTE_H
Bulk_quote.h
#ifndef BULK_QUOTE_H_
#define BULK_QUOTE_H_
#include "Disc_quote.h"
class Bulk_quote : public Disc_quote
{
public:
Bulk_quote() = default;
Bulk_quote(const std::string& b, double p, std::size_t q, double disc) :
Disc_quote(b, p, q, disc)
{}
double net_price(std::size_t n) const override;
};
#endif // BULK_QUOTE_H
练习15.16
改写你在15.2.2节练习中编写的数量受限的折扣策略,令其继承 Disc_quote。
#ifndef LIMIT_QUOTE_H
#define LIMIT_QUOTE_H
#include "disc_quote.h"
class Limit_quote : public Disc_quote
{
public:
Limit_quote() = default;
Limit_quote(const std::string& b, double p, std::size_t max, double disc) :
Disc_quote(b, p, max, disc)
{}
double net_price(std::size_t n) const override
{
return n * price * (n < quantity ? 1 - discount : 1);
}
};
#endif // LIMIT_QUOTE_H
练习15.17
尝试定义一个 Disc_quote 的对象,看看编译器给出的错误信息是什么?
error: cannot declare variable ‘d’ to be of abstract type ‘Disc_quote’: Disc_quote d;
练习15.18
假设给定了第543页和第544页的类,同时已知每个对象的类型如注释所示,判断下面的哪些赋值语句是合法的。解释那些不合法的语句为什么不被允许。
Base *p = &d1; //d1 的类型是 Pub_Derv
p = &d2; //d2 的类型是 Priv_Derv
p = &d3; //d3 的类型是 Prot_Derv
p = &dd1; //dd1 的类型是 Derived_from_Public
p = &dd2; //dd2 的类型是 Derived_from_Private
p = &dd3; //dd3 的类型是 Derived_from_Protected
- Base *p = &d1; 合法
- p = &d2; 不合法
- p = &d3; 不合法
- p = &dd1; 合法
- p = &dd2; 不合法
- p = &dd3; 不合法
练习15.19
假设543页和544页的每个类都有如下形式的成员函数:
void memfcn(Base &b) { b = *this; }
对于每个类,分别判断上面的函数是否合法。
合法:
- Pub_Derv
- Priv_Derv
- Prot_Derv
- Derived_from_Public
- Derived_from_Protected 不合法:
- Derived_from_Private
练习15.20
编写代码检验你对前面两题的回答是否正确。
#include <iostream>
#include <string>
#include "exercise15_5.h"
#include "bulk_quote.h"
#include "limit_quote.h"
#include "disc_quote.h"
class Base
{
public:
void pub_mem(); // public member
protected:
int prot_mem; // protected member
private:
char priv_mem; // private member
};
struct Pub_Derv : public Base
{
void memfcn(Base &b) { b = *this; }
};
struct Priv_Derv : private Base
{
void memfcn(Base &b) { b = *this; }
};
struct Prot_Derv : protected Base
{
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Public : public Pub_Derv
{
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Private : public Priv_Derv
{
//void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Protected : public Prot_Derv
{
void memfcn(Base &b) { b = *this; }
};
int main()
{
Pub_Derv d1;
Base *p = &d1;
Priv_Derv d2;
//p = &d2;
Prot_Derv d3;
//p = &d3;
Derived_from_Public dd1;
p = &dd1;
Derived_from_Private dd2;
//p =& dd2;
Derived_from_Protected dd3;
//p = &dd3;
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)