zoukankan      html  css  js  c++  java
  • 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
    ";
        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] = '';
    113 }
    114 
    115 lacksDMA::lacksDMA(const char *pt, const baseABC & st): baseABC(st){
    116     strncpy(color, pt, COL_LEN);
    117     color[COL_LEN - 1] = '';
    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
    ";
    177         cin.get(temp, LEN).get();
    178         int r;
    179         cout << "enter the rating
    ";
    180         cin >> r;
    181         while (cin.get() != '
    ')
    182             continue;
    183         char kind;
    184         cout << "enter 1/2/3 to choice a model to fit baseDMA/lacksDMA/hasDMA
    ";
    185         cin >> kind;
    186         while (cin.get() != '
    ')
    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
    ";
    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
    ";
    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 }
  • 相关阅读:
    桌面工具集
    运维工具集
    使用Maven插件构建Spring Boot应用程序Docker镜像
    解决Ubuntu 17.10设置面板打不开的问题
    防止Web表单重复提交的方法总结
    深入浅出mybatis之启动详解
    yum方式安装mysql
    在Java中调用Python
    UUID在Java中的实现与应用
    VM克隆后找不到eth0的问题解决
  • 原文地址:https://www.cnblogs.com/coding-time/p/4530933.html
Copyright © 2011-2022 走看看