C++PrimerPlus中文第六版第13章编程练习答案

1、

// cd.h

#ifndef CD_H_
#define CD_H_

class CD
{
private:
    char performers[50];
    char label[20];
    int selections;
    double playtime;
public:
    CD(const char* p, const char* l, int s, double pt);
    CD(const CD& cd);
    CD();
    virtual ~CD() {};
    virtual void Report() const;
    CD& operator=(const CD& cd);
};

class Classic : public CD
{
private:
    char mainWorks[50];
public:
    Classic(const char* mw, const char* p, const char* l, int s, double pt);
    Classic(const Classic& cl);
    //Classic(const char* mw, const CD& cd);
    Classic();
    virtual ~Classic() {};
    virtual void Report() const;
    Classic& operator=(const Classic& cl);
};

#endif
// cd.cpp


#include<iostream>
#include<string>
#include"cd.h"

CD::CD(const char* p, const char* l, int s, double pt)
{
    strcpy_s(performers, strlen(p) + 1, p);
    strcpy_s(label, strlen(l) + 1, l);
    selections = s;
    playtime = pt;
}

CD::CD()
{
    performers[0] = '\0';
    label[0] = '\0';
    selections = 0;
    playtime = 0.0;
}

CD::CD(const CD& cd)
{
    strcpy_s(performers, strlen(cd.performers) + 1, cd.performers);
    strcpy_s(label, strlen(cd.label) + 1, cd.label);
    selections = cd.selections;
    playtime = cd.playtime;
}

void CD::Report() const
{
    using std::cout;
    using std::endl;
    cout << "Performers: " << performers << endl;
    cout << "Label: " << label << endl;
    cout << "Selections: " << selections << endl;
    cout << "Playtime: " << playtime << endl << endl;
}


CD& CD::operator=(const CD& cd)
{
    if (this == &cd)
        return *this;
    strcpy_s(performers, strlen(cd.performers) + 1, cd.performers);
    strcpy_s(label, strlen(cd.label) + 1, cd.label);
    selections = cd.selections;
    playtime = cd.playtime;
    return *this;
}


Classic::Classic(const char* mw, const char* p, const char* l, int s, double pt) : CD(p, l, s, pt)
{
    strcpy_s(mainWorks, strlen(mw) + 1, mw);
}

Classic::Classic(const Classic& cl) : CD(cl)
{
    strcpy_s(mainWorks, strlen(cl.mainWorks) + 1, cl.mainWorks);
}

Classic::Classic() : CD()
{
    mainWorks[0] = '\0';
}

void Classic::Report() const
{
    using std::cout;
    using std::endl;
    cout << "Main works: " << mainWorks << endl;
    CD::Report();
}

Classic& Classic::operator=(const Classic& cl)
{
    if (this == &cl)
        return *this;
    CD::operator=(cl);
    strcpy_s(mainWorks, strlen(cl.mainWorks) + 1, cl.mainWorks);
    return *this;
}
#include<iostream>
using namespace std;
#include"cd.h"

void Bravo(const CD& cd);

int main()
{
    CD cd("Beatles", "Capitol", 14, 35.5);
    Classic cl = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
    // 上面首先调用含有5个参数的构造函数创建一个匿名临时对象,其次调用复制构造函数用该匿名对象初始化Classic类对象cl
    
    cd.Report();
    cl.Report();

    Bravo(cd);
    Bravo(cl);

    CD* pcd = &cd;
    pcd->Report();
    pcd = &cl;
    pcd->Report();

    Classic copy;
    copy = cl;
    copy.Report();

    return 0;
}


void Bravo(const CD& cd)
{
    cd.Report();
}

2、

// cd.h

#ifndef CD_H_
#define CD_H_

class CD
{
private:
    char* performers;
    char* label;
    int selections;
    double playtime;
public:
    CD(const char* p, const char* l, int s, double pt);
    CD(const CD& cd);
    CD();
    virtual ~CD() { delete[] performers; delete[] label; };
    virtual void Report() const;
    CD& operator=(const CD& cd);
};

class Classic : public CD
{
private:
    char* mainWorks;
public:
    Classic(const char* mw, const char* p, const char* l, int s, double pt);
    Classic(const Classic& cl);
    //Classic(const char* mw, const CD& cd);
    Classic();
    virtual ~Classic() { delete[] mainWorks; };
    virtual void Report() const;
    Classic& operator=(const Classic& cl);
};

#endif
// cd.cpp

#include<iostream>
#include<string>
#include"cd.h"


CD::CD(const char* p, const char* l, int s, double pt)
{
    performers = new char[strlen(p) + 1];
    strcpy_s(performers, strlen(p) + 1, p);
    label = new char[strlen(l) + 1];
    strcpy_s(label, strlen(l) + 1, l);
    selections = s;
    playtime = pt;
}

CD::CD()
{
    performers = label = nullptr;
    selections = 0;
    playtime = 0.0;
}

CD::CD(const CD& cd)
{
    performers = new char[strlen(cd.performers) + 1];
    strcpy_s(performers, strlen(cd.performers) + 1, cd.performers);
    label = new char[strlen(cd.label) + 1];
    strcpy_s(label, strlen(cd.label) + 1, cd.label);
    selections = cd.selections;
    playtime = cd.playtime;
}

