第13章 类继承
13.11 编程练习
第二题
1头文件源码:"classic.h"
#ifndef CLASSIC_H_INCLUDED
#define CLASSIC_H_INCLUDED
class Cd
{
private:
char *performers;
char *label;
int selections;
double playtime;
public:
Cd(char * s1, char * s2, int n, double x);
Cd();
Cd(const Cd & C);
virtual ~Cd();
virtual void Report() const;
Cd & operator=(const Cd &d);
};
class Classic : public Cd
{
private:
char *details;
public:
Classic(char * s0, char * s1, char * s2, int n, double x);
Classic() {}
Classic(const Classic & C);
Classic(char * s0, const Cd & d);
~Classic();
virtual void Report() const;
Classic & operator=(const Classic & C);
};
#endif // CLASSIC_H_INCLUDED 2 头文件实现:
1 #include <iostream>
2 #include <cstring>
3 #include "classic.h"
4
5 using std::cout;
6 using std::endl;
7
8 Cd::Cd(char * s1, char * s2, int n, double x)
9 {
10 int len1 = std::strlen(s1);
11 int len2= std::strlen(s2);
12 performers = new char[len1+1];
13 label = new char[len2+1];
14 std::strcpy(performers, s1);
15 std::strcpy(label, s2);
16 selections = n;
17 playtime = x;
18 }
19
20 Cd::Cd()
21 {
22 performers = 0;
23 label = 0;
24 selections = 0;
25 playtime = 0.0;
26 }
27
28 Cd::Cd(const Cd & C)
29 {
30 int len1 = std::strlen(C.performers);
31 int len2= std::strlen(C.label);
32 performers = new char[len1+1];
33 label = new char[len2+1];
34 std::strcpy(performers, C.performers);
35 std::strcpy(label, C.label);
36 selections = C.selections;
37 playtime = C.playtime;
38 }
39 Cd::~Cd()
40 {
41 delete [] performers;
42 delete [] label;
43 }
44
45 void Cd::Report() const
46 {
47 cout << "performers: " << performers << endl;
48 cout << "label: " << label << endl;
49 cout << "selection: " << selections << endl;
50 cout << "playtime: " << playtime << endl;
51
52 }
53
54 Cd & Cd::operator=(const Cd &C)
55 {
56 if(this == &C)
57 return *this;
58 int len1 = std::strlen(C.performers);
59 int len2= std::strlen(C.label);
60 performers = new char[len1+1];
61 label = new char[len2+1];
62 std::strcpy(performers, C.performers);
63 std::strcpy(label, C.label);
64 selections = C.selections;
65 playtime = C.playtime;
66 return *this;
67 }
68
69
70 Classic::Classic(char * s0, char * s1, char * s2, int n, double x):Cd(s1, s2, n, x)
71 {
72 int len = std::strlen(s0);
73 details = new char[len+1];
74 std::strcpy(details, s0);
75 }
76
77
78
79 Classic::Classic(char * s0, const Cd &C):Cd(C)
80 {
81 details = new char[std::strlen(s0)+1];
82 std::strcpy(details, s0);
83 }
84
85 Classic::Classic(const Classic & C):Cd(C)
86 {
87 int len = std::strlen(C.details);
88 details = new char[len+1];
89 std::strcpy(details, C.details);
90 }
91
92 Classic::~Classic()
93 {
94 delete [] details;
95 }
96
97 void Classic::Report() const
98 {
99 Cd::Report();
100 cout << "details: " << details << endl;
101 }
102
103 Classic & Classic::operator=(const Classic & C)
104 {
105 if(this == &C)
106 return *this;
107 Cd::operator=(C);
108 delete [] details;
109 int len = std::strlen(C.details);
110 details = new char[len+1];
111 std::strcpy(details, C.details);
112 return *this;
113 }