c++ primer plus 习题答案(8)

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\n";
    c1.Report();
    c2.Report();

    cout << "using type cd *pointer to objects:\n";
    pcd->Report();
    pcd = &c2;
    pcd->Report();

    cout << "calling a function with a Cd reference argument:\n";
    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] = ‘\0‘;
113 }
114
115 lacksDMA::lacksDMA(const char *pt, const baseABC & st): baseABC(st){
116     strncpy(color, pt, COL_LEN);
117     color[COL_LEN - 1] = ‘\0‘;
118 }
119
120 std::ostream & operator<<(std::ostream & os, const lacksDMA & st){
121     os << (const baseABC &)st<<endl
122     <<"color: "<<st.color;
123     return os;
124 }
125
126 hasDMA::hasDMA(const char *pt, const char *ct, int r): baseABC(ct, r){
127     style = new char[strlen(pt) + 1];
128     strcpy(style, pt);
129 }
130
131 hasDMA::hasDMA(const char *ct, const baseABC &st): baseABC(st){
132     style = new char[strlen(ct) + 1];
133     strcpy(style, ct);
134 }
135
136 hasDMA::hasDMA(const hasDMA &st): baseABC(st){
137     style = new char[strlen(st.style) + 1];
138     strcpy(style, st.style);
139 }
140
141 hasDMA::~hasDMA(){
142     delete[]style;
143 }
144
145 hasDMA & hasDMA::operator=(const hasDMA & st){
146     if (this == &st)
147         return *this;
148     delete[]style;
149     baseABC::operator=(st);
150     style = new char[strlen(st.style) + 1];
151     strcpy(style, st.style);
152     return *this;
153 }
154
155 std::ostream & operator <<(std::ostream &os, const hasDMA &st){
156     os << (const baseABC &)st<<endl
157     <<"style: "<< st.style << endl;
158     return os;
159 }
160
161 //驱动:
162 #include<iostream>
163 #include<cstdlib>
164 #include"classic.h"
165
166 const int CLIENTS = 2;
167 const int LEN = 40;
168
169 int main(){
170     using namespace std;
171     int i;
172     baseABC *pt[CLIENTS];
173
174     for (i = 0; i < CLIENTS; i++){
175         char temp[LEN];
176         cout << "enter the label\n";
177         cin.get(temp, LEN).get();
178         int r;
179         cout << "enter the rating\n";
180         cin >> r;
181         while (cin.get() != ‘\n‘)
182             continue;
183         char kind;
184         cout << "enter 1/2/3 to choice a model to fit baseDMA/lacksDMA/hasDMA\n";
185         cin >> kind;
186         while (cin.get() != ‘\n‘)
187             continue;
188         if (kind == ‘1‘)
189             pt[i] = new baseDMA(temp, r);
190         else if (kind == ‘2‘){
191             char ar[40];
192             cout << "enter a color\n";
193             cin.get(ar, 40).get();
194             baseDMA test1(temp, r);
195             pt[i] = new lacksDMA(ar, test1);
196         }
197         else if (kind == ‘3‘){
198             char ptr[40];
199             cout << "enter a style\n";
200             cin.get(ptr, 40).get();
201             pt[i] = new hasDMA(ptr, temp, r);
202         }
203     }
204     for (i = 0; i < CLIENTS; i++)
205         cout << *pt[i] << endl;
206
207
208     system("pause");
209     return 0;
210 }
时间: 2024-10-10 14:14:27

c++ primer plus 习题答案(8)的相关文章

c++ primer plus 习题答案(1)

第五版c++ primer plus,IDE是visual studio2013. p180.2 1 #include<iostream> 2 #include<ctime> 3 #include<cstdlib> 4 5 int main(void){ 6 using namespace std; 7 int i, j, count=0, sum = 0, pt[10]; 8 double mean; 9 for (i = 0; i < 10; i++){ 10

c primer plus 习题答案(4)

p319.3 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<ctype.h> 4 #include<string.h> 5 void deliver(char *a1, char *a2, int n); 6 7 int main(void) 8 { 9 int n; 10 char str1[81], *ptr, ar[81]; 11 ptr=gets(str1); 12 n=strlen(ptr)

c++ primer plus 习题答案(2)

p259.4 1 #include<iostream> 2 #include<cstdlib> 3 #include<cctype> 4 using namespace std; 5 struct stringy{ 6 char* str; 7 int ct; 8 }; 9 void set(stringy &, const char*); 10 void show(const stringy &, int times = 1); 11 void sho

c primer plus 习题答案(3)

p281.2 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define SIZE 5 4 void copy_arr(double ar[], double pr[],int n); 5 void copy_ptr(double ar[], double pr[], int n); 6 7 int main(void) 8 { 9 double source[5]={1.1, 2.2, 3.3, 4.4, 5.5}; 10 double

c primer plus 习题答案(6)

p376.7 A方案 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define LEN 40 4 void rank(FILE *, FILE *); 5 int main(void) 6 { 7 FILE *fp, *fc; 8 int ch, bp; 9 char file1[LEN], file2[LEN]; 10 11 puts("enter file1 name"); 12 if((fp=fopen(gets

c primer plus 习题答案(5)

p352.6 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main(void) 6 { 7 int i, j, count[10]={0}, seed[10]; 8 9 for(i=0; i<10; i++){ 10 printf("please enter a seed.\n"); 11 scanf("%d", &see

c primer plus 习题答案(7)

p421.5 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define CSIZE 4 4 #define SCORE 3 5 6 void get_info(struct students *p); 7 void get_average(struct students *p); 8 void print_info(struct students *p); 9 void print_class_average(struct student

《C++primer》v5 第5章 语句 读书笔记 习题答案

5.1 空语句只有一个";".如果什么也不想做可以使用空语句. 5.2 用花括号{}括起来的叫块,也叫复合语句.有多条语句作用在同一个作用域时,需要用花括号括起来. 5.3 降低了. 5.4 (a)每次迭代时候会初始化iter,但是iter缺少初值,所以这段代码根本不会通过编译.另外这里的括号需要一个bool类型的,而定义迭代器根本不会返回一个bool类型.假如上面那些问题都可以通过,每次迭代都会初始化这个iter,会导致死循环. (b)我试了一下编译未通过是因为没找到适合的find函

《C++primer》v5 第3章 字符串、向量和数组 读书笔记 习题答案

3.1略 3.2 string str; //读行 while(getline(cin,str)) cout<<str<<endl; //读单个词 while(cin>>str) cout<<str<<endl; 3.3 输入运算符读到空白符结束 getline读到换行符结束,并丢弃换行符 3.4 比较大小. 比较大小是比较的第一个不相同的字符的大小. int main() { string a,b; cin>>a>>b;