C++11: inherited constructors
来源:互联网 发布:英国美国留学对比知乎 编辑:程序博客网 时间:2024/04/29 08:49
#include <string>#include <iostream>class Quote {public: Quote() = default; Quote(const std::string &book, double sales_price) : bookNo(book), price(sales_price) {} std::string isbn() const { return bookNo; } virtual double net_price(std::size_t n) const { return n * price; } virtual ~Quote() = default;private: std::string bookNo;protected: double price = 0.0;};class Disc_quote : public Quote {public: Disc_quote() = default; Disc_quote(const std::string &book, double price, std::size_t qty, double disc) : Quote(book, price), quantity(qty), discount(disc) { } double net_price(std::size_t) const = 0;protected: std::size_t quantity = 0; double discount = 0.0;};class Bulk_quote : public Disc_quote {public: using Disc_quote::Disc_quote; double net_price(std::size_t) const override;};double Bulk_quote::net_price(std::size_t cnt) const{ if (cnt >= quantity) { return cnt * (1 - discount) * price; } else { return cnt * price; }}int main(){ Bulk_quote bulk("758-333-33333", 50.2, 20, 0.05); std::cout << bulk.net_price(10) << std::endl; std::cout << bulk.net_price(30) << std::endl; Bulk_quote bulk2; return 0;}
// from C++ primer 5th(p.628)// g++ xx.cpp -std=c++11// gcc 4.9.2
0 0
- C++11: inherited constructors
- C++/Constructors of Global Object
- Constructors in C++11
- Constructors
- Constructors
- why Constructors cannot be inherited in java ?(为什么构造方法不能继承)
- @Inherited
- inherited
- inherited
- Inheriting constructors - C++11, 23 of n
- C++11 FAQ中文版:委托构造函数(Delegating constructors)
- C++11 FAQ中文版:委托构造函数(Delegating constructors)
- Chain Constructors
- understanding constructors
- C++ Constructors
- Custom Constructors
- Static Constructors
- CPP-Constructors
- 深度理解链式前向星
- python 正则表达式 参照表
- 学习SSH三大框架(一)之struts2起步
- Jenkins
- Tomcat heap 配置案例
- C++11: inherited constructors
- mysql 性能优化方案
- Block
- Scalaz(19)- Monad: \/ - Monad 版本的 Either
- AlertDialog居中问题
- liunx CentOS 6.5 匿名用户 安装FTP
- Xcode快捷键
- Linux下文件的权限
- poj 2449 Remmarguts' Date(A*+Dijsktra 求第K短路)