C++ Primer Plus第六版 編程練習第十三章

1.

classic.h

#ifndef CLASSIC_H_
#define CLASSIC_H_

class Cd
{
private:
    char performers[50];
    char label[20];
    int selections;
    double playtime;

public:
    Cd(const char* s1, const char* s2, int n, double x);
    Cd();
    virtual ~Cd() {};
    virtual void Report() const;
    Cd& operator=(const Cd& d);
};

class Classic : public Cd
{
private:
    char works[100];

public:
    Classic(const char* w, const char* s1, const char* s2, int n, double x);
    Classic();
    virtual ~Classic() {};
    virtual void Report() const;
    Classic& operator=(const Classic& d);
};

#endif

classic.cpp

#include "classic.h"
#include <iostream>
#include <cstring>

Cd::Cd(const char* s1, const char* s2, int n, double x)
{
    strcpy_s(performers, s1);
    strcpy_s(label, s2);
    selections = n;
    playtime = x;
}

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

void Cd::Report() const
{
    std::cout << "performers: " << performers << std::endl;
    std::cout << "label: " << label << std::endl;
    std::cout << "selections: " << selections << std::endl;
    std::cout << "playtime: " << playtime << std::endl;
}

Cd& Cd::operator=(const Cd& d)
{
    if (this == &d)
        return *this;
    strcpy_s(performers, d.performers);
    strcpy_s(label, d.label);
    selections = d.selections;
    playtime = d.playtime;
    return *this;
}

Classic::Classic(const char* w, const char* s1, const char* s2, int n, double x)
    : Cd(s1, s2, n, x)
{
    strcpy_s(works, w);
}

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

void Classic::Report() const
{
    Cd::Report();
    std::cout << "works: " << works << std::endl;
}

Classic& Classic::operator=(const Classic& d)
{
    if (this == &d)
        return *this;
    Cd::operator=(d);
    strcpy_s(works, d.works);
    return *this;
}

main.cpp

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

void Bravo(const Cd& disk);

