C++ Primer Plus, Chapter 13, excercise

1.CD

//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(const CD & d);
    CD();
    virtual ~CD();
    virtual void Report() const;
    //CD & operator=(const CD & d);
};
class Classic :public CD
{
private:
    char main[50];
public:
    Classic(const char * m = "null", const char * s1 = "nullbody", const char * s2 = "null", int n = 0, double x = 0.0);
    Classic(const char * m, const CD & d);
    virtual void Report() const;
};
#endif

//classic.cpp
#include "stdafx.h"
#include "classic.h"
#include <iostream>

//CD
CD::CD(const char * s1, const char * s2, int n, double x)
{
    strcpy(performers, s1);
    strcpy(label, s2);
    selections = n;
    playtime = x;
}
CD::CD()
{
    strcpy(performers, "nullbody");
    strcpy(label, "null");
    selections = 0;
    playtime = 0.0;
}
CD::~CD()
{

}
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;
}

//Classic
Classic::Classic(const char * m, const char * s1, const char * s2, int n, double x) :CD(s1, s2, n, x)
{
    strcpy(main, m);
}
Classic::Classic(const char * m, const CD & d) : CD(d)
{
    strcpy(main, m);
}
void Classic::Report() const
{
    CD::Report();
    std::cout << "main: " << main << std::endl;
}

//main.cpp
#include "stdafx.h"
#include "classic.h"
#include <iostream>

void Bravo(const CD & disk);
int main()
{
    using std::cout;

    CD c1("Beatles", "Capitol", 14, 35.5);
    Classic c2 = Classic("Piano Sonata in B flat, Fantansia 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();
    std::cin.get();
    return 0;
}
void Bravo(const CD & disk)
{
    disk.Report();
}

2.动态分配内存完成1

//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(const CD & d);
    CD();
    virtual ~CD();
    virtual void Report() const;
    CD & operator=(const CD & d);
};
class Classic :public CD
{
private:
    char * main;
public:
    Classic(const char * m = "null", const char * s1 = "nullbody", const char * s2 = "null", int n = 0, double x = 0.0);
    Classic(const char * m, const CD & d);
    virtual void Report() const;
    Classic(const Classic & c);
    Classic & operator=(const Classic & c);
    ~Classic();
};
#endif

//classic.cpp
#include "stdafx.h"
#include "classic.h"
#include <iostream>

//CD
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()
{
    performers = nullptr;
    //strcpy(performers, "nullbody");
    label = nullptr;
    //strcpy(label, "null");
    selections = 0;
    playtime = 0.0;
}
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(const CD & d)
{
    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;
}
CD & CD::operator=(const CD & d)
{
    if (this == &d)
        return *this;
    else
    {
        delete[] performers;
        performers = new char[strlen(d.performers) + 1];
        strcpy(performers, d.performers);
        delete[] label;
        label = new char[strlen(d.label) + 1];
        strcpy(label, d.label);
        selections = d.selections;
        playtime = d.playtime;
        return *this;
    }

}

//Classic
Classic::Classic(const char * m, const char * s1, const char * s2, int n, double x) :CD(s1, s2, n, x)
{
    main = new char[strlen(m) + 1];
    strcpy(main, m);
}
Classic::Classic(const char * m, const CD & d) : CD(d)
{
    main = new char[strlen(m) + 1];
    strcpy(main, m);
}
void Classic::Report() const
{
    CD::Report();
    std::cout << "main: " << main << std::endl;
}
Classic::Classic(const Classic & c) :CD(c)
{
    main = new char[strlen(c.main) + 1];
    strcpy(main, c.main);
}
Classic & Classic::operator=(const Classic & c)
{
    if (this == &c)
        return *this;
    else
    {
        CD::operator=(c);
        delete[] main;
        main = new char[strlen(c.main) + 1];
        strcpy(main, c.main);
        return *this;
    }
}
Classic::~Classic()
{
    delete[] main;
}

//main.cpp
#include "stdafx.h"
#include "classic.h"
#include <iostream>

