#include <iostream>
#include <string>
#include <memory>
#include <functional>
#include <map>
#include <vector>
#include <set> class Quote
{
friend double print_total(std::ostream &os, const Quote &item, std::size_t n);
private:
std::string Isbn;
protected:
double price;
public:
Quote() = default; Quote(const std::string &BookNm, double o_price)
: Isbn(BookNm),
price(o_price)
{
std::cout << "Quot constructor" << std::endl;
} Quote(const Quote &rhs)
: Isbn(rhs.Isbn),
price(rhs.price)
{
std::cout << "Quot copy constructor" << std::endl;
} //赋值运算符完成析构和拷贝工作
Quote &operator=(const Quote &rhs)
{
//考虑自赋值情况
if (this != &rhs)
{
Isbn = rhs.Isbn;
price = rhs.price; } return *this;
} //主要不是动态内存,不需要做窃取资源
Quote(Quote &&rhs)
: Isbn(std::move(rhs.Isbn)),
price(std::move(rhs.price))
{
std::cout << "Quote move copy constructor" << std::endl;
} ~Quote()
{
std::cout << "~Quote()" << std::endl;
} public:
std::string IsBn() const
{
return Isbn;
} //动态拷贝自己一份给智能指针
virtual Quote *clon() const &
{
return new Quote(*this);
} virtual Quote *clon() &&
{
return new Quote(std::move(*this));
} //虚函数定义给子类自己定义价格的方式
virtual double net_price(std::size_t n) const
{
return price * n;
} virtual void Debug() const
{
std::cout << "This is Quote Class" << std::endl;
std::cout << "ISBN: " << Isbn << std::endl;
std::cout << "Price: " << price << std::endl;
} }; double print_total(std::ostream &os, const Quote &item, std::size_t n)
{
auto price_total = item.net_price(n);
std::cout << "ISBN: " << item.IsBn() << std::endl;
std::cout << "Bugs: " << n << " Price: " << price_total << std::endl; return price_total;
} class bulk_quote : public Quote
{
private:
double discount;
std::size_t min_num; public:
//继承基类构造,初始化基类部分
// using Quote::Quote; bulk_quote() = delete; bulk_quote(const std::string &bookNm, double o_price, double discount_, std::size_t min_num_)
: Quote(bookNm, o_price),
discount(discount_),
min_num(min_num_)
{
std::cout << "bulk_quote constructor" << std::endl;
} bulk_quote(const bulk_quote &rhs)
: Quote(rhs) //调用基类构造,用rhs的基类部分初始化自己的基类
{
//如果派生类有自己的数据可以在这
discount = rhs.discount;
min_num = rhs.min_num;
} bulk_quote &operator=(const bulk_quote &rhs)
{
//基类赋值运算符使用rhs基类初始化
Quote::operator=(rhs);
discount = rhs.discount;
min_num = rhs.min_num;
return *this;
} bulk_quote(bulk_quote &&rhs)
: Quote(std::move(rhs)) //调用基类移动构造
{
discount = std::move(rhs.discount);
min_num = std::move(rhs.min_num);
} bulk_quote &operator=(bulk_quote &&rhs)
{
Quote::operator=(std::move(rhs)); //基类移动赋值运算符
discount = std::move(rhs.discount);
min_num = std::move(rhs.min_num);
return *this;
} public:
double net_price(std::size_t n) const override
{
if (n >= min_num)
{
return price * ( - discount) * n;
} else
{
return price * n;
}
} void Debug() const override
{
std::cout << "This is bulk_quote Class" << std::endl;
std::cout << "DISCOUNT: " << discount << std::endl;
std::cout << "Min_qty: " << min_num << std::endl;
std::cout << "Price: " << price << std::endl;
} bulk_quote *clon() const &override
{
return new bulk_quote(*this);
} bulk_quote *clon() &&override
{
return new bulk_quote(std::move(*this));
} }; class basket
{
public:
void add_item(const Quote &quote)
{
items.insert(std::shared_ptr<Quote>(quote.clon()));
} void add_item(Quote &&quote)
{
items.insert(std::shared_ptr<Quote>(std::move(quote).clon()));
} void print_recent(std::ostream &os) const
{
auto sum_price = ;
for (auto iter = items.cbegin();
iter != items.cend();
iter = items.upper_bound(*iter))
{
sum_price += print_total(os, **iter, items.count(*iter));
} std::cout << "\t\t\t total_price: " << sum_price << std::endl;
} private:
static bool compara(const std::shared_ptr<Quote> &c1, const std::shared_ptr<Quote> &c2)
{
return c1->IsBn() > c2->IsBn();
} std::multiset<std::shared_ptr<Quote>, decltype(compara) * > items{compara};
}; int main(int argc, char *argv[])
{ Quote A("烤香肠", ), B("炸面板", ), C("其他食品", );
bulk_quote D("面包", , 0.9, ), E("可乐", , 0.8, ); basket buy; buy.add_item(A);
buy.add_item(B);
buy.add_item(C); for (int i = ; i < ; i++)
{
buy.add_item(D);
buy.add_item(E);
} buy.print_recent(std::cout); return ;
}

