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>
 6
 7 using namespace std;
 8
 9 class Quote {
10 public:
11     Quote() = default;
12     Quote(const string &book, double sales_price) : bookNo(book), price(sales_price) {}
13     string isbn() { return bookNo; }
14     virtual double net_price(size_t n) const{ return n * price; }
15     virtual ~Quote() = default;
16 private:
17     string bookNo;
18 protected:
19     double price = 0.0;
20 };
21
22 class Bulk_quote : public Quote {
23 public:
24     Bulk_quote() = default;
25     Bulk_quote(const string& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {}
26     double net_price(size_t n) const;
27 private:
28     size_t min_qty = 0;
29     double discount = 0.0;
30 };
31
32 double print_total(ostream& os, const Quote& item, size_t t);
33
34 int main()
35 {
36
37     system("pause");
38     return 0;
39 }
40
41 double print_total(ostream & os, const Quote &item, size_t t)
42 {
43     double ret = item.net_price(t);
44     os << "ISBN: " << item.isbn << " # sold: " << t << " total due: " << ret << endl;
45     return ret;
46 }
47
48 double Bulk_quote::net_price(size_t n) const
49 {
50     if (n >= min_qty)
51         return n * (1 - discount) * price;
52     else
53         return n * price;
54 }

练习15.6

 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, double sales_price) : bookNo(book), price(sales_price) {}
13     const string& isbn() const { return bookNo; }
14     virtual double net_price(size_t n) const{ return n * price; }
15     virtual ~Quote() = default;
16 private:
17     string bookNo;
18 protected:
19     double price = 0.0;
20 };
21
22 class Bulk_quote : public Quote {
23 public:
24     Bulk_quote() = default;
25     Bulk_quote(const string& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {}
26     double net_price(size_t n) const;
27 private:
28     size_t min_qty = 0;
29     double discount = 0.0;
30 };
31
32 double print_total(ostream& os, const Quote& item, size_t t);
33
34 int main()
35 {
36     Quote q1;
37     Quote q2("C++ primer", 128.00);
38     Bulk_quote b1;
39     Bulk_quote b2("C++ primer", 128.00, 5, 0.4);
40     print_total(cout, q1, 6);
41     print_total(cout, q2, 6);
42     print_total(cout, b1, 6);
43     print_total(cout, b2, 6);
44     system("pause");
45     return 0;
46 }
47
48 double print_total(ostream & os, const Quote &item, size_t t)
49 {
50     double ret = item.net_price(t);
51     os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl;
52     return ret;
53 }
54
55 double Bulk_quote::net_price(size_t n) const
56 {
57     if (n >= min_qty)
58         return n * (1 - discount) * price;
59     else
60         return n * price;
61 }

练习15.7

 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, double sales_price) : bookNo(book), price(sales_price) {}
13     const string& isbn() const { return bookNo; }
14     virtual double net_price(size_t n) const{ return n * price; }
15     virtual ~Quote() = default;
16 private:
17     string bookNo;
18 protected:
19     double price = 0.0;
20 };
21
22 class Bulk_quote : public Quote {
23 public:
24     Bulk_quote() = default;
25     Bulk_quote(const string& book, double p, size_t qty, double disc) : Quote(book, p), min_qty(qty), discount(disc) {}
26     double net_price(size_t n) const;
27 private:
28     size_t min_qty = 0;
29     double discount = 0.0;
30 };
31
32 class lim_discount : public Quote {
33 public:
34     lim_discount() = default;
35     lim_discount(const string& book, double p, size_t lcount, double ldis) : Quote(book, p), lim_count(lcount), lim_dis(ldis) {}
36     double net_price(size_t n) const override;
37 private:
38     size_t lim_count = 0;
39     double lim_dis = 0.0;
40 };
41 double print_total(ostream& os, const Quote& item, size_t t);
42
43 int main()
44 {
45     Quote q1;
46     Quote q2("C++ primer", 128.00);
47     Bulk_quote b1;
48     Bulk_quote b2("C++ primer", 128.00, 5, 0.4);
49     print_total(cout, q1, 6);
50     print_total(cout, q2, 6);
51     print_total(cout, b1, 6);
52     print_total(cout, b2, 6);
53     lim_discount l1("C++ primer", 128.00, 10, 0.5);
54     print_total(cout, l1, 9);
55     print_total(cout, l1, 11);
56     system("pause");
57     return 0;
58 }
59
60 double print_total(ostream & os, const Quote &item, size_t t)
61 {
62     double ret = item.net_price(t);
63     os << "ISBN: " << item.isbn() << " # sold: " << t << " total due: " << ret << endl;
64     return ret;
65 }
66
67 double Bulk_quote::net_price(size_t n) const
68 {
69     if (n >= min_qty)
70         return n * (1 - discount) * price;
71     else
72         return n * price;
73 }
74
75 double lim_discount::net_price(size_t n) const
76 {
77     if (n <= lim_count)
78         return n * (1 - lim_dis) * price;
79     else
80         return Quote::net_price(n);
81 }
时间: 2024-10-05 04:40:36

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

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(

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-