编程练习答案——第13章

【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:
";
130     c1.Report();
131     c2.Report();
132 
133     cout << "Using type cd * pointer to objects:
";
134     pcd -> Report();
135     pcd = &c2;
136     pcd -> Report();
137 
138     cout << "Calling a function with a Cd reference argument:
";
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 }
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 编程练习答案——第13章