C++:(拷贝,继承,智能指针)练习的更多相关文章

  1. c/c++ 继承与多态 文本查询的小例子(非智能指针版本)

    问题:在上一篇继承与多态 文本查询的小例子(智能指针版本)在Query类里使用的是智能指针,只把智能指针换成普通的指针,并不添加拷贝构造方法,会发生什么呢? 执行时,代码崩掉. 分析下面一行代码: Q ...

  2. c/c++ 继承与多态 文本查询的小例子(智能指针版本)

    为了更好的理解继承和多态,做一个文本查询的小例子. 接口类:Query有2个方法. eval:查询,返回查询结果类QueryResult rep:得到要查询的文本 客户端程序的使用方法: //查询包含 ...

  3. C++ 拷贝控制和资源管理,智能指针的简单实现

    C++ 关于拷贝控制和资源管理部分的笔记,并且介绍了部分C++ 智能指针的概念,然后实现了一个基于引用计数的智能指针.关于C++智能指针部分,后面会有专门的研究. 通常,管理类外资源的类必须定义拷贝控 ...

  4. C++智能指针详解

    本文出自http://mxdxm.iteye.com/ 一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最 ...

  5. C++11智能指针

    今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...

  6. 【转】C++ 智能指针详解

    一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...

  7. c++ 中的8种智能指针[转]

    一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...

  8. Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6786239 Android 系统的运行时库层代 ...

  9. shared_ptr智能指针源码剖析

    (shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,读写操作不能原子化.根据文档 (http://www.boost.org/doc/ ...

随机推荐

  1. mysql和oracle查询出的一条结果中的多个字段拼接

    1,mysql concat('a','b','c')和concat_ws('a','b','c')的区别:前者如果有某个值为空,结果为空;后者如果有某个值为空,可以忽略这个控制 SELECT con ...

  2. TeamWork#3,Week5,Scrum Meeting 11.6, 11.7, 11.11, 11.12

    11.6:到目前为止基本已完成相关知识的学习,各方面工作都开始进行,一开始进行比较慢. 11.7:项目遇到困难,需要补充相关知识,进度慢了下来. 11.11:各方面工作进展比较顺利,没有什么大问题. ...

  3. Daily Scrum 11.14

    姓名 今日任务 黄新越 按照热度排序->产生柱状图 刘垚鹏 总体代码架构整合 王骜 总体代码架构整合 林旭鹏 优化整体UI布局 安康 优化整体UI布局 黄伟龙 预先合作编写测试用例 马佐霖 预先 ...

  4. 【每日scrum】第一次冲刺day2

    和小伙伴一起找地图 ,学习了mapinfo地图格式的基本知识,数据和图像分开存储

  5. 《Spring1之第七次站立会议》

    <第七次站立会议> 昨天:我把自己项目工程里的服务器端界面进行了优化和完善. 今天:我查找了关于实现视频功能的相关代码. 遇到的问题:找到的都是基于C#的相关代码,很难找到用java实现的 ...

  6. 进阶系列(10)—— C#元数据和动态编程

    一.元数据的介绍 元数据是用来描述数据的数据(Data that describes other data).单单这样说,不太好理解,我来举个例子.下面是契诃夫的小说<套中人>中的一段,描 ...

  7. wamp 修改www目录

    我的情况 Wamp版本:2.2 WAMP2.2安装目录:C:/ www目录:D:/wamp/www/ 变更目录:E:/HbuilderProjects/ 一 主要过程: (1)修改 D:\wamp\b ...

  8. java集合ArrayList

    基于jdk_1.8.0 关于List,主要是有序的可重复的数据结构.jdk主要实现类有ArrayList(底层使用数组).LinkedList(底层使用双向链表) ArrayList: (一)继承关系 ...

  9. visual stdio2013软件安装及单元测试

    visual stdio2013软件安装及单元测试 一.visual stdio2013软件安装详解 今天,笔者为大家带来如何在Windows10下完美安装Visual Studio 2013专业版. ...

  10. 怎样利用好单片机上的存储器资源来实现OD的存储与访问

    转自:http://www.cnblogs.com/winshton/p/4897789.html 我们知道OD(对象字典)是CANopen的核心,所有功能都是围绕它开展的,是协议栈的数据中心,良好的 ...