C++ primer plus 第六版第13章编程练习答案(1-3题)

1.
//head
#ifndef C__13_1_hpp
#define C__13_1_hpp

class Cd
{
private:
    char performers[50];
    char label[20];
    int selections;
    double playtime;
public:
    Cd(const char * s1="blank",const char * s2="blank",int n=0,double x=0.0);
    Cd(const Cd & d);
    virtual~Cd(){};
    virtual void Report()const;
    Cd &operator=(const Cd & d);
    
};
class Classic:public Cd
{
private:
    char work[100];
public:
    Classic(const char * c="blank",const char *m="blank",const char *s2="blank",int n=0,double x=0.0);//指针不能初始化为nullptr
    virtual void Report()const;
    Classic & operator=(const Classic & c);
};
#endif /* C__13_1_hpp */
//定义
#include "C++13_1.hpp"
#include <iostream>
#include <cstring>
Cd::Cd(const char * s1,const char * s2,int n,double x)
{
    strcpy(performers,s1);
    strcpy(label,s2);
    selections=n;
    playtime=x;
}
void Cd:: Report()const
{
    using std::cout;
    using std::endl;
    cout<<"Performers: "<<performers<<endl;
    cout<<"Label: "<<label<<endl;
    cout<<"Selection: "<<selections<<endl;
    cout<<"Playtime: "<<playtime<<endl;
}
Cd & Cd::operator=(const Cd & d)
{
    strcpy(performers,d.performers);
    strcpy(label,d.label);
    selections=d.selections;
    playtime=d.playtime;
    return *this;
}
void Classic:: Report()const
{
    std::cout<<"Primary work: "<<work<<std::endl;
    Cd::Report();
}
Classic::Classic(const char * c,const char *s1,const char *s2,int n,double x):Cd(s1,s2,n,x)
{
    strcpy(work,c);
}
Classic & Classic::operator=(const Classic & c)
{
    strcpy(work,c.work);
    Cd::operator=(c);
    return *this;
}
//主函数
#include <iostream>
#include "C++13_1.hpp"
using namespace std;
void Bravo(const Cd & disk);
int main() {
    Cd c1("Beatles","Captiol",14,35.5);
          Classic c2=Classic("Piano Sonata in B flat, Fantasia in C",
                         "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();
    return 0;
}
void Bravo(const Cd& disk)
{
    disk.Report();
}

2.
//head
#ifndef C__13_2_hpp
#define C__13_2_hpp

class Cd
{
private:
    char *performers;
    char *label;
    int selections;
    double playtime;
public:
    Cd(const char * s1="blank",const char * s2="blank",int n=0,double x=0.0);
    Cd(const Cd & d);
    virtual~Cd();
    virtual void Report()const;
    Cd &operator=(const Cd & d);
    
};
class Classic:public Cd
{
private:
    char *work;
public:
    Classic(const char * c="blank",const char *m="blank",const char *s2="blank",int n=0,double x=0.0);//指针不能初始化为nullptr
    ~Classic();
    virtual void Report()const;
    Classic & operator=(const Classic & c);
};

#endif /* C__13_2_hpp */
//定义
#include "C++13_2.hpp"
#include <iostream>
#include <cstring>
Cd::Cd(const char * s1,const 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()
{
    delete []performers;
    delete []label;
}
void Cd:: Report()const
{
    using std::cout;
    using std::endl;
    cout<<"Performers: "<<performers<<endl;
    cout<<"Label: "<<label<<endl;
    cout<<"Selection: "<<selections<<endl;
    cout<<"Playtime: "<<playtime<<endl;
}
Cd & Cd::operator=(const Cd & d)
{
    if(this==&d)
        return *this;
    delete []performers;
    delete []label;
    performers=new char[strlen(d.performers)+1];
    strcpy(performers,d.performers);
    label=new char[strlen(d.label)+1];
    strcpy(label,d.label);
    selections=d.selections;
    playtime=d.playtime;
    return *this;
}
void Classic:: Report()const
{
    std::cout<<"Primary work: "<<work<<std::endl;
    Cd::Report();
}
Classic::Classic(const char * c,const char *s1,const char *s2,int n,double x):Cd(s1,s2,n,x)
{
    work=new char[strlen(c)+1];
    strcpy(work,c);
}
Classic::~Classic()
{
    delete []work;
}
Classic & Classic::operator=(const Classic & c)
{
    if(this==&c)
        return *this;
    delete[]work;
    work=new char[strlen(c.work)+1];
    strcpy(work,c.work);
    Cd::operator=(c);
    return *this;
}

//主函数
#include <iostream>
#include "C++13_2.hpp"
using namespace std;
void Bravo(const Cd & disk);
int main() {
    Cd c1("Beatles","Captiol",14,35.5);
          Classic c2=Classic("Piano Sonata in B flat, Fantasia in C",
                         "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();
    return 0;
}
void Bravo(const Cd& disk)
{
    disk.Report();
}

3.
//head
#ifndef C__13_3_hpp
#define C__13_3_hpp

#include <iostream>
class Base
{
private:
    char * label;
    int rating;
public:
    Base(const char *c="blank",int n=0);
    virtual void show()const=0;
    virtual~Base();
};
class baseDMA:public Base
{
private:
    char *from;
public:
    baseDMA(const char* c,const char * a,int n);
    virtual~baseDMA();
    virtual void show()const;
    
};
class lacksDMA:public Base
{
private:
    enum{COL_LEN=40};
    char color[COL_LEN];
public:
    lacksDMA(const char *c="blank",const char*l="NULL",int r=0);
    virtual~lacksDMA(){};
    virtual void show()const;
    
};
class hasDMA:public Base
{
private:
    char *style;
public:
    hasDMA(const char * s="none",const char * l="null",int r=0);
    virtual~hasDMA();
    virtual void show()const;
    
};

#endif /* C__13_3_hpp */
//定义
#include "C++13_3.hpp"
using namespace std;
Base::Base(const char*c,int n)
{
    label=new char[strlen(c)+1];
    strcpy(label,c);
    rating=n;
}
Base::~Base()
{
    delete []label;
}
lacksDMA::lacksDMA(const char *c,const char*l,int r):Base(l,r)
{
    strncpy(color,c,COL_LEN-1);
    color[COL_LEN-1]='\0';
}

void Base::show()const
{
    cout<<"Label: "<<label<<endl;
    cout<<"Rating: "<<rating<<endl;
}
baseDMA::baseDMA(const char* c,const char * a,int n):Base(a,n)
{
    from=new char[strlen(c)+1];
    strcpy(from,c);
}
hasDMA::hasDMA(const char * s,const char * l,int r):Base(l,r)
{
    style=new char[strlen(s)+1];
    strcpy(style,s);
}
hasDMA::~hasDMA()
{
    delete[]style;
}
baseDMA::~baseDMA()
{
    delete []from;
}
void baseDMA::show()const
{
    Base::show();
    cout<<"From "<<from<<endl;
}
void lacksDMA::show()const
{
    Base::show();
    cout<<"Color: "<<color<<endl;
}
void hasDMA::show()const
{
    Base::show();
    cout<<"Style: "<<style<<endl;
}
//主函数
#include <iostream>
#include "C++13_3.hpp"
int main(void) {
    using std::cin;
    using std::cout;
    using std::endl;
    Base *ptr[5];
    char c[30];
    for(int i=0;i<5;++i)
    {
    cout<<"Enter label:";
    cin.getline(c,30);
    cout<<"Enter rating: ";
    int t;
    (cin>>t).get();
    cout<<"Enter 1 for bassDMA,2 for lacksDMA,3 for hasDMA: ";
    int kind;
    while(cin>>kind&&(kind!=1&&kind!=2&&kind!=3))
        cout<<"Enter 1,2 or 3: ";
    if(kind==1)
    {
        cout<<"From where: ";
        char f[30];
        cin.get();
        cin.getline(f,30);
           ptr[i]=new baseDMA(f,c,t);
    }
     
        if(kind==2)
        {
            cout<<"Enter color: ";
            char r[30];
            cin.get();
            cin.getline(r,30);
               ptr[i]=new lacksDMA(r,c,t);
        }
        
        if(kind==3)
        {
            cout<<"Enter style: ";
            char s[30];
            cin.get();
            cin.getline(s,30);
               ptr[i]=new hasDMA(s,c,t);
        }
    }
    for(int i=0;i<5;++i)
        ptr[i]->show();
    return 0;
}
//完
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值