Skip to content

Commit

Permalink
Exercise 15.9: Adding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gehtmaguad committed Apr 12, 2016
1 parent c6a935f commit 375a713
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions ch15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The dynamic type is the type of the object in memory that the variable or expres
> When is it possible for an expression’s static type to differ from its dynamic type? Give three examples in which the static and dynamic type differ.
The static type of a pointer or reference to a base class may differ from its dynamic type. Anything like this can be an example.
[Exercise 15.9](ex15.9/main.cpp)

## Exercise 15.10:
> 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.
Expand Down
7 changes: 7 additions & 0 deletions ch15/ex15.9/bulk_quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "bulk_quote.h"

double Bulk_quote::net_price(std::size_t n) const
{
std::cout << "Bulk_Quote net_price method got called" << std::endl;
return n * price * ( n >= min_qty ? 1 - discount : 1);
}
19 changes: 19 additions & 0 deletions ch15/ex15.9/bulk_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef BULK_QUOTE_H
#define BULK_QUOTE_H
#include "quote.h"

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

double net_price(std::size_t n) const override;

private:
std::size_t min_qty = 0;
double discount = 0.0;
};

#endif // BULK_QUOTE_H
9 changes: 9 additions & 0 deletions ch15/ex15.9/limit_quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "limit_quote.h"

double Limit_quote::net_price(std::size_t n) const
{
if (n > max_qty)
return max_qty * price * discount + (n - max_qty) * price;
else
return n * discount *price;
}
20 changes: 20 additions & 0 deletions ch15/ex15.9/limit_quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef LIMIT_QUOTE_H
#define LIMIT_QUOTE_H

#include "quote.h"

class Limit_quote : public Quote
{
public:
Limit_quote();
Limit_quote(const std::string& b, double p, std::size_t max, double disc):
Quote(b, p), max_qty(max), discount(disc) { }

double net_price(std::size_t n) const override;

private:
std::size_t max_qty = 0;
double discount = 0.0;
};

#endif // LIMIT_QUOTE_H
45 changes: 45 additions & 0 deletions ch15/ex15.9/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************************
* @file main.cpp
* @author Hoesel Markus
* @date 11 Apr 2016
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 15.9:
// When is it possible for an expression’s static type to differ from its
// dynamic type? Give three examples in which the static and dynamic type differ.
//

#include <iostream>
#include <string>

#include "quote.h"
#include "bulk_quote.h"
#include "limit_quote.h"

int main()
{
Bulk_quote bulk_quote("bulk_quote_1", 10.10, 10, 0.5);

// The pointer is of static type Quote, but it's dynamic type is Bulk Quote
// Because of polymorphism the Bulk Quote implementation of the net_price()
// method gets called.
Quote *quote_pointer = &bulk_quote;
quote_pointer->net_price(5);

// The reference is of static type Quote, but it's dynamic type is Bulk Quote
// Like with the pointer, the Bulk Quote implementation of the net_price()
// method gets called.
Quote &quote_reference = bulk_quote;
quote_reference.net_price(5);

// The static type of this variable is Quote. Here the Bulk Quote object
// gets upcasted. The Quote part of bulk_quote gets copied into quote, but
// the rest is not handled. Because of the cast the Quote implementation of
// the net_price() method gets called.
Quote quote = bulk_quote;
quote.net_price(5);

return 0;
}
2 changes: 2 additions & 0 deletions ch15/ex15.9/quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "quote.h"

30 changes: 30 additions & 0 deletions ch15/ex15.9/quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef QUOTE_H
#define QUOTE_H

#include <string>
#include <iostream>

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

std::string isbn() const { return bookNo; }
virtual double net_price(std::size_t n) const {
std::cout << "Quote net_price method got called" << std::endl;
return n * price;
}

virtual ~Quote() = default;

private:
std::string bookNo;

protected:
double price = 0.0;

};

#endif // QUOTE_H

0 comments on commit 375a713

Please sign in to comment.