-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6a935f
commit 375a713
Showing
8 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "e_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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#include "quote.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |