zoukankan      html  css  js  c++  java
  • C++:(拷贝,继承,智能指针)练习

      1 #include <iostream>
      2 #include <string>
      3 #include <memory>
      4 #include <functional>
      5 #include <map>
      6 #include <vector>
      7 #include <set>
      8 
      9 class Quote
     10 {
     11     friend double print_total(std::ostream &os, const Quote &item, std::size_t n);
     12 private:
     13     std::string Isbn;
     14 protected:
     15     double price;
     16 public:
     17     Quote() = default;
     18 
     19     Quote(const std::string &BookNm, double o_price)
     20             : Isbn(BookNm),
     21               price(o_price)
     22     {
     23         std::cout << "Quot constructor" << std::endl;
     24     }
     25 
     26     Quote(const Quote &rhs)
     27             : Isbn(rhs.Isbn),
     28               price(rhs.price)
     29     {
     30         std::cout << "Quot copy constructor" << std::endl;
     31     }
     32 
     33 
     34     //赋值运算符完成析构和拷贝工作
     35     Quote &operator=(const Quote &rhs)
     36     {
     37         //考虑自赋值情况
     38         if (this != &rhs)
     39         {
     40             Isbn = rhs.Isbn;
     41             price = rhs.price;
     42 
     43         }
     44 
     45         return *this;
     46     }
     47 
     48     //主要不是动态内存,不需要做窃取资源
     49     Quote(Quote &&rhs)
     50             : Isbn(std::move(rhs.Isbn)),
     51               price(std::move(rhs.price))
     52     {
     53         std::cout << "Quote move copy constructor" << std::endl;
     54     }
     55 
     56     ~Quote()
     57     {
     58         std::cout << "~Quote()" << std::endl;
     59     }
     60 
     61 public:
     62     std::string IsBn() const
     63     {
     64         return Isbn;
     65     }
     66 
     67     //动态拷贝自己一份给智能指针
     68     virtual Quote *clon() const &
     69     {
     70         return new Quote(*this);
     71     }
     72 
     73     virtual Quote *clon() &&
     74     {
     75         return new Quote(std::move(*this));
     76     }
     77 
     78 
     79     //虚函数定义给子类自己定义价格的方式
     80     virtual double net_price(std::size_t n) const
     81     {
     82         return price * n;
     83     }
     84 
     85     virtual void Debug() const
     86     {
     87         std::cout << "This is Quote Class" << std::endl;
     88         std::cout << "ISBN: " << Isbn << std::endl;
     89         std::cout << "Price: " << price << std::endl;
     90     }
     91 
     92 
     93 };
     94 
     95 double print_total(std::ostream &os, const Quote &item, std::size_t n)
     96 {
     97     auto price_total = item.net_price(n);
     98     std::cout << "ISBN: " << item.IsBn() << std::endl;
     99     std::cout << "Bugs: " << n << " Price: " << price_total << std::endl;
    100 
    101     return price_total;
    102 }
    103 
    104 
    105 class bulk_quote : public Quote
    106 {
    107 private:
    108     double discount;
    109     std::size_t min_num;
    110 
    111 public:
    112     //继承基类构造,初始化基类部分
    113     //  using Quote::Quote;
    114 
    115     bulk_quote() = delete;
    116 
    117     bulk_quote(const std::string &bookNm, double o_price, double discount_, std::size_t min_num_)
    118             : Quote(bookNm, o_price),
    119               discount(discount_),
    120               min_num(min_num_)
    121     {
    122         std::cout << "bulk_quote constructor" << std::endl;
    123     }
    124 
    125     bulk_quote(const bulk_quote &rhs)
    126             : Quote(rhs)     //调用基类构造,用rhs的基类部分初始化自己的基类
    127     {
    128         //如果派生类有自己的数据可以在这
    129         discount = rhs.discount;
    130         min_num = rhs.min_num;
    131     }
    132 
    133     bulk_quote &operator=(const bulk_quote &rhs)
    134     {
    135         //基类赋值运算符使用rhs基类初始化
    136         Quote::operator=(rhs);
    137         discount = rhs.discount;
    138         min_num = rhs.min_num;
    139         return *this;
    140     }
    141 
    142     bulk_quote(bulk_quote &&rhs)
    143             : Quote(std::move(rhs))     //调用基类移动构造
    144     {
    145         discount = std::move(rhs.discount);
    146         min_num = std::move(rhs.min_num);
    147     }
    148 
    149     bulk_quote &operator=(bulk_quote &&rhs)
    150     {
    151         Quote::operator=(std::move(rhs));      //基类移动赋值运算符
    152         discount = std::move(rhs.discount);
    153         min_num = std::move(rhs.min_num);
    154         return *this;
    155     }
    156 
    157 public:
    158     double net_price(std::size_t n) const override
    159     {
    160         if (n >= min_num)
    161         {
    162             return price * (1 - discount) * n;
    163         } else
    164         {
    165             return price * n;
    166         }
    167     }
    168 
    169     void Debug() const override
    170     {
    171         std::cout << "This is bulk_quote Class" << std::endl;
    172         std::cout << "DISCOUNT: " << discount << std::endl;
    173         std::cout << "Min_qty: " << min_num << std::endl;
    174         std::cout << "Price: " << price << std::endl;
    175     }
    176 
    177 
    178     bulk_quote *clon() const &override
    179     {
    180         return new bulk_quote(*this);
    181     }
    182 
    183     bulk_quote *clon() &&override
    184     {
    185         return new bulk_quote(std::move(*this));
    186     }
    187 
    188 
    189 };
    190 
    191 
    192 class basket
    193 {
    194 public:
    195     void add_item(const Quote &quote)
    196     {
    197         items.insert(std::shared_ptr<Quote>(quote.clon()));
    198     }
    199 
    200     void add_item(Quote &&quote)
    201     {
    202         items.insert(std::shared_ptr<Quote>(std::move(quote).clon()));
    203     }
    204 
    205     void print_recent(std::ostream &os) const
    206     {
    207         auto sum_price = 0;
    208         for (auto iter = items.cbegin();
    209              iter != items.cend();
    210              iter = items.upper_bound(*iter))
    211         {
    212             sum_price += print_total(os, **iter, items.count(*iter));
    213         }
    214 
    215         std::cout << "			 total_price: " << sum_price << std::endl;
    216     }
    217 
    218 private:
    219     static bool compara(const std::shared_ptr<Quote> &c1, const std::shared_ptr<Quote> &c2)
    220     {
    221         return c1->IsBn() > c2->IsBn();
    222     }
    223 
    224     std::multiset<std::shared_ptr<Quote>, decltype(compara) * > items{compara};
    225 };
    226 
    227 
    228 int main(int argc, char *argv[])
    229 {
    230 
    231     Quote A("烤香肠", 6), B("炸面板", 7), C("其他食品", 10);
    232     bulk_quote D("面包", 5, 0.9, 5), E("可乐", 3, 0.8, 5);
    233 
    234     basket buy;
    235 
    236     buy.add_item(A);
    237     buy.add_item(B);
    238     buy.add_item(C);
    239 
    240     for (int i = 0; i < 5; i++)
    241     {
    242         buy.add_item(D);
    243         buy.add_item(E);
    244     }
    245 
    246     buy.print_recent(std::cout);
    247 
    248     return 0;
    249 }
  • 相关阅读:
    linux项目部署学习(1) pyhton3的虚拟环境virtualenv和virtualenvwrapper详解
    linux下pip查看安装包的路径、和指定安装目录
    python+selenium实现长截图
    selenium设置chrome浏览器保持登录方式两种options和cookie
    linux中离线安装django
    理解 bashrc 和 profile
    Django部署阿里云服务时候报错:SQLite 3.8.3 or later is required (found 3.7.17)
    Django的django-admin工具详解
    Yum Install --Downloadonly 离线下载依赖包研究
    分治法求n个点中的最近距离
  • 原文地址:https://www.cnblogs.com/xuaidongstdudyrecording/p/7205273.html
Copyright © 2011-2022 走看看