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 &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 ;
}
上一篇:ORM介绍(字段 和 字段的参数)


下一篇:2021-03-26