C++ Primer Plus第十三章练习

13.11.1
成员数据并无不需要动态内存分配所以不需要拷贝构造与赋值构造
Cd.h

#ifndef CD_H
#define CD_H
#include <iostream>
#include <cstring>
using namespace std;
class Cd
{
private:
    char performers[50];
    char label[20];
    int selections;
    double playtime;
public:
    Cd(void);
    Cd(const char *, const char *, int, double);
    virtual ~Cd(void) {};
    virtual void Report(void) const;
};
class Classic : public Cd
{
private:
    char significance[30];
public:
    Classic(const char * s1 = "none", const char * s2 = "none", const char * s3 = "none", int n = 0, double m = 0);
    void Report(void) const;
};
#endif

Cd.cpp

#include "Cd.h"
Cd::Cd(void)
{
    strcpy(performers, "none");
    strcpy(label, "none");
    selections = playtime = 0;
}
Cd::Cd(const char * s1, const char * s2, int n, double m) : selections(n), playtime(m)
{
    strcpy(performers, s1);
    strcpy(label, s2);
}
void Cd::Report(void) const
{
    cout << "performers: " << performers << endl;
    cout << "label: " << label << endl;
    cout << "selections: " << selections << endl;
    cout << "playtime: " << playtime << endl;
}
Classic::Classic(const char * s1, const char * s2, const char * s3, int n, double m) : Cd(s1, s2, n, m)
{
    strcpy(significance, s3);
}
void Classic::Report(void) const
{
    Cd::Report();
    cout << "significance: " << significance << endl;
}

13.11.2
Cd.h

#ifndef CD_H
#define CD_H
#include <iostream>
#include <cstring>
using namespace std;
class Cd
{
private:
    char * performers;
    char * label;
    int selections;
    double playtime;
public:
    Cd(void);
    Cd(const Cd &);
    Cd(const char *, const char *, int, double);
    virtual ~Cd(void);
    virtual void Report(void) const;
    Cd & operator=(const Cd &);
};
class Classic : public Cd
{
private:
    char * significance;
public:
    Classic(const Classic &);
    Classic(const char * s1 = "none", const char * s2 = "none", const char * s3 = "none", int n = 0, double m = 0);
    ~Classic(void);
    void Report(void) const;
    Classic & operator=(const Classic &);
};
#endif

Cd.cpp

#include "Cd.h"
Cd::Cd(void)
{
    performers = new char[50];
    strcpy(performers, "none");
    label = new char[30];
    strcpy(label, "none");
    selections = playtime = 0;
}
Cd::Cd(const Cd & c)
{
    performers = new char[strlen(c.performers) + 1];
    strcpy(performers, c.performers);
    label = new char[strlen(c.label) + 1];
    strcpy(label, c.label);
    selections = c.selections;
    playtime = c.playtime;
}
Cd::Cd(const char * s1, const char * s2, int n, double m) : selections(n), playtime(m)
{
    performers = new char[strlen(s1) + 1];
    strcpy(performers, s1);
    label = new char[strlen(s2) + 1];
    strcpy(label, s2);
}
Cd::~Cd(void)
{
    delete [] performers;
    delete [] label;
}
void Cd::Report(void) const
{
    cout << "performers: " << performers << endl;
    cout << "label: " << label << endl;
    cout << "selections: " << selections << endl;
    cout << "playtime: " << playtime << endl;
}
Cd & Cd::operator=(const Cd & c)
{
    if(this == &c)
        return *this;
    delete [] performers;
    delete [] label;
    performers = new char[strlen(c.performers) + 1];
    strcpy(performers, c.performers);
    label = new char[strlen(c.label) + 1];
    strcpy(label, c.label);
    selections = c.selections;
    playtime = c.playtime;
    return *this;
}
Classic::Classic(const Classic & c) : Cd()
{
    significance = new char[strlen(c.significance) + 1];
    strcpy(significance, "none");
}
Classic::Classic(const char * s1, const char * s2, const char * s3, int n, double m) : Cd(s1, s2, n, m)
{
    significance = new char[strlen(s3) + 1];
    strcpy(significance, s3);
}
Classic::~Classic(void)
{
    delete [] significance;
}
void Classic::Report(void) const
{
    Cd::Report();
    cout << "significance: " << significance << endl;
}
Classic & Classic::operator=(const Classic & c)
{
    if(this == &c)
        return *this;
    delete [] significance;
    Cd::operator=(c);
    significance = new char[strlen(c.significance) + 1];
    strcpy(significance, c.significance);
    return *this;
}

