码迷,mamicode.com
首页 > 编程语言 > 详细

C++primer 15.7.3节练习

时间:2017-09-16 23:22:21      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:tor   operator   ble   赋值   color   default   span   names   override   

练习15.26

写的时候不小心写到了派生类Disc_quote,其实是一样的,主要明白原理即可

  1 #include <iostream>
  2 #include <string>
  3 #include <utility>
  4 #include <memory>
  5 #include <vector>
  6 
  7 using namespace std;
  8 
  9 class Quote {
 10 public:
 11     Quote() : bookNo(""), price(0.0) { cout << "默认构造函数" << endl; }
 12     Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) { cout << "构造函数"<< endl; }
 13     Quote(const Quote& q) : bookNo(q.bookNo), price(q.price) { cout << "拷贝构造函数" << endl; }
 14     Quote(Quote &&q) noexcept : bookNo(q.bookNo), price(q.price) { cout << "移动构造函数" << endl; }
 15     Quote& operator=(const Quote& rhs);
 16     Quote& operator=(Quote&& rhs) noexcept;
 17     const string& isbn() const { return bookNo; }
 18     virtual double net_price(size_t n) const { return n * price; }
 19     virtual ~Quote() = default;
 20 private:
 21     string bookNo;
 22 protected:
 23     double price = 0.0;
 24 };
 25 
 26 class Disc_quote : public Quote {
 27 public:
 28     Disc_quote() : Quote() { cout << "派生类默认构造函数" << endl; }
 29     Disc_quote(const string &book, double sales_price, size_t quan, double dis) : Quote(book, sales_price), quantity(quan), discount(dis) { cout << "派生类构造函数" <<endl; }
 30     Disc_quote(const Disc_quote& d) : Quote(d), quantity(d.quantity), discount(d.discount) { cout << "派生类拷贝构造函数" <<endl; }
 31     Disc_quote(Disc_quote&& d) : Quote(std::move(d)), quantity(d.quantity), discount(d.discount) { cout << "派生类移动构造函数" << endl; }
 32     Disc_quote& operator=(const Disc_quote& rhs);
 33     Disc_quote& operator=(Disc_quote&& rhs);
 34     double net_price(size_t n) const override;
 35     ~Disc_quote() { cout << "派生类析构函数" << endl; }
 36 protected:
 37     size_t quantity = 0;
 38     double discount = 0.0;
 39 };
 40 
 41 class Bulk_quote : public Disc_quote {
 42 public:
 43     Bulk_quote() = default;
 44     Bulk_quote(const string& book, double p, size_t qty, double disc) : Disc_quote(book, p, qty, disc) {}
 45     double net_price(size_t n) const override;
 46 };
 47 
 48 class lim_discount : public Disc_quote {
 49 public:
 50     lim_discount() = default;
 51     lim_discount(const string& book, double p, size_t qty, double disc) : Disc_quote(book, p, qty, disc) {}
 52     double net_price(size_t n) const override;
 53 };
 54 
 55 double print_total(ostream& os, const Quote& item, size_t t);
 56 
 57 int main()
 58 {
 59     Quote q1;
 60     Quote q2("C++ primer", 128.00);
 61     Quote q3(q2);
 62     Quote q4(std::move(q2));
 63     print_total(cout, q2, 2);
 64     q1 = q2;
 65     q1 = std::move(q2);
 66     Disc_quote d1;
 67     Disc_quote d2("C++ plus", 100.32, 5, 0.6);
 68     Disc_quote d3(d2);
 69     Disc_quote d4(std::move(d2));
 70     d1 = d3;
 71     d1 = std::move(d3);
 72     system("pause");
 73     return 0;
 74 }
 75 
 76 double print_total(ostream & os, const Quote &item, size_t t)
 77 {
 78     double ret = item.net_price(t);
 79     os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl;
 80     return ret;
 81 }
 82 
 83 double Bulk_quote::net_price(size_t n) const
 84 {
 85     if (n >= quantity)
 86         return n * (1 - discount) * price;
 87     else
 88         return n * price;
 89 }
 90 
 91 double lim_discount::net_price(size_t n) const
 92 {
 93     if (n <= quantity)
 94         return n * (1 - discount) * price;
 95     else
 96         return Quote::net_price(n);
 97 }
 98 
 99 Quote & Quote::operator=(const Quote & rhs)
100 {
101     bookNo = rhs.bookNo;
102     price = rhs.price;
103     cout << "拷贝赋值运算符" << endl;
104     return *this;
105     // TODO: 在此处插入 return 语句
106 }
107 
108 Quote & Quote::operator=(Quote && rhs) noexcept
109 {
110     bookNo = rhs.bookNo;
111     price = rhs.price;
112     cout << "移动赋值运算符" << endl;
113     return *this;
114     // TODO: 在此处插入 return 语句
115 }
116 
117 Disc_quote & Disc_quote::operator=(const Disc_quote & rhs)
118 {
119     Quote::operator=(rhs);
120     quantity = rhs.quantity;
121     discount = rhs.discount;
122     cout << "派生类的拷贝构造运算符" << endl;
123     return *this;
124     // TODO: 在此处插入 return 语句
125 }
126 
127 Disc_quote & Disc_quote::operator=(Disc_quote && rhs)
128 {
129     Quote::operator=(rhs);
130     quantity = rhs.quantity;
131     discount = rhs.discount;
132     cout << "派生类移动构造运算符" << endl;
133     return *this;
134     // TODO: 在此处插入 return 语句
135 }
136 
137 double Disc_quote::net_price(size_t n) const
138 {
139     if (n >= quantity)
140         return n * (1 - discount) * price;
141     else
142         return n * price;
143 }

 

C++primer 15.7.3节练习

标签:tor   operator   ble   赋值   color   default   span   names   override   

原文地址:http://www.cnblogs.com/wuyinfenghappy/p/7533156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!