Skip to content

Commit

Permalink
added 15.11
Browse files Browse the repository at this point in the history
1. added debug for Quote/Bulk_quote/Limit_quote
2. Test output
  • Loading branch information
pezy committed Apr 22, 2015
1 parent 586ff0d commit 754be42
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 197 deletions.
6 changes: 6 additions & 0 deletions ch15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ Anything like this can be an example.
> Recalling the discussion from 8.1 (p. 311), explain how the program on page 317 that passed an `ifstream` to the `Sales_data` `read` function works.
the function takes a `istream` from which `ifstream` is derived. Hence the `ifstream` object "is a" `istream` ,which is why it works.

## Exercise 15.11:
> Add a virtual `debug` function to your `Quote` class hierarchy
that displays the data members of the respective classes.

[Quote](ex15_11_Quote.h) | [Bulk_quote](ex15_11_Bulk_quote.h) | [Limit_quote](ex15_11_Limit_quote.h)
16 changes: 16 additions & 0 deletions ch15/TEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@ using namespace ch15;

int main()
{
cout << "\n===== ex03 =====" << endl;

EX03::Quote quote_03("0-201-78345-X", 23.8);
EX03::print_total(std::cout, quote_03, 3);

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

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

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

EX07::Limit_quote limit_quote("0-201-78345-X", 23.8, 3, 5, 0.5);
EX03::print_total(std::cout, limit_quote, 6);

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

EX11::Quote quote_11("0-201-82470-1", 50);
EX11::Bulk_quote bulk_quote_11("0-201-82470-1", 50, 5, .19);
EX11::Limit_quote limit_quote_11("0-201-82470-1", 50, 5, 10, .19);

quote_11.debug();
bulk_quote_11.debug();
limit_quote_11.debug();
}
3 changes: 3 additions & 0 deletions ch15/ch15.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace ch15 {
#include "ex15_03_Quote.h"
#include "ex15_05_Bulk_quote.h"
#include "ex15_07_Limit_quote.h"
#include "ex15_11_Quote.h"
#include "ex15_11_Bulk_quote.h"
#include "ex15_11_Limit_quote.h"
}

#endif // CH15_H_
13 changes: 0 additions & 13 deletions ch15/ex15.11/bulk_quote.cpp

This file was deleted.

20 changes: 0 additions & 20 deletions ch15/ex15.11/bulk_quote.h

This file was deleted.

9 changes: 0 additions & 9 deletions ch15/ex15.11/limit_quote.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions ch15/ex15.11/limit_quote.h

This file was deleted.

88 changes: 0 additions & 88 deletions ch15/ex15.11/main.cpp

This file was deleted.

8 changes: 0 additions & 8 deletions ch15/ex15.11/quote.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions ch15/ex15.11/quote.h

This file was deleted.

6 changes: 3 additions & 3 deletions ch15/ex15_03_Quote.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#ifndef QUOTE_H
#define QUOTE_H
#ifndef CP5_EX15_03_QUOTE_H_
#define CP5_EX15_03_QUOTE_H_

#include <string>
#include <iostream>
Expand Down Expand Up @@ -45,4 +45,4 @@ double print_total(ostream& os, Quote const& item, size_t n) {

}

#endif // QUOTE_H
#endif // CP5_EX15_03_QUOTE_H_
6 changes: 3 additions & 3 deletions ch15/ex15_05_Bulk_quote.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#ifndef BULK_QUOTE_H_
#define BULK_QUOTE_H_
#ifndef CP5_EX15_05_BULK_QUOTE_H_
#define CP5_EX15_05_BULK_QUOTE_H_

#include "ex15_03_Quote.h"
#include <string>
Expand All @@ -36,4 +36,4 @@ class Bulk_quote : public Quote {
};
}

#endif // BULK_QUOTE_H_
#endif // CP5_EX15_05_BULK_QUOTE_H_
4 changes: 2 additions & 2 deletions ch15/ex15_07_Limit_quote.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#ifndef LIMIT_QUOTE_H_
#define LIMIT_QUOTE_H_
#ifndef CP5_EX15_07_LIMIT_QUOTE_H_
#define CP5_EX15_07_LIMIT_QUOTE_H_

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

#ifndef CP5_EX15_11_BULK_QUOTE_H_
#define CP5_EX15_11_BULK_QUOTE_H_

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

namespace EX11 {
using std::string;

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

virtual void debug() const override {
Quote::debug();
cout << "\tminqty: " << min_qty << "\tdiscount: " << discount << endl;
}

protected:
size_t min_qty = 0;
double discount = 0.0;
};
}

#endif // CP5_EX15_11_BULK_QUOTE_H_
46 changes: 46 additions & 0 deletions ch15/ex15_11_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_11_LIMIT_QUOTE_H_
#define CP5_EX15_11_LIMIT_QUOTE_H_

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

namespace EX11 {
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 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_11_LIMIT_QUOTE_H_
Loading

0 comments on commit 754be42

Please sign in to comment.