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

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