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

Posted 开心果壳好硬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 // chatper13_3_dma.h 2 3 4 // 明天写QAQ 

 1 // chapter13_4_port.h 2 3 4 // 明天写QAQ 

以上是关于C++ Primer Plus编程练习答案——第13章的主要内容,如果未能解决你的问题,请参考以下文章

C++ Primer Plus编程练习答案——第14章

C++ Primer Plus编程练习答案——第9章

C++ Primer Plus编程练习答案——第8章

C++ Primer Plus 编程练习——第2章

C++ Primer Plus 编程练习——第6章

C Primer Plus(第六版)第十章 编程练习答案