int main()
{
    Cd c1("Beatles", "Capitol", 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.

classic.h

#ifndef CLASSIC_H_
#define CLASSIC_H_

class Cd
{
private:
    char * performers;
    char * label;
    int selections;
    double playtime;

public:
    Cd(const char* s1, const char* s2, int n, double x);
    Cd();
    Cd(const Cd& d);
    virtual ~Cd();
    virtual void Report() const;
    Cd& operator=(const Cd& d);
};

class Classic : public Cd
{
private:
    char * works;

public:
    Classic(const char* w, const char* s1, const char* s2, int n, double x);
    Classic();
    Classic(const Classic& c);
    virtual ~Classic();
    virtual void Report() const;
    Classic& operator=(const Classic& d);
};

#endif

classic.cpp

#include "classic.h"
#include <iostream>
#include <cstring>

Cd::Cd(const char* s1, const char* s2, int n, double x)
{
    performers = new char[std::strlen(s1) + 1];
    strcpy_s(performers, strlen(s1) + 1,s1);
    label = new char[std::strlen(s2) + 1];
    strcpy_s(label, strlen(s2) + 1,s2);
    selections = n;
    playtime = x;
}

Cd::Cd()
{
    performers = new char[1];
    performers[0] = '\0';
    label = new char[1];
    label[0] = '\0';
    selections = 0;
    playtime = 0.0;
}

Cd::Cd(const Cd& d)
{
    performers = new char[std::strlen(d.performers) + 1];
    strcpy_s(performers, std::strlen(d.performers) + 1, d.performers);

    label = new char[std::strlen(d.label) + 1];
    strcpy_s(label, std::strlen(d.label) + 1,d.label);

    selections = d.selections;
    playtime = d.playtime;
}

Cd::~Cd()
{
    delete[] performers;
    delete[] label;
}

void Cd::Report() const
{
    std::cout << "performers: " << performers << std::endl;
    std::cout << "label: " << label << std::endl;
    std::cout << "selections: " << selections << std::endl;
    std::cout << "playtime: " << playtime << std::endl;
}

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

Classic::Classic(const char* w, const char* s1, const char* s2, int n, double x)
    : Cd(s1, s2, n, x)
{
    works = new char[std::strlen(w) + 1];
    strcpy_s(works, std::strlen(w) + 1, w);
}

Classic::Classic()  
{
    works = new char[1];
    works[0] = '\0';
}

Classic::Classic(const Classic& c)
    : Cd(c)
{
    works = new char[std::strlen(c.works) + 1];
    strcpy_s(works, std::strlen(c.works) + 1,c.works);
}

Classic::~Classic()
{
    delete[] works;
}
void Classic::Report() const
{
    Cd::Report();
    std::cout << "works: " << works << std::endl;
}

Classic& Classic::operator=(const Classic& c)
{
    if (this == &c)
        return *this;
    Cd::operator=(c);
    delete[] works;
    works = new char[std::strlen(c.works) + 1];
    strcpy_s(works, std::strlen(c.works) + 1,c.works);
    return *this;
}

main.cpp

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

void Bravo(const Cd& disk);

int main()
{
    Cd c1("Beatles", "Capitol", 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.

dma.h

#ifndef DMA_H_
#define DMA_H_
#include <iostream>

class ABC
{
private:
    char* label;
    int rating;

public:
    ABC(const char* l = "null", int r = 1);
    ABC(const ABC& a);
    virtual ~ABC() = 0;
    virtual void View() const;
    ABC& operator=(const ABC& a);
    friend std::ostream& operator<<(std::ostream& os, const ABC& a);
};

class baseDMA : public ABC
{
private:

public:
    baseDMA(const char* l = "null", int r = 0);
    friend std::ostream& operator<<(std::ostream& os,
        const baseDMA& rs);
};

class lacksDMA :public ABC
{
private:
    enum { COL_LEN = 40 };
    char color[COL_LEN];
public:
    lacksDMA(const char* c = "blank", const char* l = "null",
        int r = 0);
    lacksDMA(const char* c, const ABC& a);
    virtual void View() const;
    friend std::ostream& operator<<(std::ostream& os,
        const lacksDMA& rs);
};

class hasDMA :public ABC
{
private:
    char* style;
public:
    hasDMA(const char* s = "none", const char* l = "null",
        int r = 0);
    hasDMA(const char* s, const ABC& c);
    hasDMA(const hasDMA& hs);
    ~hasDMA();
    virtual void View() const;
    hasDMA& operator=(const hasDMA& rs);
    friend std::ostream& operator<<(std::ostream& os,
        const hasDMA& rs);
};

#endif

dma.cpp

#include "dma.h"
#include <cstring>

ABC::ABC(const char* l, int r)
{
    label = new char[std::strlen(l) + 1];
    strcpy_s(label, strlen(l) + 1,l);
    rating = r;
}

ABC::ABC(const ABC& a)
{
    label = new char[std::strlen(a.label) + 1];
    strcpy_s(label, strlen(a.label) + 1,a.label);
    rating = a.rating;
}

ABC::~ABC()
{
    delete[] label;
}

void ABC::View() const
{
    std::cout << "label: " << label << std::endl;
    std::cout << "rating: " << rating << std::endl;
}

ABC& ABC::operator=(const ABC& a)
{
    if (this == &a)
        return *this;

    delete[] label;
    label = new char[std::strlen(a.label) + 1];
    strcpy_s(label, strlen(a.label) + 1,a.label);

    rating = a.rating;
}

std::ostream& operator<<(std::ostream& os, const ABC& a)
{
    os << a.label << ", " << a.rating;
    return os;
}

baseDMA::baseDMA(const char* l, int r)
    : ABC(l, r)
{
}

std::ostream& operator<<(std::ostream& os, const baseDMA& rs)
{
    os << (const ABC&)rs;
    return os;
}

lacksDMA::lacksDMA(const char* c, const char* l, int r)
    : ABC(l, r)
{
    strcpy_s(color, c);
}

lacksDMA::lacksDMA(const char* c, const ABC& a)
    : ABC(a)
{
    strcpy_s(color, c);
}

void lacksDMA::View() const
{
    ABC::View();
    std::cout << color << std::endl;
}

std::ostream& operator<<(std::ostream& os, const lacksDMA& rs)
{
    os << rs.color << ", " << (const ABC&)rs;
    return os;
}


hasDMA::hasDMA(const char* s, const char* l, int r)
    : ABC(l, r)
{
    style = new char[std::strlen(s) + 1];
    strcpy_s(style, strlen(s) + 1, s);
}

hasDMA::hasDMA(const char* s, const ABC& c)
    : ABC(c)
{
    style = new char[std::strlen(s) + 1];
    strcpy_s(style, strlen(s) + 1, s);
}

hasDMA::hasDMA(const hasDMA& hs)
    : ABC(hs)
{
    style = new char[std::strlen(hs.style) + 1];
    strcpy_s(style, strlen(hs.style) + 1, hs.style);
}

hasDMA::~hasDMA()
{
    delete[] style;
}

void hasDMA::View() const
{
    ABC::View();
    std::cout << style << std::endl;
}

hasDMA& hasDMA::operator=(const hasDMA& hs)
{
    if (this == &hs)
        return *this;
    ABC::operator=(hs);
    delete[] style;
    style = new char[std::strlen(hs.style) + 1];
    strcpy_s(style, strlen(hs.style) + 1,hs.style);
    return *this;
}

std::ostream& operator<<(std::ostream& os, const hasDMA& rs)
{
    os << rs.style << ", " << (const ABC&)rs;
    return os;
}

main.cpp

#include <iostream>
#include "dma.h"

int main()
{
    using std::cout;
    using std::endl;

    baseDMA shirt("Portrabelly", 8);
    lacksDMA balloon("red", "Blumpo", 4);
    hasDMA map("Mercator", "Buffalo Kyes", 5);
    cout << shirt << endl;
    cout << balloon << endl;
    cout << map << endl;
    lacksDMA balloon2(balloon);
    hasDMA map2;
    map2 = map;
    cout << balloon2 << endl;
    cout << map2 << endl;

    ABC* pts[3];
    pts[0] = &shirt;
    pts[1] = &balloon;
    pts[2] = &map;

    for (int i = 0; i < 3; ++i)
        cout << *pts[i] << endl;
    for (int i = 0; i < 3; ++i)
        pts[i]->View();

    return 0;
}

4.

port.h

#ifndef PORT_H_
#define PORT_H_
#include <iostream>

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; }
    int BottleCount() const { return bottles; }
    Port& operator=(const Port& p);
    Port& operator+=(int b);
    Port& operator-=(int b);
    virtual void Show() const;
    friend std::ostream& operator<<(std::ostream& os, const Port& p);
};

#endif


class VintagePort : public Port
{
private:
    char* nickname;
    int year;

public:
    VintagePort(const char* br = "none", int b = 0, const char* nn = "none", int y = 0);
    VintagePort(const VintagePort& vp);
    ~VintagePort() { delete[] nickname; }
    VintagePort& operator=(const VintagePort& vp);
    virtual void Show() const;
    friend std::ostream& operator<<(std::ostream& os, const VintagePort& vp);
};

port.cpp

#include <cstring>
#include "port.h"

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

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

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

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

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

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

std::ostream& operator<<(std::ostream& os, const Port& p)
{
    os << p.brand << ", " << p.style << ", " << p.bottles;
    return os;
}


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

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

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

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

    return *this;
}

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

main.cpp

#include "port.h"
#include <iostream>

int main()
{
    Port p1;
    Port p2("Tsingtao", "Beer", 30);
    std::cout << p1 << std::endl;
    std::cout << p2 << std::endl;
    Port p3 = p2;
    p3.Show();
    p3 += 3;
    p3.Show();

    VintagePort vp1("Harbin", 50, "hb", 1992);
    vp1.Show();
    VintagePort vp2;
    vp2.Show();
    vp1 -= 3;
    vp2 = vp1;
    vp2.Show();

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章