Skip to content

Commit

Permalink
move 15.24, 15.25 to README
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 9, 2015
1 parent 6440a54 commit b7545bf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
60 changes: 60 additions & 0 deletions ch15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,63 @@ such as `shape_name()`, `resize_by_percentage(float pct)`, `area()`, `volume()`,
Generally, a base class should define a virtual destructor.

The destructor needs to be virtual to allow objects in the inheritance hierarchy to be **dynamically allocated**.

## Exercise 15.25:
> Why did we define a default constructor for `Disc_quote`? What effect, if any, would removing that constructor have on the behavior of `Bulk_quote`?
```cpp
#include <string>
using std::string;

class Quote {
public:
Quote() = default;
Quote(string const& b, double p) : bookNo(b), price(p) { }

string isbn() const { return bookNo; }
virtual double net_price(size_t n) const { return n * price; }

virtual ~Quote() = default;
private:
string bookNo;
protected:
double price = 0.0;
};

class Disc_quote : public Quote {
public:
//Disc_quote() = default;
Disc_quote(string const& b, double p, size_t q, double d) : Quote(b, p), quantity(q), discount(d){ }
virtual double net_price(size_t) const = 0;
protected:
size_t quantity = 0;
double discount = 0.0;
};

class Bulk_quote : public Disc_quote {
public:
Bulk_quote() = default;
Bulk_quote(string const& book, double price, size_t qty, double disc) : Disc_quote(book, price, qty, disc) { }
virtual double net_price(std::size_t cnt) const override {
if (cnt >= quantity) return cnt * (1 - discount) * price;
else return cnt * price;
}
};

int main()
{
Bulk_quote b_quote;
}
```
Without it, when building the upper codes, the compiler would conplain that:
```
error: use of deleted function 'Bulk_quote::Bulk_quote()'
Bulk_quote b_quote;
^
'Bulk_quote::Bulk_quote()' is implicitly deleted because the default definition would be ill-formed:
Bulk_quote() = default;
^
```
The reason is that a constructor taking 4 parameters has been defined, which prevented the compiler generate synthesized version default constructor. As a result, the default constructor of any class derived from it has been defined as deleted. Thus the default constructor must be defined explicitly so that the derived classes can call it when executing its default constructor.
48 changes: 0 additions & 48 deletions ch15/ex15.24.25.cpp

This file was deleted.

0 comments on commit b7545bf

Please sign in to comment.