From f0cc41eab2499bc7e679e7b6ecdb7377d472e722 Mon Sep 17 00:00:00 2001 From: yangsoon Date: 2020年6月25日 21:43:27 +0800 Subject: [PATCH 1/3] 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 From 7b803212e0f67800c76a8f4ef1d6027c02ccf696 Mon Sep 17 00:00:00 2001 From: yangsoon Date: 2020年6月26日 20:44:53 +0800 Subject: [PATCH 2/3] add smart-point --- C++Primer/Template/com.cpp | 2 +- C++Primer/smart_point/strblob.cpp | 29 +++++++++++++++++++ C++Primer/smart_point/strblob.h | 20 +++++++++++++ .../{tcpcliser => tcpclistener}/tcpcli01.c | 0 .../{tcpcliser => tcpclistener}/tcpcli04.c | 0 .../{tcpcliser => tcpclistener}/tcpserv01.c | 0 .../{tcpcliser => tcpclistener}/tcpserv02.c | 0 .../{tcpcliser => tcpclistener}/tcpserv04.c | 0 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 C++Primer/smart_point/strblob.cpp create mode 100644 C++Primer/smart_point/strblob.h rename muduo/unp/{tcpcliser => tcpclistener}/tcpcli01.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpcli04.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpserv01.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpserv02.c (100%) rename muduo/unp/{tcpcliser => tcpclistener}/tcpserv04.c (100%) diff --git a/C++Primer/Template/com.cpp b/C++Primer/Template/com.cpp index e0e5231..8f76be3 100644 --- a/C++Primer/Template/com.cpp +++ b/C++Primer/Template/com.cpp @@ -9,5 +9,5 @@ int compare(const T &v1, const T &v2) { } int main() { - + cout << compare(1,2); } \ No newline at end of file diff --git a/C++Primer/smart_point/strblob.cpp b/C++Primer/smart_point/strblob.cpp new file mode 100644 index 0000000..6d0cf4d --- /dev/null +++ b/C++Primer/smart_point/strblob.cpp @@ -0,0 +1,29 @@ +#include "strblob.h" + +// make_shared 在动态内存中分配一个对象并初始化它 +StrBlob::StrBlob(): data(std::make_shared>()) {}; + +StrBlob::StrBlob(std::initializer_list il): data(std::make_shared>(il)){}; + +void StrBlob::check(size_type i, const std::string &msg) const { + if (i>= data->size()) { + throw std::out_of_range(msg); + } +} + +std::string& StrBlob::front() { + check(0, "front on empty StrBlob"); + return data->front(); +} + +std::string& StrBlob::back() { + check(0, "back on empty StrBlob"); + return data->back(); +} + +void StrBlob::pop_back() { + check(0, "pop_back on empty StrBlob"); + data->pop_back(); +} + + diff --git a/C++Primer/smart_point/strblob.h b/C++Primer/smart_point/strblob.h new file mode 100644 index 0000000..4ae85b4 --- /dev/null +++ b/C++Primer/smart_point/strblob.h @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +class StrBlob { +public: + typedef std::vector::size_type size_type; + StrBlob(); + StrBlob(std::initializer_list il); + size_type size() const {return data->size();} + bool empty() const {return data->empty();} + void push_back(const std::string &t) {data->push_back(t)}; + void pop_back(); + std::string& front(); + std::string& back(); +private: + std::shared_ptr> data; + void check(size_type i, const std::string &msg) const; +}; diff --git a/muduo/unp/tcpcliser/tcpcli01.c b/muduo/unp/tcpclistener/tcpcli01.c similarity index 100% rename from muduo/unp/tcpcliser/tcpcli01.c rename to muduo/unp/tcpclistener/tcpcli01.c diff --git a/muduo/unp/tcpcliser/tcpcli04.c b/muduo/unp/tcpclistener/tcpcli04.c similarity index 100% rename from muduo/unp/tcpcliser/tcpcli04.c rename to muduo/unp/tcpclistener/tcpcli04.c diff --git a/muduo/unp/tcpcliser/tcpserv01.c b/muduo/unp/tcpclistener/tcpserv01.c similarity index 100% rename from muduo/unp/tcpcliser/tcpserv01.c rename to muduo/unp/tcpclistener/tcpserv01.c diff --git a/muduo/unp/tcpcliser/tcpserv02.c b/muduo/unp/tcpclistener/tcpserv02.c similarity index 100% rename from muduo/unp/tcpcliser/tcpserv02.c rename to muduo/unp/tcpclistener/tcpserv02.c diff --git a/muduo/unp/tcpcliser/tcpserv04.c b/muduo/unp/tcpclistener/tcpserv04.c similarity index 100% rename from muduo/unp/tcpcliser/tcpserv04.c rename to muduo/unp/tcpclistener/tcpserv04.c From 62d5bc3923149681d4e05f8f1d51dd34ca75869f Mon Sep 17 00:00:00 2001 From: dingdong9763 Date: Thu, 7 Jan 2021 14:57:38 +0800 Subject: [PATCH 3/3] Update string.h --- cpp-part1/string.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cpp-part1/string.h b/cpp-part1/string.h index 9fedc10..bf521a4 100644 --- a/cpp-part1/string.h +++ b/cpp-part1/string.h @@ -51,15 +51,15 @@ String::String(const String& str) { // 首先 需要把 b清空 然后重新分配一块和a一样大的内存 将a拷贝过来 inline String& String::operator=(const String& str) { - // 检测是不是自己 如果没有这一步 会出现错误 + // 检测是不是自己 如果没有这一步 可能会出现错误 影响效率 因为第一个动作是把自己杀掉,如果a = a,那么没有东西可以拷贝了 if(this==&str) { return *this; } - // #1 + // #1 把自己的内存空间删掉 delete[] m_data; - // #2 + // #2 分配一个足够的空间 m_data = new char[strlen(str.m_data)+1]; - // #3 + // #3 深拷贝,把a拷贝过来 strcpy(m_data, str.m_data); return *this; } @@ -81,4 +81,4 @@ std::ostream& operator << (std::ostream& os, const String& str) { return os << str.get_c_str(); } -#endif \ No newline at end of file +#endif

AltStyle によって変換されたページ (->オリジナル) /