13.11.3
写的时候没注意 应该把View设置为纯虚 这里让派生类使用基类的友元但是是用的基类指针来指向的派生类对象 所以打印出来的都是基类格式
dma.h

#ifndef DMA_A
#define DMA_A
#include <iostream>
#include <cstring>
using namespace std;
class dmaABC
{
private:
    char * label;
    int rating;
public:
    dmaABC(const dmaABC &);
    dmaABC(const char * l = "null", int r = 0);
    virtual ~dmaABC(void) = 0;
    dmaABC & operator=(const dmaABC &);
    friend ostream & operator<<(ostream &, const dmaABC &);
};
class baseDMA : public dmaABC
{
public:
    baseDMA(const char *, int);
    friend ostream & operator<<(ostream &, const baseDMA &);
};
class lacksDMA : public dmaABC
{
private:
    enum { COL_LEN = 40 };
    char color[COL_LEN];
public:
    lacksDMA(const char *, const dmaABC &);
    lacksDMA(const char *, int, const char * c = "blank");
    friend ostream & operator<<(ostream &, const lacksDMA &);
};
class hasDMA : public dmaABC
{
private:
    char * style;
public:
    hasDMA(const hasDMA &);
    hasDMA(const char *, const dmaABC &);
    hasDMA(const char *, int, const char * s = "none");
    ~hasDMA(void);
    hasDMA & operator=(const hasDMA &);
    friend ostream & operator<<(ostream &, const hasDMA &);
};
#endif

dma.cpp

#include "dma.h"
dmaABC::dmaABC(const dmaABC & d)
{
    label = new char[strlen(d.label) + 1];
    strcpy(label, d.label);
    rating = d.rating;
}
dmaABC::dmaABC(const char * l, int r)
{
    label = new char[strlen(l) + 1];
    strcpy(label, l);
    rating = r;
}
dmaABC::~dmaABC(void)
{
    delete [] label;
}
dmaABC & dmaABC::operator=(const dmaABC & d)
{
    if(this == &d)
        return *this;
    label = new char[strlen(d.label) + 1];
    strcpy(label, d.label);
    rating = d.rating;
    return *this;
}
ostream & operator<<(ostream & out, const dmaABC & d)
{
    out << "label: " << d.label << endl;
    out << "rating: " << d.rating << endl;
    return out;
}
baseDMA::baseDMA(const char * l, int r) : dmaABC(l, r) {}
ostream & operator<<(ostream & out, const baseDMA & b)
{
    out << (const dmaABC &)b;
    return out;
}
lacksDMA::lacksDMA(const char * c, const dmaABC & d) : dmaABC(d)
{
    strcpy(color, c);
}
lacksDMA::lacksDMA(const char * s, int r, const char * c) : dmaABC(s, r)
{
    strcpy(color, c);
}
ostream & operator<<(ostream & out, const lacksDMA & l)
{
    out << (const dmaABC &)l;
    out << l.color << endl;
    return out;
}
hasDMA::hasDMA(const hasDMA & h) : dmaABC(h)
{
    style = new char[strlen(h.style) + 1];
    strcpy(style, h.style);
}
hasDMA::hasDMA(const char * s, const dmaABC & d) : dmaABC(d)
{
    style = new char[strlen(s) + 1];
    strcpy(style, s);
}
hasDMA::hasDMA(const char * l, int r, const char * s) : dmaABC(l, r)
{
    style = new char[strlen(s) + 1];
    strcpy(style, s);
}
hasDMA::~hasDMA(void)
{
    delete [] style;
}
hasDMA & hasDMA::operator=(const hasDMA & h)
{
    if(this == &h)
        return *this;
    dmaABC::operator=(h);
    delete [] style;
    style = new char[strlen(h.style) + 1];
    strcpy(style, h.style);
    return *this;
}
ostream & operator<<(ostream & out, const hasDMA & h)
{
    out << (const dmaABC &)h;
    out << h.style << endl;
    return out;
}

