-
Notifications
You must be signed in to change notification settings - Fork 73
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
Showing
4 changed files
with
73 additions
and
4 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,31 @@ | ||
#include "quote.h" | ||
|
||
std::string Quote::isbin() const { | ||
return bookNo; | ||
} | ||
|
||
class Bulk_quote: public Quote { | ||
public: | ||
Bulk_quote() = default; | ||
// 遵循基类接口 通过调用基类的构造函数来初始化那些从基类中继承的对象 | ||
Bulk_quote(const std::string& book, double p, | ||
std::size_t qty, double disc) : | ||
Quote(book, p), min_qty(qty), discount(disc) {} | ||
double net_price(std::size_t cnt) const override { | ||
if(cnt >= min_qty) { | ||
return cnt * (1-discount) * price; | ||
} else { | ||
return cnt + price; | ||
} | ||
}; | ||
private: | ||
std::size_t min_qty = 0; | ||
double discount = 0.0; | ||
}; | ||
|
||
double print_total(std::ostream &os, const Quote &item, size_t n) { | ||
double ret = item.net_price(n); | ||
os << "ISBN: " << item.isbin() | ||
<< " # sold: " << n << "total due: " << ret << std::endl; | ||
return ret; | ||
} |
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,29 @@ | ||
#include <string> | ||
#include <iostream> | ||
|
||
class Quote { | ||
public: | ||
// =default 表示我们需要一个自定义的构造函数 也需要一个默认的构造函数 | ||
Quote() = default; | ||
Quote(const std::string &book, double sales_price): | ||
bookNo(book), price(sales_price) {} | ||
|
||
std::string isbin() const; | ||
// virtual double net_price(std::size_t n) const = 0; | ||
virtual double net_price(std::size_t n) const {return n * price;} | ||
virtual ~Quote() = default; | ||
private: | ||
std::string bookNo; | ||
// 允许派生类 访问 | ||
protected: | ||
double price = 1.0; | ||
}; | ||
|
||
// 派生类的声明 只包含类名 不包含派生列表 | ||
class Bulk_quote; | ||
|
||
// error 不能派生 | ||
// class Bulk_quote: public Quote; | ||
|
||
// 不想某一个类作为基类 在声明后面加上 final | ||
class Base final {}; |
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,13 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
template<typename T> | ||
int compare(const T &v1, const T &v2) { | ||
if (v1 > v2) return -1; | ||
if (v1 < v2) return 1; | ||
return 0; | ||
} | ||
|
||
int main() { | ||
|
||
} |
This file was deleted.
Oops, something went wrong.