C++primer 15.7.3节练习

练习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 }
时间: 2024-10-24 00:41:41

C++primer 15.7.3节练习的相关文章

C++primer 15.2.2节练习

练习15.4 a)错误,一个类不能派生它本身 b)正确,Derived从他的基类Base派生,且规定派生类从基类继承的数据成员对于派生类的用户是不可见. c)错误,派生类的声明与其他类相差不大,声明中包含类名但是不包含他的派生列表: 练习15.5 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector>

C++primer 15.7.4节练习

练习15.27 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 <<

C++primer 15.2.1节练习

练习15.1 基类将类型相关的函数与派生类不做改变直接继承的函数区别对待,对于某些函数,基类希望他的派生类个自定义适合自身的版本,此时基类就将这些函数声明成虚函数. 练习15.2 protected:允许类的派生类访问其成员,而不允许其他用户访问 private:禁止所有用户包括其派生类访问其私有成员 练习15.3 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <

C++primer 15.2.3节练习

练习15.8 静态类型:对象在声明时采用的类型,是在编译期确定的 动态类型:目前所指对象的类型,是在运行时确定的 练习15.9 第一种情况:当对象的静态类型和对象的动态类型之间存在隐式的转换的情况下,静态类型可能与动态类型不同 第二种:当基类指针或者基类引用指向派生类对象的时候 练习15.10 ifsteam定义一个文件输入流对象,将输入流对象所输入写到sales_data类对象中.

C++primer 15.3节练习

练习15.11 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() = default; 12 Quote(const string &book, doubl

C++primer 15.8节练习

练习15.28 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 <<

C++primer 15.4节练习

练习15.15.练习15.16 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() = default; 12 Quote(const string &boo

C++primer 15.5节练习

练习15.18 只有当派生类公有继承基类时,用户代码才能使用派生类向基类的转换:B &tb=D; Base *p=&d1; 正确,Pub_Derv是公有继承Base p=&d2; 错误,Priv_Derv是私有继承Base p=&d3; 错误,Prot_Dery是保护继承Base p=&dd1; 正确,Derived_from_Public是公有继承于Pub_Dery的 p=&dd2; 错误,Priv_Derv是私有继承于Base p=&dd3; 错

C++primer 15.6节练习

练习15.23 1 class Base { 2 public: 3 virtual int fcn(); 4 }; 5 6 class D1 : public Base { 7 public: 8 int fcn(); 9 virtual void f2(); 10 }; 11 12 class D2 : public D1 { 13 public: 14 int fcn(int); 15 int fcn(); 16 void f2(); 17 }; 18 跟bp2有关的需要重新解析:bp2-