p475.2
//头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; public: Cd(char *s1, char *s2, int n, double x); Cd(const Cd & st); Cd(); virtual ~Cd(); virtual void Report()const; Cd & operator = (const Cd & st); }; class Classic : public Cd { private: char *production; public: Classic(char *s1 = "nullbody1", char *s2 = "nullbody2", char *s3 = "nullbody3", int n = 0, double x = 0); Classic(const Classic & st); virtual void Report()const; virtual ~Classic(); Classic & operator=(const Classic & st); }; //方法: #include<iostream> #include<cstring> #include"classic.h" using std::cout; using std::endl; Cd::Cd(char *s1, char *s2, int n, double x){ performers = new char[strlen(s1) + 1]; strcpy(performers, s1); label = new char[strlen(s2) + 1]; strcpy(label, s2); selections = n; playtime = x; } Cd::Cd(const Cd & st){ performers = new char[strlen(st.performers) + 1]; strcpy(performers, st.performers); label = new char[strlen(st.performers) + 1]; strcpy(label, st.label); selections = st.selections; playtime = st.playtime; } Cd::Cd(){ performers = NULL; label = NULL; selections = 0; playtime = 0; } void Cd::Report()const{ cout << "performers: " << performers << endl << "label: " << label << endl << "selections: " << selections << endl << "playtime: " << playtime << endl; } Cd & Cd::operator = (const Cd & st){ if (this == &st) return *this; performers = new char[strlen(st.performers) + 1]; strcpy(performers, st.performers); label = new char[strlen(st.performers) + 1]; strcpy(label, st.label); selections = st.selections; playtime = st.playtime; return *this; } Classic::Classic(char *s1, char *s2, char *s3, int n, double x): Cd(s2, s3, n, x){ production = new char[strlen(s1) + 1]; strcpy(production, s1); } Classic::Classic(const Classic & st): Cd(st){ production = new char[strlen(st.production) + 1]; strcpy(production, st.production); } Cd::~Cd(){ delete[]performers; delete[]label; } Classic::~Classic(){ delete[]production; } void Classic::Report()const{ cout << "production: " << production << endl; Cd::Report(); } Classic & Classic::operator=(const Classic & st){ if (this == &st) return *this; Cd::operator=(st); production = new char[strlen(st.production) + 1]; strcpy(production, st.production); return *this; } //驱动: #include<iostream> #include<cstdlib> using namespace std; #include"classic.h" void Bravo(const Cd & disk); int main(){ Cd c1("Beatles", "Capitol", 14, 35.5); Classic c2 = Classic("piano sonata in B flat", "Alfred Brendel", "Philips", 2, 57.17); Cd *pcd = &c1; cout << "using object directly "; c1.Report(); c2.Report(); cout << "using type cd *pointer to objects: "; pcd->Report(); pcd = &c2; pcd->Report(); cout << "calling a function with a Cd reference argument: "; Bravo(c1); Bravo(c2); cout << "testing assignment: "; Classic copy; copy = c2; copy.Report(); system("pause"); return 0; } void Bravo(const Cd & disk){ disk.Report(); }
p475.3
1 //头文件: 2 #include<iostream> 3 4 #ifndef BASE_H_ 5 #define BASE_H_ 6 class baseABC 7 { 8 private: 9 char *label; 10 int rating; 11 public: 12 baseABC(const char *s1="null", int r = 0); 13 baseABC(const baseABC &st); 14 virtual ~baseABC() = 0; 15 baseABC & operator=(const baseABC &rs); 16 friend std::ostream & operator<<(std::ostream & os, const baseABC & rs); 17 }; 18 19 class baseDMA : public baseABC 20 { 21 public: 22 baseDMA(const char *s1="null", int r = 0); 23 baseDMA(const baseDMA &st); 24 baseDMA & operator=(const baseDMA &rs); 25 friend std::ostream & operator<<(std::ostream & os, const baseDMA & rs); 26 }; 27 28 class lacksDMA : public baseABC 29 { 30 private: 31 enum{COL_LEN=40}; 32 char color[COL_LEN]; 33 public: 34 lacksDMA(const char *pt = "blank", const char *ct = "null", int r = 0); 35 lacksDMA(const char *pt, const baseABC & st); 36 friend std::ostream & operator<<(std::ostream & os, const lacksDMA & st); 37 }; 38 39 class hasDMA : public baseABC 40 { 41 private: 42 char *style; 43 public: 44 hasDMA(const char *pt = "none", const char *ct = "null", int r = 0); 45 hasDMA(const char *ct, const baseABC &st); 46 hasDMA(const hasDMA &st); 47 virtual ~hasDMA(); 48 hasDMA & operator=(const hasDMA & st); 49 friend std::ostream & operator <<(std::ostream &os, const hasDMA &st); 50 }; 51 52 #endif 53 54 //方法: 55 #include<iostream> 56 #include<cstring> 57 #include"classic.h" 58 59 using std::endl; 60 61 baseABC::baseABC(const char *s1, int r){ 62 label = new char[strlen(s1) + 1]; 63 strcpy(label, s1); 64 rating = r; 65 } 66 67 baseABC::baseABC(const baseABC &st){ 68 label = new char[strlen(st.label) + 1]; 69 strcpy(label, st.label); 70 rating = st.rating; 71 } 72 73 baseABC::~baseABC(){ 74 delete[]label; 75 } 76 77 baseABC & baseABC::operator=(const baseABC &rs){ 78 if (this == &rs) 79 return *this; 80 label = new char[strlen(rs.label) + 1]; 81 strcpy(label, rs.label); 82 rating = rs.rating; 83 return *this; 84 } 85 86 std::ostream & operator<<(std::ostream & os, const baseABC & rs){ 87 os << "label: " << rs.label << endl 88 << "rating: " << rs.rating << endl; 89 return os; 90 } 91 92 baseDMA::baseDMA(const char *s1, int r): baseABC(s1, r){ 93 } 94 95 baseDMA::baseDMA(const baseDMA &st): baseABC(st){ 96 } 97 98 baseDMA & baseDMA::operator=(const baseDMA &rs){ 99 if (this == &rs) 100 return *this; 101 baseABC::operator=(rs); 102 return *this; 103 } 104 105 std::ostream & operator<<(std::ostream & os, const baseDMA & rs){ 106 os << (const baseABC &)rs; 107 return os; 108 } 109 110 lacksDMA::lacksDMA(const char *pt, const char *ct, int r): baseABC(ct, r){ 111 strncpy(color, pt, COL_LEN); 112 color[COL_LEN - 1] = '