Skip to content

Commit

Permalink
ad cppprimer
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsoon committed Jun 25, 2020
1 parent feac099 commit f0cc41e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
31 changes: 31 additions & 0 deletions C++Primer/OOP/quote.cpp
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;
}
29 changes: 29 additions & 0 deletions C++Primer/OOP/quote.h
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 {};
13 changes: 13 additions & 0 deletions C++Primer/Template/com.cpp
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() {

}
4 changes: 0 additions & 4 deletions apue/demo/test.c

This file was deleted.

0 comments on commit f0cc41e

Please sign in to comment.