void Bravo(const CD & disk);
int main()
{
    using std::cout;

    CD c1("Beatles", "Capitol", 14, 35.5);
    Classic c2 = Classic("Piano Sonata in B flat, Fantansia 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();
    std::cin.get();
    return 0;
}
void Bravo(const CD & disk)
{
    disk.Report();
}

3.三个DMA由ABC派生

//dma.h
#ifndef DMA_H_
#define DMA_H_
#include <iostream>
class DMA
{
private:
    char * label;
    int rating;
public:
    DMA(const char * l = "null", int r = 0);
    DMA(const DMA & rs);
    virtual ~DMA();
    DMA & operator=(const DMA & rs); //不是共有的,不能用作纯虚函数
    //friend std::ostream & operator<<(std::ostream & os, const DMA & rs);
    virtual void View() const = 0;
};
class baseDMA :public DMA
{
public:
    baseDMA(const char * l = "null", int r = 0);
    //friend std::ostream & operator<<(std::ostream & os, const baseDMA & bs);
    virtual void View() const;
};
class lacksDMA :public DMA
{
private:
    enum{ COL_LEN = 40 };
    char color[COL_LEN];
public:
    lacksDMA(const char * c = "black", const char * l = "null", int r = 0);
    lacksDMA(const char * c, const DMA & rs);
    //friend std::ostream & operator<<(std::ostream & os, const lacksDMA & ls);
    virtual void View() const;
};
class hasDMA :public DMA
{
private:
    char * style;
public:
    hasDMA(const char * s = "none", const char * l = "null", int r = 0);
    hasDMA(const char * s, const DMA & rs);
    hasDMA(const hasDMA & hs);
    ~hasDMA();
    virtual hasDMA & operator=(const hasDMA & hs);
    //friend std::ostream & operator<<(std::ostream & os, const hasDMA & hs);
    virtual void View() const;
};
#endif

//dma.cpp
#include "stdafx.h"
#include "dma.h"

//DMA
DMA::DMA(const char * l, int r)
{
    label = new char[strlen(l) + 1];
    strcpy(label, l);
    rating = r;
}
DMA::DMA(const DMA & rs)
{
    label = new char[strlen(rs.label) + 1];
    strcpy(label, rs.label);
    rating = rs.rating;
}
DMA::~DMA()
{
    delete[] label;
}
DMA & DMA::operator=(const DMA & rs)
{
    if (this == &rs)
        return *this;
    else
    {
        delete[] label;
        label = new char[strlen(rs.label) + 1];
        strcpy(label, rs.label);
        rating = rs.rating;
        return *this;
    }
}
void DMA::View() const
{
    std::cout << "label: " << label << std::endl;
    std::cout << "rating: " << rating << std::endl;
}
/*std::ostream & operator<<(std::ostream & os, const DMA & rs)
{
    os << "label: " << rs.label << std::endl;
    os << "rating: " << rs.rating << std::endl;
    return os;
}*/

//baseDMA
baseDMA::baseDMA(const char * l, int r) :DMA(l, r)
{

}
void baseDMA::View() const
{
    DMA::View();
}
/*std::ostream & operator<<(std::ostream & os, const baseDMA & bs)
{
    os << (const DMA &)bs;
    return os;
}*/


//lacksDMA
lacksDMA::lacksDMA(const char * c, const char * l, int r) :DMA(l, r)
{
    strcpy(color, c);
}
lacksDMA::lacksDMA(const char * c, const DMA & rs) : DMA(rs)
{
    strcpy(color, c);
}
void lacksDMA::View() const
{
    DMA::View();
    std::cout << "color: " << color << std::endl;
}
/*std::ostream & operator<<(std::ostream & os, const lacksDMA & ls)
{
    os << (const DMA &)ls; //输出baseDMA的
    os << "color: " << ls.color << std::endl;
    return os;
}*/

//hasDMA
hasDMA::hasDMA(const char * s, const char * l, int r) :DMA(l, r)
{
    style = new char[strlen(s) + 1];
    strcpy(style, s);
}
hasDMA::hasDMA(const char * s, const DMA & rs) : DMA(rs)
{
    style = new char[strlen(s) + 1];
    strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs) : DMA(hs)
{
    style = new char[strlen(hs.style) + 1];
    strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{
    delete[] style;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
    if (this == &hs)
        return *this;
    else
    {
        DMA::operator=(hs); //赋值DMA的
        delete[] style;
        style = new char[strlen(hs.style) + 1];
        strcpy(style, hs.style);
        return *this;
    }
}
void hasDMA::View() const
{
    DMA::View();
    std::cout << "style: " << style << std::endl;
}
/*std::ostream & operator<<(std::ostream & os, const hasDMA & hs)
{
    os << (const DMA &)hs;
    os << "style: " << hs.style << std::endl;
    return os;
}*/

//main.cpp
#include "stdafx.h"
#include "dma.h"
#include <ctime>
#include <string>

const int CLIENT = 4;
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;

    DMA * p_clients[CLIENT];
    char lb[50]; //不能用string, string 不能转为char
    int rt;
    char kind;
    for (int i = 0; i < CLIENT; i++)
    {
        cout << "Enter label: ";
        cin.getline(lb, 50);
        cout << "Enter rating: ";
        cin >> rt;
        while (cin.get() != '\n') //读取换行符有时候要有时候不要
            continue;
        cout << "Enter 1 for baseDMA, 2 for lacksDMA or 3 for hasDMA: ";
        while (cin >> kind && kind != '1' && kind != '2' && kind != '3')
        {
            cout << "Enter 1, 2 or 3: ";
        }
        while (cin.get() != '\n')
            continue;
        if (kind == '1')
            p_clients[i] = new baseDMA(lb, rt);
        else if (kind == '2')
        {
            char cl[50];
            cout << "Enter color: ";
            cin.getline(cl, 50);
            p_clients[i] = new lacksDMA(cl, lb, rt);
        }
        else
        {
            char st[50];
            cout << "Enter style: ";
            cin.getline(st, 50);
            p_clients[i] = new hasDMA(st, lb, rt);
        }
    }
    cout << endl;
    for (int i = 0; i < CLIENT; i++)
    {
        p_clients[i]->View();
        cout << endl;
    }
    for (int i = 0; i < CLIENT; i++)
        delete p_clients[i];
    cout << "Done!\n";


    clock_t delay = 100 * CLOCKS_PER_SEC;
    clock_t start = clock();
    while (clock() - start < delay)
        ;
}

4.葡萄酒类

//port.h
#ifndef PORT_H_
#define 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(const char * br, const char * st, int b, const char * nn, int y);
    VintagePort(const VintagePort & vp);
    ~VintagePort() { delete[] nickname; }
    VintagePort & operator=(const VintagePort & vp);
    void Show() const;
    friend ostream & operator<<(ostream & os, const VintagePort & vp);
};
#endif

