C++ Primer Plus 第六版 第十三章 练习题参考答案

//1 *************************
//cd.h
#ifndef CD_H_
#define CD_H_
#include<iostream>

class Cd
{
private:
    char performers[50];
    char label[30];
    int selections;
    double playtime;
protected:
    struct Formatting
    {
        std::ios_base::fmtflags flag;
        std::streamsize pr;
    };
    Formatting SetFormat() const;
public:
    Cd(char *s1, char *s2, int n, double x);
    Cd(const Cd & d);
    Cd();
    virtual ~Cd();
    virtual void Report() const;
    Cd & operator=(const Cd & d);
    friend std::ostream & operator<<(std::ostream &os,char *s) ;
};
class Classic : public Cd
{
private:
    char songs1[50];
public:
    Classic(char *s0 = "Null", char * s1 = "Null", char * s2 = "Null", int n = 0, double x = 0.0);
    Classic(const Cd & c, char * s1 = "Null");
    ~Classic();
    virtual void Report() const;
};
#endif // CD_H_
//cd.cpp
#include<iostream>
#include"cd.h"
using std::ios_base;


Cd::Formatting Cd::SetFormat() const
{
    Formatting f;
    f.flag =
        std::cout.setf(ios_base::fixed, ios_base::floatfield);
    f.pr = std::cout.precision(2);
    return f;
}

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

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

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

Cd::~Cd()
{
}

void Cd::Report() const
{
    using std::cout;
    using std::endl;
    Formatting f = SetFormat();
    cout << "performers: " << performers << endl;
    cout << "label: " << label << endl;
    cout << "selections: " << selections << endl;
    cout << "playtime: " << playtime << endl;
}

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

std::ostream & operator<<(std::ostream& os,char s[])
{
    for (int i = 0; i < strlen(s); ++i)
        os << s[i];
    return os;
}

Classic::Classic(char * s0, char * s1, char * s2, int n, double x) :Cd(s1,s2,n,x)
{
    strcpy_s(songs1, strlen(s0) + 1, s0);
}

Classic::Classic(const Cd & c, char * s1) :Cd(c)
{
    strcpy_s(songs1, strlen(s1) + 1, s1);
}

Classic::~Classic()
{
}

void Classic::Report() const
{
    using std::cout;
    using std::endl;
    Formatting f = SetFormat();
    Cd::Report();
    cout << "songs1 is: " << songs1 << endl;
}
//main.cpp
#include <iostream>
#include <cstdlib>     //rand,srand() prototypes
#include <ctime>   //time() prototype
#include<stdlib.h>
#include "cd.h"   
using std::cout;
using std::endl;
using std::cin;
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();
    system("pause");
    return 0;
}
void Bravo(const Cd & disk)
{
    disk.Report();
}
//2***************************************
//cd.h
#ifndef CD_H_
#define CD_H_
#include<iostream>

class Cd
{
private:
    char *performers;
    char *label;
    int selections;
    double playtime;
protected:
    struct Formatting
    {
        std::ios_base::fmtflags flag;
        std::streamsize pr;
    };
    Formatting SetFormat() const;
public:
    Cd(char *s1, char *s2, int n, double x);
    Cd(const Cd & d);
    Cd();
    virtual ~Cd();
    virtual void Report() const;
    Cd & operator=(const Cd & d);
    friend std::ostream & operator<<(std::ostream &os,char *s) ;
};
class Classic : public Cd
{
private:
    char *songs1;
public:
    Classic(char *s0 = "Null", char * s1 = "Null", char * s2 = "Null", int n = 0, double x = 0.0);
    Classic(const Cd & c, char * s1 = "Null");
    ~Classic();
    virtual void Report() const;
};
#endif // CD_H_
//cd.cpp
#include<iostream>
#include"cd.h"
using std::ios_base;


Cd::Formatting Cd::SetFormat() const
{
    Formatting f;
    f.flag =
        std::cout.setf(ios_base::fixed, ios_base::floatfield);
    f.pr = std::cout.precision(2);
    return f;
}

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

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

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

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

void Cd::Report() const
{
    using std::cout;
    using std::endl;
    Formatting f = SetFormat();
    cout << "performers: " << performers << endl;
    cout << "label: " << label << endl;
    cout << "selections: " << selections << endl;
    cout << "playtime: " << playtime << endl;
}

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

std::ostream & operator<<(std::ostream& os,char s[])
{
    for (int i = 0; i < strlen(s); ++i)
        os << s[i];
    return os;
}

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

Classic::Classic(const Cd & c, char * s1) :Cd(c)
{
    strcpy_s(songs1, strlen(s1) + 1, s1);
}

Classic::~Classic()
{
}

void Classic::Report() const
{
    using std::cout;
    using std::endl;
    Formatting f = SetFormat();
    Cd::Report();
    cout << "songs1 is: " << songs1 << endl;
}
//3**********************
//使用ABC,即抽象类结构进行类的派生和多态实现。
//DMA.h
#ifndef DMA_H_
#define DMA_H_
#include<iostream>

