Skip to content

Commit

Permalink
refactor section 15.8
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 12, 2015
1 parent 8d8ad98 commit 0b603ea
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 344 deletions.
82 changes: 82 additions & 0 deletions ch15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,85 @@ The reason is that a constructor taking 4 parameters has been defined, which pre
> Redefine your `Bulk_quote` class to inherit its constructors.
[code](ex15_27_Bulk_quote.h)
## Exercise 15.28:
> Define a `vector` to hold `Quote` objects but put `Bulk_quote` objects into that `vector`. Compute the total `net_price` of all the elements in the `vector`.
```cpp
#include <vector>
#include <numeric>
#include "ex15_27_Bulk_quote.h"
int main()
{
std::vector<Quote> vecQuote;
Bulk_quote bulk0("0-201-82470-1", 50, 5, 0.2);
Bulk_quote bulk1("0-201-82470-1", 50, 3, 0.5);
// total price should be:
std::cout << "bulk_quote's total: " << bulk0.net_price(5) + bulk1.net_price(5) << std::endl;
vecQuote.push_back(bulk0);
vecQuote.push_back(bulk1);
double total = std::accumulate(vecQuote.cbegin(), vecQuote.cend(),0.0, [](double ret, const Quote &obj){
return ret += obj.net_price(5);
});
// total price in the vector.
std::cout << "total in the vector: " << total << std::endl;
}
```

**Output:**

```
bulk_quote's total: 325
total in the vector: 500
```

## Exercise 15.29:
> Repeat your program, but this time store `shared_ptrs` to objects of type `Quote`. Explain any discrepancy in the sum generated by the this version and the previous program. If there is no discrepancy, explain why there isn’t one.
```cpp
#include <vector>
#include <numeric>
#include <memory>

#include "ex15_27_Bulk_quote.h"

int main()
{
std::vector<std::shared_ptr<Quote>> vecQuote;

std::shared_ptr<Bulk_quote> spBulk0 = std::make_shared<Bulk_quote>("0-201-82470-1", 50, 5, 0.2);
std::shared_ptr<Bulk_quote> spBulk1 = std::make_shared<Bulk_quote>("0-201-82470-1", 50, 3, 0.5);

// total price should be:
std::cout << "bulk_quote's total: " << spBulk0->net_price(5) + spBulk1->net_price(5) << std::endl;

vecQuote.push_back(spBulk0);
vecQuote.push_back(spBulk1);

double total = std::accumulate(vecQuote.cbegin(), vecQuote.cend(),0.0, [](double ret, std::shared_ptr<Quote> sp){
return ret += sp->net_price(5);
});

// total price in the vector.
std::cout << "total in the vector: " << total << std::endl;
}
```

**Output:**

```
bulk_quote's total: 325
total in the vector: 325
```

Because derived objects are "sliced down" when assigned to a base-type object. Thus, when put `Bulk_quote` object into the `vector` of `Quote`, all the object "slice down" to `Quote`.

If we want to holds objects related by inheritance, we should define the `vector` to hold pointers(preferable smart pointers) to the base class. So, the `vector` contains are dynamic type of the objects.

Different from previous program, it can be found that 20% and 50% discount has been applied to the total price calculation.
13 changes: 0 additions & 13 deletions ch15/ex15.28.29/bulk_quote.cpp

This file was deleted.

62 changes: 0 additions & 62 deletions ch15/ex15.28.29/bulk_quote.h

This file was deleted.

2 changes: 0 additions & 2 deletions ch15/ex15.28.29/disc_quote.cpp

This file was deleted.

79 changes: 0 additions & 79 deletions ch15/ex15.28.29/disc_quote.h

This file was deleted.

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

This file was deleted.

19 changes: 0 additions & 19 deletions ch15/ex15.28.29/limit_quote.h

This file was deleted.

79 changes: 0 additions & 79 deletions ch15/ex15.28.29/main.cpp

This file was deleted.

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

This file was deleted.

Loading

0 comments on commit 0b603ea

Please sign in to comment.