forked from huangmingchuan/Cpp_Primer_Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
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
93adeb3
commit 6f82d41
Showing
8 changed files
with
372 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef BULK_QUOTE_H_ | ||
#define BULK_QUOTE_H_ | ||
#include "Disc_quote.h" | ||
|
||
class Bulk_quote : public Disc_quote | ||
{ | ||
public: | ||
Bulk_quote() = default; | ||
Bulk_quote(const std::string& b, double p, std::size_t q, double disc) : | ||
Disc_quote(b, p, q, disc) | ||
{} | ||
|
||
double net_price(std::size_t n) const override; | ||
}; | ||
|
||
#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,21 @@ | ||
#ifndef DISC_QUOTE_H | ||
#define DISC_QUOTE_H | ||
|
||
#include "exercise15_5.h" | ||
|
||
class Disc_quote : public Quote | ||
{ | ||
public: | ||
Disc_quote(); | ||
Disc_quote(const std::string& b, double p, std::size_t q, double d) : | ||
Quote(b, p), quantity(q), discount(d) | ||
{} | ||
|
||
virtual double net_price(std::size_t n) const override = 0; | ||
|
||
protected: | ||
std::size_t quantity; | ||
double discount; | ||
}; | ||
|
||
#endif // DISC_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,21 @@ | ||
#ifndef LIMIT_QUOTE_H | ||
#define LIMIT_QUOTE_H | ||
|
||
#include "disc_quote.h" | ||
|
||
class Limit_quote : public Disc_quote | ||
{ | ||
public: | ||
Limit_quote() = default; | ||
Limit_quote(const std::string& b, double p, std::size_t max, double disc) : | ||
Disc_quote(b, p, max, disc) | ||
{} | ||
|
||
double net_price(std::size_t n) const override | ||
{ | ||
return n * price * (n < quantity ? 1 - discount : 1); | ||
} | ||
|
||
}; | ||
|
||
#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
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,67 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "exercise15_5.h" | ||
#include "bulk_quote.h" | ||
#include "limit_quote.h" | ||
#include "disc_quote.h" | ||
|
||
class Base | ||
{ | ||
public: | ||
void pub_mem(); // public member | ||
protected: | ||
int prot_mem; // protected member | ||
private: | ||
char priv_mem; // private member | ||
}; | ||
|
||
struct Pub_Derv : public Base | ||
{ | ||
void memfcn(Base &b) { b = *this; } | ||
}; | ||
struct Priv_Derv : private Base | ||
{ | ||
void memfcn(Base &b) { b = *this; } | ||
}; | ||
struct Prot_Derv : protected Base | ||
{ | ||
void memfcn(Base &b) { b = *this; } | ||
}; | ||
|
||
struct Derived_from_Public : public Pub_Derv | ||
{ | ||
void memfcn(Base &b) { b = *this; } | ||
}; | ||
struct Derived_from_Private : public Priv_Derv | ||
{ | ||
//void memfcn(Base &b) { b = *this; } | ||
}; | ||
struct Derived_from_Protected : public Prot_Derv | ||
{ | ||
void memfcn(Base &b) { b = *this; } | ||
}; | ||
|
||
int main() | ||
{ | ||
Pub_Derv d1; | ||
Base *p = &d1; | ||
|
||
Priv_Derv d2; | ||
//p = &d2; | ||
|
||
Prot_Derv d3; | ||
//p = &d3; | ||
|
||
Derived_from_Public dd1; | ||
p = &dd1; | ||
|
||
Derived_from_Private dd2; | ||
//p =& dd2; | ||
|
||
Derived_from_Protected dd3; | ||
//p = &dd3; | ||
|
||
|
||
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,78 @@ | ||
#ifndef QUOTE_H | ||
#define QUOTE_H | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class Quote | ||
{ | ||
friend bool operator !=(const Quote& lhs, const Quote& rhs); | ||
public: | ||
Quote() { std::cout << "default constructing Quote\n"; } | ||
Quote(const std::string &b, double p) : | ||
bookNo(b), price(p) | ||
{ | ||
std::cout << "Quote : constructor taking 2 parameters\n"; | ||
} | ||
|
||
// copy constructor | ||
Quote(const Quote& q) : bookNo(q.bookNo), price(q.price) | ||
{ | ||
std::cout << "Quote: copy constructing\n"; | ||
} | ||
|
||
// move constructor | ||
Quote(Quote&& q) noexcept : bookNo(std::move(q.bookNo)), price(std::move(q.price)) | ||
{ std::cout << "Quote: move constructing\n"; } | ||
|
||
// copy = | ||
Quote& operator =(const Quote& rhs) | ||
{ | ||
if (*this != rhs) | ||
{ | ||
bookNo = rhs.bookNo; | ||
price = rhs.price; | ||
} | ||
std::cout << "Quote: copy =() \n"; | ||
|
||
return *this; | ||
} | ||
|
||
// move = | ||
Quote& operator =(Quote&& rhs) noexcept | ||
{ | ||
if (*this != rhs) | ||
{ | ||
bookNo = std::move(rhs.bookNo); | ||
price = std::move(rhs.price); | ||
} | ||
std::cout << "Quote: move =!!!!!!!!! \n"; | ||
|
||
return *this; | ||
} | ||
|
||
std::string isbn() const { return bookNo; } | ||
virtual double net_price(std::size_t n) const { return n * price; } | ||
virtual void debug() const; | ||
|
||
virtual ~Quote() | ||
{ | ||
std::cout << "destructing Quote\n"; | ||
} | ||
|
||
private: | ||
std::string bookNo; | ||
|
||
protected: | ||
double price = 10.0; | ||
}; | ||
|
||
bool inline | ||
operator !=(const Quote& lhs, const Quote& rhs) | ||
{ | ||
return lhs.bookNo != rhs.bookNo | ||
&& | ||
lhs.price != rhs.price; | ||
} | ||
|
||
#endif // 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,59 @@ | ||
#ifndef BULK_QUOTE_H | ||
#define BULK_QUOTE_H | ||
#include "Disc_quote.h" | ||
#include <iostream> | ||
|
||
class Bulk_quote : public Disc_quote | ||
{ | ||
|
||
public: | ||
Bulk_quote() { std::cout << "default constructing Bulk_quote\n"; } | ||
Bulk_quote(const std::string& b, double p, std::size_t q, double disc) : | ||
Disc_quote(b, p, q, disc) | ||
{ | ||
std::cout << "Bulk_quote : constructor taking 4 parameters\n"; | ||
} | ||
|
||
// copy constructor | ||
Bulk_quote(const Bulk_quote& bq) : Disc_quote(bq) | ||
{ | ||
std::cout << "Bulk_quote : copy constructor\n"; | ||
} | ||
|
||
// move constructor | ||
Bulk_quote(Bulk_quote&& bq) : Disc_quote(std::move(bq)) noexcept | ||
{ | ||
std::cout << "Bulk_quote : move constructor\n"; | ||
} | ||
|
||
// copy =() | ||
Bulk_quote& operator =(const Bulk_quote& rhs) | ||
{ | ||
Disc_quote::operator =(rhs); | ||
std::cout << "Bulk_quote : copy =()\n"; | ||
|
||
return *this; | ||
} | ||
|
||
|
||
// move =() | ||
Bulk_quote& operator =(Bulk_quote&& rhs) noexcept | ||
{ | ||
Disc_quote::operator =(std::move(rhs)); | ||
std::cout << "Bulk_quote : move =()\n"; | ||
|
||
return *this; | ||
} | ||
|
||
double net_price(std::size_t n) const override; | ||
void debug() const override; | ||
|
||
~Bulk_quote() override | ||
{ | ||
std::cout << "destructing Bulk_quote\n"; | ||
} | ||
}; | ||
|
||
|
||
|
||
#endif // BULK_QUOTE_H |
Oops, something went wrong.