From f0cc41eab2499bc7e679e7b6ecdb7377d472e722 Mon Sep 17 00:00:00 2001 From: yangsoon Date: Thu, 25 Jun 2020 21:43:27 +0800 Subject: [PATCH] ad cppprimer --- C++Primer/OOP/quote.cpp | 31 +++++++++++++++++++++++++++++++ C++Primer/OOP/quote.h | 29 +++++++++++++++++++++++++++++ C++Primer/Template/com.cpp | 13 +++++++++++++ apue/demo/test.c | 4 ---- 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 C++Primer/OOP/quote.cpp create mode 100644 C++Primer/OOP/quote.h create mode 100644 C++Primer/Template/com.cpp delete mode 100644 apue/demo/test.c diff --git a/C++Primer/OOP/quote.cpp b/C++Primer/OOP/quote.cpp new file mode 100644 index 0000000..768afc2 --- /dev/null +++ b/C++Primer/OOP/quote.cpp @@ -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; +} \ No newline at end of file diff --git a/C++Primer/OOP/quote.h b/C++Primer/OOP/quote.h new file mode 100644 index 0000000..dc5a717 --- /dev/null +++ b/C++Primer/OOP/quote.h @@ -0,0 +1,29 @@ +#include +#include + +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 {}; \ No newline at end of file diff --git a/C++Primer/Template/com.cpp b/C++Primer/Template/com.cpp new file mode 100644 index 0000000..e0e5231 --- /dev/null +++ b/C++Primer/Template/com.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +template +int compare(const T &v1, const T &v2) { + if (v1 > v2) return -1; + if (v1 < v2) return 1; + return 0; +} + +int main() { + +} \ No newline at end of file diff --git a/apue/demo/test.c b/apue/demo/test.c deleted file mode 100644 index 857689c..0000000 --- a/apue/demo/test.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "apue.h" -int main() { - -} \ No newline at end of file