C++:(拷贝,继承,智能指针)练习
#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 "e)
{
items.insert(std::shared_ptr<Quote>(quote.clon()));
} void add_item(Quote &"e)
{
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++:(拷贝,继承,智能指针)练习的更多相关文章
- c/c++ 继承与多态 文本查询的小例子(非智能指针版本)
问题:在上一篇继承与多态 文本查询的小例子(智能指针版本)在Query类里使用的是智能指针,只把智能指针换成普通的指针,并不添加拷贝构造方法,会发生什么呢? 执行时,代码崩掉. 分析下面一行代码: Q ...
- c/c++ 继承与多态 文本查询的小例子(智能指针版本)
为了更好的理解继承和多态,做一个文本查询的小例子. 接口类:Query有2个方法. eval:查询,返回查询结果类QueryResult rep:得到要查询的文本 客户端程序的使用方法: //查询包含 ...
- C++ 拷贝控制和资源管理,智能指针的简单实现
C++ 关于拷贝控制和资源管理部分的笔记,并且介绍了部分C++ 智能指针的概念,然后实现了一个基于引用计数的智能指针.关于C++智能指针部分,后面会有专门的研究. 通常,管理类外资源的类必须定义拷贝控 ...
- C++智能指针详解
本文出自http://mxdxm.iteye.com/ 一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最 ...
- C++11智能指针
今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...
- 【转】C++ 智能指针详解
一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...
- c++ 中的8种智能指针[转]
一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...
- Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6786239 Android 系统的运行时库层代 ...
- shared_ptr智能指针源码剖析
(shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,读写操作不能原子化.根据文档 (http://www.boost.org/doc/ ...
随机推荐
- Python序列之元组 (tuple)
作者博文地址:http://www.cnblogs.com/spiritman/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号, ...
- iframe子页面position的fixed
前言: 首先说一说我昨天天的苦逼经历.中午吃饭时一同事跟我说,他做的项目嵌套iframe后,子页面的position设置fixed失效了. 经过反复询问,得知他用了两层iframe,再加上最外的父页面 ...
- 20162325 金立清 S2 W9 C18
20162325 2017-2018-2 <程序设计与数据结构>第9周学习总结 教材学习内容概要 堆是一棵完全二叉树,其中每个元素大于等于其所有子结点的值. 向堆中添加一个元素的方法是,首 ...
- 团队项目--NABCD模型
蹭课神器 N(need需求) 每个人的兴趣都不一样,或许你很喜欢自己的专业,并且想再进一步学习专业知识:或许你不是太喜欢自己的专业,想上一些自己感兴趣的课程:但是,当你想上你所钟 意的课时,却发现自己 ...
- Beta 冲刺1
队名:日不落战队 安琪(队长) 过去两天完成了那些任务 修改个人信息界面. 修改手写涂鸦界面. 接下来的任务 改进手写涂鸦,加入其他功能. 还剩下的任务 社交模块功能. 遇到的困难 无. 有哪些收获和 ...
- Leetcode题库——15.三数之和
@author: ZZQ @software: PyCharm @file: threeSum.py @time: 2018/10/6 19:47 说明:给定一个包含 n 个整数的数组 nums,判断 ...
- 学习总结:jQuery插件开发模式和结构
学习博客链接: ①https://www.cnblogs.com/cyStyle/ ② https://www.cnblogs.com/chengyunshen/p/7277305.html ③ ht ...
- jquery-numberformatter插件
项目地址:https://code.google.com/p/jquery-numberformatter/ 非jquery版:https://github.com/andrewgp/jsNumber ...
- 浏览器播放rtmp流
我是利用flash插件实现的,需要以下几个文件: flowplayer-3.2.8.min.js flowplayer-3.2.18.swf flowplayer.rtmp-3.2.8.swf flo ...
- 获取SQL Server中连接的客户端IP地址[转]
有时候需要获取连接到SQL Server服务器上的客户端IP地址,用什么办法呢? SELECT *FROM sys.dm_exec_connections WHERE session_id = @@S ...