Skip to content

Commit

Permalink
Refactor 15.2.2
Browse files Browse the repository at this point in the history
1. fixed 15.7, limit_quote should inherit from bulk_quote class.
2. added the question to README
  • Loading branch information
pezy committed Apr 14, 2015
1 parent bf437d5 commit 9789aa1
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 241 deletions.
28 changes: 28 additions & 0 deletions ch15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,31 @@ base class members:
> Define your own versions of the `Quote` class and the `print_total` function.
[code](ex15_03_Quote.h)

## Exercise 15.4:
> Which of the following declarations, if any, are incorrect? Explain why.
```cpp
class Base { ... };
(a) class Derived : public Derived { ... };
(b) class Derived : private Base { ... };
(c) class Derived : public Base;
```

- (a) incorrect, derive from itself
- (b) incorrect, this is a definition not a declaration
- (c) incorrect, A derived class is declared like any other class. The declaration contains the class name but does not include its derivation list

## Exercise 15.5:
> Define your own version of the `Bulk_quote` class.
[code](ex15_05_Bulk_quote.h)

## Exercise 15.6:
> Test your `print_total` function from the exercises in 15.2.1 (p. 595) by passing both `Quote` and `Bulk_quote` objects o that function.
[TEST](TEST.cpp)

## Exercise 15.7:
> Define a class that implements a limited discount strategy, which applies a discount to books purchased up to a given limit. If the number of copies exceeds that limit, the normal price applies to those purchased beyond the limit.
[code](ex15_07_Limit_quote.h)
6 changes: 6 additions & 0 deletions ch15/TEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ int main()
{
EX03::Quote quote_03("0-201-78345-X", 23.8);
EX03::print_total(std::cout, quote_03, 3);

EX05::Bulk_quote bulk_quote("0-201-78345-X", 23.8, 3, 0.5);
EX03::print_total(std::cout, bulk_quote, 4);

EX07::Limit_quote limit_quote("0-201-78345-X", 23.8, 3, 5, 0.5);
EX03::print_total(std::cout, limit_quote, 6);
}
2 changes: 2 additions & 0 deletions ch15/ch15.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ If you have questions, try to connect with me: pezy<[email protected]>

namespace ch15 {
#include "ex15_03_Quote.h"
#include "ex15_05_Bulk_quote.h"
#include "ex15_07_Limit_quote.h"
}

#endif // CH15_H_
6 changes: 0 additions & 6 deletions ch15/ex15.5.6/bulk_quote.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions ch15/ex15.5.6/bulk_quote.h

This file was deleted.

59 changes: 0 additions & 59 deletions ch15/ex15.5.6/main.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions ch15/ex15.5.6/quote.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions ch15/ex15.5.6/quote.h

This file was deleted.

6 changes: 0 additions & 6 deletions ch15/ex15.7/bulk_quote.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions ch15/ex15.7/bulk_quote.h

This file was deleted.

3 changes: 0 additions & 3 deletions ch15/ex15.7/limit_quote.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions ch15/ex15.7/limit_quote.h

This file was deleted.

52 changes: 0 additions & 52 deletions ch15/ex15.7/main.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions ch15/ex15.7/quote.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions ch15/ex15.7/quote.h

This file was deleted.

39 changes: 39 additions & 0 deletions ch15/ex15_05_Bulk_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/Cpp-Primer
Bulk_quote class
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#ifndef BULK_QUOTE_H_
#define BULK_QUOTE_H_

#include "ex15_03_Quote.h"
#include <string>

namespace EX05 {
using std::string;
using namespace EX03;

class Bulk_quote : public Quote {
public:
Bulk_quote() = default;
Bulk_quote(string const& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {}

virtual double net_price(size_t cnt) const override {
if (cnt >= min_qty) return cnt * (1 - discount) * price;
else return cnt * price;
}
protected:
size_t min_qty = 0;
double discount = 0.0;
};
}

#endif // BULK_QUOTE_H_
40 changes: 40 additions & 0 deletions ch15/ex15_07_Limit_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/Cpp-Primer
Limit_quote class
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#ifndef LIMIT_QUOTE_H_
#define LIMIT_QUOTE_H_

#include "ex15_05_Bulk_quote.h"
#include <string>

namespace EX07 {
using namespace EX05;
using std::string;

class Limit_quote : public Bulk_quote {
public:
Limit_quote() = default;
Limit_quote(string const& book, double p, size_t min, size_t max, double dist) : Bulk_quote(book, p, min, dist), max_qty(max) {}

double net_price(size_t cnt) const override {
if (cnt > max_qty) return max_qty * (1 - discount) * price + (cnt - max_qty) * price;
else if (cnt >= min_qty) return cnt * (1 - discount) * price;
else return cnt * price;
}

private:
size_t max_qty = 0;
};
}

#endif // LIMIT_QUOTE_H_

0 comments on commit 9789aa1

Please sign in to comment.