【C++ Primer Plus】编程练习答案——第13章

  1 // chatper13_1_cd.h
  2 
  3 #ifndef LEARN_CPP_CHAPTER13_1_CD_H
  4 #define LEARN_CPP_CHAPTER13_1_CD_H
  5 
  6 
  7 class Cd {
  8 private:
  9     char performers[50];
 10     char label[20];
 11     int selections;
 12     double playtime;
 13 public:
 14     Cd(char * s1, char * s2, int n, double x);
 15     Cd(const Cd & d);
 16     Cd();
 17     virtual ~Cd();
 18     virtual void Report() const;
 19     Cd & operator=(const Cd & d);
 20 };
 21 
 22 class Classic : public Cd {
 23 private:
 24     char mainworks[100];
 25 public:
 26     Classic(char * mw, char * s1, char * s2, int n, double x);
 27     Classic(const Classic & cl);
 28     Classic();
 29     virtual ~Classic();
 30     virtual void Report() const;
 31     Classic & operator=(const Classic & cl);
 32 };
 33 
 34 
 35 #endif //LEARN_CPP_CHAPTER13_1_CD_H
 36 
 37 
 38 // chapter13_1_cd.cpp
 39 
 40 #include "chapter13_1_cd.h"
 41 #include <cstring>
 42 #include <iostream>
 43 
 44 Cd::Cd(char *s1, char *s2, int n, double x) {
 45     strcpy(performers, s1);
 46     strcpy(label, s2);
 47     selections = n;
 48     playtime = x;
 49 }
 50 
 51 Cd::Cd(const Cd &d) {
 52     strcpy(performers, d.performers);
 53     strcpy(label, d.label);
 54     selections = d.selections;
 55     playtime = d.playtime;
 56 }
 57 
 58 Cd::Cd() {
 59     strcpy(performers, "none");
 60     strcpy(label, "none");
 61     selections = 0;
 62     playtime = 0;
 63 }
 64 
 65 Cd::~Cd() {
 66  // nothing to do
 67 }
 68 
 69 void Cd::Report() const {
 70     using namespace std;
 71     cout << "performers: " << performers << endl;
 72     cout << "label: " << label << endl;
 73     cout << "selections: " << selections << endl;
 74     cout << "playtime: " << playtime << endl;
 75 }
 76 
 77 Cd &Cd::operator=(const Cd &d) {
 78     if (this == &d)
 79         return *this;
 80     strcpy(performers, d.performers);
 81     strcpy(label, d.label);
 82     selections = d.selections;
 83     playtime = d.playtime;
 84     return *this;
 85 }
 86 
 87 Classic::Classic(char *mw, char *s1, char *s2, int n, double x)
 88     : Cd(s1, s2, n, x) {
 89     strcpy(mainworks, mw);
 90 }
 91 
 92 Classic::Classic(const Classic &cl)
 93     : Cd(cl) {
 94     strcpy(mainworks, cl.mainworks);
 95 }
 96 
 97 Classic::Classic()
 98     : Cd() {
 99     strcpy(mainworks, "none");
100 }
101 
102 Classic::~Classic() {
103     // nothing to do
104 }
105 
106 void Classic::Report() const {
107     using namespace std;
108     Cd::Report();
109     cout << "mainworks: " << mainworks << endl;
110 }
111 
112 Classic &Classic::operator=(const Classic &cl) {
113     if (this == &cl)
114         return *this;
115     Cd::operator=(cl);
116     strcpy(mainworks, cl.mainworks);
117     return *this;
118 }
119 
120 // run
121 
122 void ch13_1() {
123     using namespace std;
124 
125     Cd c1("Beatles", "Captiol", 14, 35.5);
126     Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
127     Cd * pcd = &c1;
128 
129     cout << "Using object directly:\n";
130     c1.Report();
131     c2.Report();
132 
133     cout << "Using type cd * pointer to objects:\n";
134     pcd -> Report();
135     pcd = &c2;
136     pcd -> Report();
137 
138     cout << "Calling a function with a Cd reference argument:\n";
139     ch13_1_Bravo(c1);
140     ch13_1_Bravo(c2);
141 
142     cout << "Testing assignment: ";
143     Classic copy;
144     copy = c2;
145     copy.Report();
146 }
  1 // chapter13_2_cd.h
  2 
  3 #ifndef LEARN_CPP_CHAPTER13_2_CD_H
  4 #define LEARN_CPP_CHAPTER13_2_CD_H
  5 
  6 class Cd2 {
  7 private:
  8     char * performers;
  9     char * label;
 10     int selections;
 11     double playtime;
 12 public:
 13     Cd2(char * s1, char * s2, int n, double x);
 14     Cd2(const Cd2 & d);
 15     Cd2();
 16     virtual ~Cd2();
 17     virtual void Report() const;
 18     Cd2 & operator=(const Cd2 & d);
 19 };
 20 
 21 class Classic2 : public Cd2 {
 22 private:
 23     char * mainworks;
 24 public:
 25     Classic2(char * mw, char * s1, char * s2, int n, double x);
 26     Classic2(const Classic2 & cl);
 27     Classic2();
 28     virtual ~Classic2();
 29     virtual void Report() const;
 30     Classic2 & operator=(const Classic2 & cl);
 31 };
 32 
 33 
 34 #endif //LEARN_CPP_CHAPTER13_2_CD_H
 35 
 36 // chapter13_2_cd.cpp
 37 
 38 #include "chapter13_2_cd.h"
 39 #include <cstring>
 40 #include <iostream>
 41 
 42 Cd2::Cd2(char *s1, char *s2, int n, double x) {
 43     performers = new char[strlen(s1) + 1];
 44     strcpy(performers, s1);
 45     label = new char[strlen(s2) + 1];
 46     strcpy(label, s2);
 47     selections = n;
 48     playtime = x;
 49 }
 50 
 51 Cd2::Cd2(const Cd2 &d) {
 52     performers = new char[strlen(d.performers) + 1];
 53     strcpy(performers, d.performers);
 54     label = new char[strlen(d.label) + 1];
 55     strcpy(label, d.label);
 56     selections = d.selections;
 57     playtime = d.playtime;
 58 }
 59 
 60 Cd2::Cd2() {
 61     performers = new char[5];
 62     strcpy(performers, "none");
 63     label = new char[5];
 64     strcpy(label, "none");
 65     selections = 0;
 66     playtime = 0;
 67 }
 68 
 69 Cd2::~Cd2() {
 70     delete [] performers;
 71     delete [] label;
 72 }
 73 
 74 void Cd2::Report() const {
 75     using namespace std;
 76     cout << "performers: " << performers << endl;
 77     cout << "label: " << label << endl;
 78     cout << "selections: " << selections << endl;
 79     cout << "playtime: " << playtime << endl;
 80 }
 81 
 82 Cd2 &Cd2::operator=(const Cd2 &d) {
 83     if (this == &d)
 84         return *this;
 85     delete [] performers;
 86     delete [] label;
 87     performers = new char[strlen(d.performers) + 1];
 88     strcpy(performers, d.performers);
 89     label = new char[strlen(d.label) + 1];
 90     strcpy(label, d.label);
 91     selections = d.selections;
 92     playtime = d.playtime;
 93     return *this;
 94 }
 95 
 96 Classic2::Classic2(char *mw, char *s1, char *s2, int n, double x)
 97     : Cd2(s1, s2, n, x) {
 98     mainworks = new char[strlen(mw) + 1];
 99     strcpy(mainworks, mw);
100 }
101 
102 Classic2::Classic2(const Classic2 &cl)
103     : Cd2(cl) {
104     mainworks = new char[strlen(cl.mainworks) + 1];
105     strcpy(mainworks, cl.mainworks);
106 }
107 
108 Classic2::Classic2()
109     : Cd2() {
110     mainworks = new char[5];
111     strcpy(mainworks, "none");
112 }
113 
114 Classic2::~Classic2() {
115     delete [] mainworks;
116 }
117 
118 void Classic2::Report() const {
119     using namespace std;
120     Cd2::Report();
121     cout << "mainworks: " << mainworks << endl;
122 }
123 
124 Classic2 &Classic2::operator=(const Classic2 &cl) {
125     if (this == & cl)
126         return *this;
127     Cd2::operator=(cl);
128     delete [] mainworks;
129     mainworks = new char[strlen(cl.mainworks) + 1];
130     strcpy(mainworks, cl.mainworks);
131     return *this;
132 }
133 
134 // run
135 
136 void ch13_2() {
137     using namespace std;
138 
139     Cd2 c1("Beatles", "Captiol", 14, 35.5);
140     Classic2 c2 = Classic2("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
141     Cd2 * pcd = &c1;
142 
143     cout << "Using object directly:\n";
144     c1.Report();
145     c2.Report();
146 
147     cout << "Using type cd * pointer to objects:\n";
148     pcd -> Report();
149     pcd = &c2;
150     pcd -> Report();
151 
152     cout << "Calling a function with a Cd reference argument:\n";
153     ch13_2_Bravo(c1);
154     ch13_2_Bravo(c2);
155 
156     cout << "Testing assignment: ";
157     Classic2 copy;
158     copy = c2;
159     copy.Report();
160 }
  1 // chapter13_3_dma.h
  2 
  3 #ifndef LEARN_CPP_CHAPTER13_3_DMA_H
  4 #define LEARN_CPP_CHAPTER13_3_DMA_H
  5 
  6 #include <iostream>
  7 
  8 class DMA {
  9 private:
 10 public:
 11     virtual void view() = 0;
 12 };
 13 
 14 
 15 
 16 class baseDMA : public DMA{
 17 private:
 18     char * label;
 19     int rating;
 20 public:
 21     baseDMA(const char * l = "null", int r = 0);
 22     baseDMA(const baseDMA & rs);
 23     virtual ~baseDMA();
 24     baseDMA & operator=(const baseDMA & rs);
 25     friend std::ostream & operator<<(std::ostream & os, const baseDMA & rs);
 26     virtual void view();
 27 };
 28 
 29 class lacksDMA : public baseDMA {
 30 private:
 31     enum {COL_LEN = 40};
 32     char color[COL_LEN];
 33 public:
 34     lacksDMA(const char * l = "null", int r = 0, const char * c = "blank");
 35     lacksDMA(const lacksDMA & rs);
 36     lacksDMA & operator=(const lacksDMA & rs);
 37     virtual ~lacksDMA();
 38     friend std::ostream & operator<<(std::ostream & os, const lacksDMA & rs);
 39     virtual void view();
 40 };
 41 
 42 class hasDMA : public baseDMA {
 43 private:
 44     char * style;
 45 public:
 46     hasDMA(const char * l = "null", int r = 0, const char * s = "none");
 47     hasDMA(const hasDMA & rs);
 48     virtual ~hasDMA();
 49     hasDMA & operator=(const hasDMA & rs);
 50     friend std::ostream & operator<<(std::ostream & os, const hasDMA & rs);
 51     virtual void view();
 52 };
 53 
 54 
 55 #endif //LEARN_CPP_CHAPTER13_3_DMA_H
 56 
 57 
 58 // chapter13_3_dma.cpp
 59 
 60 #include "chapter13_3_dma.h"
 61 #include <cstring>
 62 
 63 
 64 baseDMA::baseDMA(const char *l, int r) {
 65     label = new char[strlen(l) + 1];
 66     strcpy(label, l);
 67     rating = r;
 68 }
 69 
 70 baseDMA::baseDMA(const baseDMA &rs) {
 71     label = new char[strlen(rs.label) + 1];
 72     strcpy(label, rs.label);
 73     rating = rs.rating;
 74 }
 75 
 76 baseDMA::~baseDMA() {
 77     delete [] label;
 78 }
 79 
 80 baseDMA &baseDMA::operator=(const baseDMA &rs) {
 81     if (this == &rs)
 82         return *this;
 83     delete [] label;
 84     label = new char[strlen(rs.label) + 1];
 85     strcpy(label, rs.label);
 86     rating = rs.rating;
 87     return *this;
 88 }
 89 
 90 std::ostream & operator<<(std::ostream & os, const baseDMA & rs) {
 91     os << "label: " << rs.label << std::endl << "rating: " << rs.rating << std::endl;
 92     return os;
 93 }
 94 
 95 void baseDMA::view() {
 96     using namespace std;
 97     cout << "label: " << label << endl << "rating: " << rating << endl;
 98 }
 99 
100 lacksDMA::lacksDMA(const char *l, int r, const char *c)
101     : baseDMA(l, r){
102     strcpy(color, c);
103 }
104 
105 lacksDMA::lacksDMA(const lacksDMA &rs)
106     : baseDMA(rs){
107     strcpy(color, rs.color);
108 }
109 
110 lacksDMA &lacksDMA::operator=(const lacksDMA &rs) {
111     if (this == &rs)
112         return *this;
113     baseDMA::operator=(rs);
114     strcpy(color, rs.color);
115     return *this;
116 }
117 
118 lacksDMA::~lacksDMA() {
119 
120 }
121 
122 std::ostream & operator<<(std::ostream & os, const lacksDMA & rs) {
123     os << (const baseDMA &)rs;
124     os << "color: " << rs.color << std::endl;
125     return os;
126 }
127 
128 void lacksDMA::view() {
129     using namespace std;
130     baseDMA::view();
131     cout << "color: " << color << endl;
132 }
133 
134 hasDMA::hasDMA(const char *l, int r, const char *s)
135     : baseDMA(l, r){
136     style = new char[strlen(s) + 1];
137     strcpy(style, s);
138 }
139 
140 hasDMA::hasDMA(const hasDMA &rs)
141     : baseDMA(rs){
142     style = new char[strlen(rs.style) + 1];
143     strcpy(style, rs.style);
144 }
145 
146 hasDMA::~hasDMA() {
147     delete [] style;
148 }
149 
150 hasDMA &hasDMA::operator=(const hasDMA &rs) {
151     if (this == &rs)
152         return *this;
153     baseDMA::operator=(rs);
154     delete [] style;
155     style = new char[strlen(rs.style) + 1];
156     strcpy(style, rs.style);
157     return *this;
158 }
159 
160 std::ostream & operator<<(std::ostream & os, const hasDMA & rs) {
161     os << (const baseDMA &)rs;
162     os << "style: " << rs.style << std::endl;
163     return os;
164 }
165 
166 void hasDMA::view() {
167     using namespace std;
168     baseDMA::view();
169     cout << "style: " << style << endl;
170 }
171 
172 // run
173 
174 void ch13_3() {
175     using namespace std;
176     DMA * DMAs[3];
177     DMAs[0] = new baseDMA("Portabelly", 8);
178     DMAs[1] = new lacksDMA("Blimpo", 4, "red");
179     DMAs[2] = new hasDMA("Buffalo Keys", 5, "Mercator");
180     cout << "Displaying baseDMA object:\n";
181     DMAs[0]->view();
182     cout << "Displaying lacksDMA object:\n";
183     DMAs[1]->view();
184     cout << "Displaying hasDMA object:\n";
185     DMAs[2]->view();
186 }
  1 // chapter13_4_port.h
  2 
  3 #ifndef LEARN_CPP_CHAPTER13_4_PORT_H
  4 #define LEARN_CPP_CHAPTER13_4_PORT_H
  5 #include <iostream>
  6 
  7 class Port {
  8 private:
  9     char * brand;
 10     char style[20];
 11     int bottles;
 12 public:
 13     Port(const char * br = "none", const char * st = "vintage", int b = 0);
 14     Port(const Port & p);
 15     virtual ~Port() {delete [] brand;}
 16     Port & operator=(const Port & p);
 17     Port & operator+=(int b);
 18     Port & operator-=(int b);
 19     int BottleCount() const;
 20     friend std::ostream & operator<<(std::ostream & os, const Port & p);
 21     virtual void show() const;
 22 };
 23 
 24 class VintagePort : public Port {
 25 private:
 26     char * nickname;
 27     int year;
 28 public:
 29     VintagePort(const char * br = "none", int b = 0, const char * nn = "none", int y = 0);
 30     VintagePort(const VintagePort & vp);
 31     virtual ~VintagePort() {delete [] nickname;}
 32     VintagePort & operator=(const VintagePort & vp);
 33     friend std::ostream & operator<<(std::ostream & os, const VintagePort & vp);
 34     virtual void show() const;
 35 
 36 };
 37 
 38 
 39 #endif //LEARN_CPP_CHAPTER13_4_PORT_H
 40 
 41 // chapter_13_4_port.cpp
 42 
 43 #include "chapter13_4_port.h"
 44 #include <cstring>
 45 #include <iostream>
 46 
 47 
 48 Port::Port(const char *br, const char *st, int b) {
 49     brand = new char[strlen(br) + 1];
 50     strcpy(brand, br);
 51     strcpy(style, st);
 52     bottles = b;
 53 }
 54 
 55 Port::Port(const Port &p) {
 56     brand = new char[strlen(p.brand) + 1];
 57     strcpy(brand, p.brand);
 58     strcpy(style, p.style);
 59     bottles = p.bottles;
 60 }
 61 
 62 Port &Port::operator=(const Port &p) {
 63     if (this == &p)
 64         return *this;
 65     delete [] brand;
 66     brand = new char[strlen(p.brand) + 1];
 67     strcpy(brand, p.brand);
 68     strcpy(style, p.style);
 69     bottles = p.bottles;
 70     return *this;
 71 }
 72 
 73 Port &Port::operator+=(int b) {
 74     bottles += b;
 75     return *this;
 76 }
 77 
 78 Port &Port::operator-=(int b) {
 79     bottles -= b;
 80     return *this;
 81 }
 82 
 83 int Port::BottleCount() const {
 84     return bottles;
 85 }
 86 
 87 void Port::show() const {
 88     using namespace std;
 89     cout << "Brand: " << brand << endl;
 90     cout << "Kind: " << style << endl;
 91     cout << "Bottles: " << bottles << endl;
 92 }
 93 
 94 std::ostream & operator<<(std::ostream & os, const Port & p) {
 95     os << p.brand << ", " << p.style << ", " << p.bottles << std::endl;
 96     return os;
 97 }
 98 
 99 VintagePort::VintagePort(const char *br, int b, const char *nn, int y)
100     : Port(br, "vintage", b) {
101     nickname = new char[strlen(nn) + 1];
102     strcpy(nickname, nn);
103     year = y;
104 }
105 
106 VintagePort::VintagePort(const VintagePort &vp)
107     : Port(vp) {
108     nickname = new char[strlen(vp.nickname) + 1];
109     strcpy(nickname, vp.nickname);
110     year = vp.year;
111 }
112 
113 VintagePort &VintagePort::operator=(const VintagePort &vp) {
114     if (this == &vp)
115         return *this;
116     Port::operator=(vp);
117     delete [] nickname;
118     nickname = new char[strlen(vp.nickname) + 1];
119     strcpy(nickname, vp.nickname);
120     year = vp.year;
121     return *this;
122 }
123 
124 void VintagePort::show() const {
125     using namespace std;
126     Port::show();
127     cout << "Nickname: " << nickname << endl;
128     cout << "Year: " << year << endl;
129 }
130 
131 std::ostream & operator<<(std::ostream & os, const VintagePort & vp) {
132     os << (const Port &) vp;
133     os << vp.nickname << ", " << vp.year << std::endl;
134 }
135 
136 // run
137 
138 void ch13_4() {
139     using namespace std;
140 
141     Port p1("maotai", "wine", 120);
142     VintagePort p2 = VintagePort("buzhiming", 200, "The Noble", 1990);
143     Port * pp = &p1;
144 
145     cout << "Using object directly:\n";
146     p1.show();
147     p1 += 100; p1.show(); p1 -= 50; p1.show();
148     p2.show();
149 
150     cout << "Using type cd * pointer to objects:\n";
151     pp -> show();
152     pp = &p2;
153     pp -> show();
154 
155     cout << "Testing assignment: \n";
156     VintagePort copy;
157     copy = p2;
158     cout << copy << endl;
159 }

 aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa aaa欢迎大家一起讨论ccccccccccccccccccccccccccccccccaaaaaaaaa

posted @ 2021-10-04 20:57  开心果壳好硬  阅读(155)  评论(0编辑  收藏  举报