//port.cpp
#include "stdafx.h"
#include "port.h"

//Port
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(const Port & p)
{
    brand = new char[strlen(p.brand) + 1];
    strcpy(brand, p.brand);
    strcpy(style, p.style);
    bottles = p.bottles;
}
Port & Port::operator=(const Port & p)
{
    if (this == &p)
        return *this;
    else
    {
        delete[] brand;
        brand = new char[strlen(p.brand) + 1];
        strcpy(brand, p.brand);
        strcpy(style, p.style);
        bottles = p.bottles;
        return *this;
    }
}
Port & Port::operator+=(int b)
{
    this->bottles += b;
    return *this;
}
Port & Port::operator-=(int b)
{
    this->bottles -= b;
    return *this;
}
void Port::Show() const
{
    cout << "Brand: " << brand << endl;
    cout << "Kind: " << style << endl;
    cout << "Bottles: " << bottles << endl;
}
ostream & operator<<(ostream & os, const Port & p)
{
    os << p.brand << ", " << p.style << ", " << p.bottles;
    return os;
}

//VintagePort
VintagePort::VintagePort(const char * br, const char * st, int b, const char * nn, int y) :Port(br, st, 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;
    else
    {
        Port::operator=(vp);
        delete[] nickname;
        nickname = new char[strlen(vp.nickname) + 1];
        strcpy(nickname, 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.cpp
#include "stdafx.h"
#include "port.h"

int main()
{
    Port p("WLJ", "hong", 20);
    VintagePort vp("MZ", "hei", 30, "maoz", 1987);
    cout << "p:\n";
    p.Show();
    cout << endl;
    cout << p << endl;
    cout << "vp:\n";
    vp.Show();
    cout << endl << vp << endl;

    cout << "p += 20: ";
    p += 20;
    p.Show();
    cout << "\nvp -= 20: ";
    vp -= 20;
    vp.Show();

    cout << endl << "copy constructor: \n";
    Port p1(p);
    p1.Show();
    cout << "fu zhi yun suan fu:\n";
    VintagePort vp2 = vp;
    vp2.Show();

    cin.get();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值