第十五章 15.7.2节练习 & 15.7.3节练习

练习15.25

我们为什么为Disc_quote定义一个默认构造函数?如果去除掉该构造函数的话会对Bulk_quote的行为产生什么影响?

解答:

这里我们定义默认构造函数的原因,就是为成员变量进行初始化。(书中有解释)

在去掉默认构造函数时,Bulk_quote会无合适的构造函数可用。

测试代码如下:(Quote使用了随书附带的代码)

#include <memory>
#include <iostream>
#include <string>
#include <cstddef>
#include <utility> 

// Item sold at an undiscounted price
// derived classes will define various discount strategies
class Quote {
	friend std::istream& operator>>(std::istream&, Quote&);
	friend std::ostream& operator<<(std::ostream&, const Quote&);
public:
#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
	Quote() = default;
#else
	Quote() : price(0.0) { }
#endif
	Quote(const std::string &book, double sales_price) :
		bookNo(book), price(sales_price) { }

	// virtual destructor needed 
	// if a base pointer pointing to a derived object is deleted
#ifdef DEFAULT_FCNS
	virtual ~Quote() = default; // dynamic binding for the destructor
#else
	virtual ~Quote() { }        // dynamic binding for the destructor
#endif

	std::string isbn() const { return bookNo; }

	// returns the total sales price for the specified number of items
	// derived classes will override and apply different discount algorithms
	virtual double net_price(std::size_t n) const
	{
		return n * price;
	}

	// virtual function to return a dynamically allocated copy of itself

#ifdef REFMEMS
	virtual Quote* clone() const & { return new Quote(*this); }
	virtual Quote* clone() && {return new Quote(std::move(*this)); }
#else
	// without reference qualification on member functions 
	// we can't overloaded on rvalue reference and const lvalue reference
	// so for now we just implement a single version that copies itself
	virtual Quote* clone() const { return new Quote(*this); }
#endif
private:
	std::string bookNo; // ISBN number of this item
protected:
#ifdef IN_CLASS_INITS
	double price = 0.0; // normal, undiscounted price
#else
	double price;
#endif
};

class Disc_quote : public Quote {
public:
	//Disc_quote() = default;
	Disc_quote(const std::string& book, double price,
		std::size_t qty, double disc) :
		Quote(book, price),
		quantity(qty), discount(disc) { }

	double net_price(std::size_t) const = 0;

protected:
	std::size_t quantity = 0; // purchase size for the discount to apply
	double discount = 0.0;    // fractional discount to apply
};

class Bulk_quote : public Disc_quote { // Bulk_quote inherits from Quote
public:
	Bulk_quote() = default;
	Bulk_quote(const std::string& book, double p,
		std::size_t qty, double disc) :
		Disc_quote(book, p, qty, disc) { }

	// overrides the base version in order to implement the bulk purchase discount policy
	double net_price(std::size_t) const override;
};

double Bulk_quote::net_price(size_t cnt) const
{
	if (cnt >= quantity)
		return cnt * (1 - discount) * price;
	else
		return cnt * price;
}

int main(){
	Bulk_quote a;
	return 0;
}

vs2013 报的错误:

1>源.cpp(94): error C2512: “Bulk_quote”: 没有合适的默认构造函数可用
大家可以用自己手头上的编译器试试,看看会怎么样报错。


练习15.26

定义Quote和Bulk_quote的拷贝控制成员,令其与合成的版本行为一致。为这些成员以及其他构造函数添加打印状态的语句,使得我们能够知道正在运行那个函数。使用这些类编写程序,预测程序将穿件和销毁那些对象。重复实现,不对比较你的预测和实际输出结果是否相同。直到预测完全准确再结束。

解答:

这题的操作性很强,需要通过对应的实现对类的行为进行预测。

这里也写我这边的结果了,做了几个简单的和复杂的,基本上预测的和编译器执行完运行的结果一致。

在做之前给个提示吧,把自己想要预测的组合先都写出来,然后加上自己的预测,再逐个和程序运行的结果进行比较。

如果不一致,可以进行调试。

这样应该能更助于了解类的各种行为。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值