void CD::Report() const
{
    using std::cout;
    using std::endl;
    cout << "Performers: " << performers << endl;
    cout << "Label: " << label << endl;
    cout << "Selections: " << selections << endl;
    cout << "Playtime: " << playtime << endl << endl;
}


CD& CD::operator=(const CD& cd)
{
    if (this == &cd)
        return *this;
    delete[] performers;
    delete[] label;
    performers = new char[strlen(cd.performers) + 1];
    strcpy_s(performers, strlen(cd.performers) + 1, cd.performers);
    label = new char[strlen(cd.label) + 1];
    strcpy_s(label, strlen(cd.label) + 1, cd.label);
    selections = cd.selections;
    playtime = cd.playtime;
    return *this;
}



Classic::Classic(const char* mw, const char* p, const char* l, int s, double pt) : CD(p, l, s, pt)
{
    mainWorks = new char[strlen(mw) + 1];
    strcpy_s(mainWorks, strlen(mw) + 1, mw);
}

Classic::Classic(const Classic& cl) : CD(cl)
{
    if (cl.mainWorks != nullptr)
    {
        mainWorks = new char[strlen(cl.mainWorks) + 1];
        strcpy_s(mainWorks, strlen(cl.mainWorks) + 1, cl.mainWorks);
    }
    else
        mainWorks = nullptr;        
}

Classic::Classic() : CD()
{
    mainWorks = nullptr;
}


void Classic::Report() const
{
    using std::cout;
    using std::endl;
    cout << "Main works: " << mainWorks << endl;
    CD::Report();
}


Classic& Classic::operator=(const Classic& cl)
{
    if (this == &cl)
        return *this;
    CD::operator=(cl);
    delete[] mainWorks;
    mainWorks = new char[strlen(cl.mainWorks) + 1];
    strcpy_s(mainWorks, strlen(cl.mainWorks) + 1, cl.mainWorks);
    return *this;
}

main函数和第一题一样

3、略,无非三个子类,和正文两个子类没差别

4、

// port.h

#include<iostream>
using namespace std;

class Port
{
private:
    char* brand;
    char style[20];
    int bottles;
public:
    Port(const char* br = "none", const char* st = "none", int b = 0);
    Port(const Port& p);
    virtual ~Port() { delete[] brand; }
    Port& operator=(const Port& p);
    Port& operator+=(int b);
    Port& operator-=(int b);

    int BottleCount() const { return bottles; }
    virtual void Show() const;
    friend ostream& operator<<(ostream& os, const Port& p);
};


class VintagePort : public Port
{
private:
    char* nickname;
    int year;
public:
    VintagePort();
    VintagePort(const char* br, int b, const char* nn, int y);
    VintagePort(const VintagePort& vp);
    ~VintagePort() { delete[] nickname; }
    void Show() const;
    VintagePort& operator=(const VintagePort& vp);
    friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
// port.cpp


#include<iostream>
#include<string>
using namespace std;
#include"port.h"

Port::Port(const char* br, const char* st, int b)
{
    brand = new char[strlen(br) + 1];
    strcpy_s(brand, strlen(br) + 1, br);
    strcpy_s(style, strlen(st) + 1, st);
    bottles = b;
}

Port::Port(const Port& p)
{
    brand = new char[strlen(p.brand) + 1];
    strcpy_s(brand, strlen(p.brand) + 1, p.brand);
    strcpy_s(style, strlen(p.style) + 1, p.style);
    bottles = p.bottles;
}

Port& Port::operator=(const Port& p)
{
    if (this == &p)
        return *this;
    delete[] brand;
    brand = new char[strlen(p.brand) + 1];
    strcpy_s(brand, strlen(p.brand) + 1, p.brand);
    strcpy_s(style, strlen(p.style) + 1, p.style);
    bottles = p.bottles;
    return *this;
}


Port& Port::operator+=(int b)
{
    bottles += b;
}

Port& Port::operator-=(int b)
{
    bottles -= b;
}


void Port::Show() const
{
    cout << "Brand: " << brand << endl;
    cout << "Kind: " << style << endl;
    cout << "Bottles: " << bottles << endl;
}

ostream& operator<<(ostream& os, const Port& p)
{
    cout << p.brand << ", " << p.style << ", " << p.bottles << endl;
}




VintagePort::VintagePort() : Port()
{
    nickname = nullptr;
    year = 0;
}

VintagePort::VintagePort(const char* br, int b, const char* nn, int y) : Port(br, b)
{
    nickname = new char[strlen(nn) + 1];
    strcpy_s(nickname, strlen(nn) + 1, nn);
    year = y;
}

VintagePort::VintagePort(const VintagePort& vp) : Port(vp)
{
    nickname = new char[strlen(vp.nickname) + 1];
    strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
    year = vp.year;
}


VintagePort& VintagePort::operator=(const VintagePort& vp)
{
    if (this == &vp)
        return *this;
    Port::operator=(vp);
    delete[] nickname;
    nickname = new char[strlen(vp.nickname) + 1];
    strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
    year = vp.year;
    return *this;
}

void VintagePort::Show() const
{
    Port::Show();
    cout << "Nickname: " << nickname << endl;
    cout << "Year: " << year << endl;
}

ostream& operator<<(ostream& os, const VintagePort& vp)
{
    os << (const Port&)vp;
    os << ", " << vp.nickname << ", " << vp.year;
    return os;
}

尚未添加main函数进行测试

 

posted @ 2023-02-16 15:58  Triphan  阅读(78)  评论(0)    收藏  举报