// base DMA
class baseDMA {
private:
    char * label;
    int rating;
protected:
    void show() { std::cout << label; };
    const int Rate() { return rating; };
public:
    baseDMA(const char * l = "null", int r = 0);
    baseDMA(const baseDMA &rs);
    virtual ~baseDMA();
    baseDMA & operator=(const baseDMA &rs);
    virtual void view()  = 0; //pure virtual function;
    friend std::ostream &operator<<(std::ostream &os, char *c);

};
//lackDMA
class lackDMA :public baseDMA
{
private:
    enum { COL_LEN=40 };
    char color[COL_LEN];
public:
    lackDMA(const char *l = "null", int r = 0, char *c = "blank") ;
    virtual ~lackDMA();
    virtual void view();
};
//hasDMA
class hasDMA :public baseDMA
{
private:
    char * style;
public:
    hasDMA(const char *l = "null", int r = 0, char *s = "none");
    hasDMA(const char *s, const baseDMA &rs);
    hasDMA(const hasDMA & hs);
    virtual ~hasDMA();
    virtual void view() ;
};
#endif // !DMA_H_
//DMA.cpp
#include<iostream>
#include"DMA.h"
using std::endl;
using std::cout;
using std::cin;
using std::ios_base;

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

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

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

std::ostream & operator<<(std::ostream & os, char *s)
{
    for (int i = 0; i < strlen(s) + 1; i++)
        cout << s[i];
    return os;
}

baseDMA & baseDMA::operator=(const baseDMA & rs)
{
    label = new char[strlen(rs.label) + 1];
    label = rs.label;
    rating = rs.rating;
    return *this;
}
//lackDMA
lackDMA::lackDMA(const char * l, int r, char * c):baseDMA(l,r)
{
    strcpy_s(color, strlen(c) + 1, c);
}

lackDMA::~lackDMA()
{
}

void lackDMA::view()
{
    cout << "Label: ";
    show();
    cout << endl;
    cout << "Rate: " << Rate() << endl;
    cout << "Color: " << color << endl;
}
//hasDMA
hasDMA::hasDMA(const char * l, int r, char * s):baseDMA(l,r) 
{
    style = new char[strlen(s) + 1];
    strcpy_s(style, strlen(s) + 1, s);
}

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

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

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

void hasDMA::view()
{
    cout << "Label: ";
    show();
    cout << endl;
    cout << "Rate: " << Rate() << endl;
    cout << "Style: " << style << endl;
}
//main.cpp
#include <iostream>
#include <cstdlib>     //rand,srand() prototypes
#include<stdlib.h>
#include"DMA.h"
using std::cout;
using std::endl;
using std::cin;
const int MAX = 2;
int main()
{
    baseDMA *p[MAX];
    char *s1 = "good";
    char *s2 = "bad";
    char *s3 = "normal";
    int r1 = 1;
    int r2 = 2;
    int r3 = 3;
    char *c1 = "dark";
    char *st1 = "simple";
    p[0] = new lackDMA(s1, r1,c1);
    p[1] = new hasDMA(s3, r2, st1);
    for (int i = 0; i < MAX; i++)
        p[i]->view(),
        cout << endl;
    for (int i = 0; i < MAX; i++)
        delete p[i];
    cout << "Done.\n";
    system("pause");
    return 0;
}
//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);//copy constructor;
    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);
    friend ostream & operator<<(ostream & os, const char * c);
};
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; };
    VintagePort & operator=(const VintagePort &vp);
    void Show() const;
    friend ostream & operator<<(ostream & os, const VintagePort &vp);
};
#endif // !PORT_H_
//port.cpp
#include<iostream>
#include"port.h"
using std::endl;
using std::cout;
using std::cin;
using std::ios_base;

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)
{
    delete[] brand;
    brand = new char[strlen(p.brand) + 1];
    strcpy_s(brand, strlen(p.brand) + 1, p.brand);
    bottles = p.bottles;
    return *this;
}

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

Port & Port::operator-=(int b)
{
    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 << "\n";
    return os;
}

ostream & operator<<(ostream & os, const char * c)
{
    for (int i = 0; i < strlen(c); i++)
        os << c[i];
    return os;;
}

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

VintagePort::VintagePort() 
{
    Port();
    nickname[0] = '\0';
    year = 0;
}

VintagePort::VintagePort(const char * br, int b, const char * nn, int y):Port(br,"none",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
{
    cout << "Nickname: " << nickname <<endl;
    cout << "Year: " << year << endl;
}
//main.cpp
#include <iostream>
#include <cstdlib>     //rand,srand() prototypes
#include<stdlib.h>
#include"port.h"
using std::cout;
using std::endl;
using std::cin;
int main()
{
    char *b1 = "benzs";
    char *s1 = "normal";
    int bo1 = 2;
    Port p1(b1, s1, bo1);
    Port p2 = p1;
    p1.Show();
    p2.Show();
    cout << p1;
    cout << p2;
    char *n1 = "bz";
    int y1 = 2001;
    VintagePort v1(b1, bo1, n1, y1);
    VintagePort v2 = v1;
    v1.Show();
    v2.Show();
    cout << v1;
    cout << v2;
    system("pause");
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,001评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,210评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,874评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,001评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,022评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,005评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,929评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,742评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,193评论 1 309
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,427评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,583评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,305评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,911评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,564评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,731评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,581评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,478评论 2 352