Skip to content

Commit

Permalink
added 15.4
Browse files Browse the repository at this point in the history
doesn't finish.
  • Loading branch information
pezy committed Apr 23, 2015
1 parent e5ea397 commit 1f13ce0
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 20 deletions.
11 changes: 11 additions & 0 deletions ch15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,14 @@ derived dobj; base *bp2 = &dobj; base &br2 = dobj;
```

e and f are called at run time.

## Exercise 15.15:
> Define your own versions of `Disc_quote` and `Bulk_quote`.
[Disc_quote](ex15_15_Disc_quote.h) | [Bulk_quote](ex15_15_Bulk_quote.h)

## Exercise 15.16:
> Rewrite the class representing a limited discount strategy, which you wrote for the exercises in 15.2.2 (p. 601), to inherit from `Disc_quote`.
## Exercise 15.17:
> Try to define an object of type Disc_quote and see what errors you get from the compiler.
5 changes: 5 additions & 0 deletions ch15/TEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ int main()
quote_11.debug();
bulk_quote_11.debug();
limit_quote_11.debug();

cout << "\n===== ex15 =====" << endl;

EX15::Bulk_quote bulk_quote_15("0-201-82470-1", 50, 5, .19);
EX03::print_total(std::cout, bulk_quote_15, 6);
}
2 changes: 2 additions & 0 deletions ch15/ch15.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace ch15 {
#include "ex15_11_Quote.h"
#include "ex15_11_Bulk_quote.h"
#include "ex15_11_Limit_quote.h"
#include "ex15_15_Disc_quote.h"
#include "ex15_15_Bulk_quote.h"
}

#endif // CH15_H_
19 changes: 0 additions & 19 deletions ch15/ex15.15.16.17/disc_quote.h

This file was deleted.

2 changes: 1 addition & 1 deletion ch15/ex15_11_Limit_quote.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Limit_quote : public Bulk_quote {
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 {
double net_price(size_t cnt) const final 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;
Expand Down
36 changes: 36 additions & 0 deletions ch15/ex15_15_Bulk_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/Cpp-Primer
Disc_quote
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#ifndef CP5_EX15_15_BULK_QUOTE_H_
#define CP5_EX15_15_BULK_QUOTE_H_

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

inline namespace EX15 {

using std::string;

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;
}
};

}

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

#ifndef CP5_EX15_15_DISC_QUOTE_H_
#define CP5_EX15_15_DISC_QUOTE_H_

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

inline namespace EX15 {

using std::string;

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

}

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

#ifndef CP5_EX15_15_LIMIT_QUOTE_H_
#define CP5_EX15_15_LIMIT_QUOTE_H_

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

namespace EX15 {
using std::string;
using std::cout; using std::endl;

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 final 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;
}

virtual void debug() const final {
Bulk_quote::debug();
cout << "\tmax_qty: " << max_qty << endl;
}

private:
size_t max_qty = 0;
};
}

#endif // CP5_EX15_15_LIMIT_QUOTE_H_

0 comments on commit 1f13ce0

Please sign in to comment.