main.cpp

#include "dma.h"
int main(void)
{
    dmaABC * p[4];
    char s[40];
    int r;
    char choice;
    for(int i = 0; i < 4; i++)
    {
        cout << "请输入一个字符串: ";
        cin.getline(s, 39);
        cout << "请输入一个数值: ";
        (cin >> r).get();
        cout << "请输入一个选择: ";
        cin >> choice;
        switch(choice)
        {
        case '1':
            p[i] = new baseDMA(s, r);
            break;
        case '2':
        {
            char temp[40];
            cout << "请在输入一个字符串: ";
            cin >> temp;
            p[i] = new lacksDMA(s, r, temp);
            break;
        }
        case '3':
            char temp[40];
            cout << "请在输入一个字符串: ";
            cin >> temp;
            p[i] = new hasDMA(s, r, temp);
            break;
        }
        while(cin.get() != '\n')
            continue;
    }
    cout << endl;
    for(int i = 0; i < 4; i++)
        cout << *p[i] << endl;
    for(int i = 0; i < 4; i++)
        delete p[i];
    cout << "Done.\n";
    return 0;
}

13.11.1
a

Port::Port(const Port & p)
{
    strcpy(style, p.style);
    bottles = p.bottles;
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
}
Port::Port(const char * br, const char * st, int b)
{
    brand = new char[strlen(br) + 1];
    strcpy(brand, br);
    strcpy(style, st);
    bottles = b;
}
Port & Port::operator=(const Port & p)
{
    if(this == &p)
        return *this;
    delete [] brand;
    strcpy(style, p.style);
    bottles = p.bottles;
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
    return *this;
}
Port & Port::operator+=(int b)
{
    bottles += b;
    return *this;
}
Port & Port::operator-=(int b)
{
    bottles -= b;
    return *this;
}
void Port::Show(void) const
{
    cout << "Brand: " << brand << endl;
    cout << "Kind: " << style << endl;
    cout << "Bottles: " << bottles << endl;
}
ostream & operator<<(ostream & out, const Port & p)
{
    out << p.brand << ", " << p.style << ", " << p.bottles << endl;
}

b
因为不需要才没重新定义的吧
c
运算符是不能声明为虚函数的
d

VintagePort::VintagePort(void) : Port("none", "none", 0)
{
    nickname = new char [5];
    strcpy(nickname, "none");
}
VintagePort::VintagePort(const char * br, int b, const char * nn, int y) : Port(br, "none", b)
{
    nickname = new char [strlen(nn) + 1];
    strcpy(nickname, nn);
    year = y;
}
VintagePort::VintagePort(const VintagePort & vp) : Port(vp)
{
    nickname = new char [strlen(vp.nickname) + 1];
    strcpy(nickname, vp.nickname);
    year = vp.year;
}
VintagePort & VintagePort::operator=(const VintagePort & vp)
{
    if(this == &vp)
        return *this;
    delete [] nickname;
    Port::operator=(vp);
    nickname = new char [strlen(vp.nickname) + 1];
    strcpy(nickname, vp.nickname);
    year = vp.year;
    return *this;
}
void VintagePort::Show(void) const
{
    Port::Show();
    cout << "Nickname: " << nickname << endl;
    cout << "Year: " << year << endl;
}
ostream & operator<<(ostream & out, const VintagePort & vp)
{
    out << (const Port &)vp;
    cout << ", " << vp.nickname << ", " << vp.year << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值