C++ primer plus(第6版)中文版编程练习答案第13章.docx_第1页
C++ primer plus(第6版)中文版编程练习答案第13章.docx_第2页
C++ primer plus(第6版)中文版编程练习答案第13章.docx_第3页
C++ primer plus(第6版)中文版编程练习答案第13章.docx_第4页
C++ primer plus(第6版)中文版编程练习答案第13章.docx_第5页
已阅读5页,还剩11页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/cdplayer.h#ifndef CDPLAYER_H_#define CDPLAYER_H_#include #include using namespace std;class Cdprivate:char performers50;char label20;int selections;double playtime;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);class Classic :public Cdprivate:char main_music50;public:Classic()Classic(char *m, char *s1, char *s2, int n, double x);Classic(char *m, const Cd &c);Classic();Classic &operator=(const Classic &c);virtual void Report()const;#endif/cdplayer.cpp#include cdplayer.hCd: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, 50, d.performers);strcpy_s(label, 20, d.label);selections = d.selections;playtime = d.playtime;Cd:Cd()void Cd:Report()constcout Performers: performers endl;cout Label: label endl;cout Selections: selections endl;cout Playtime: playtime endl;Cd &Cd:operator=(const Cd &d)if (this = &d)return *this;strcpy_s(performers, 50, d.performers);strcpy_s(label, 20, d.label);selections = d.selections;playtime = d.playtime;return *this;Classic:Classic(char *m, char *s1, char *s2, int n, double x) :Cd(s1, s2, n, x)strcpy_s(main_music, strlen(m) + 1, m);Classic:Classic(char *m, const Cd &c) : Cd(c)strcpy_s(main_music, strlen(m) + 1, m);Classic:Classic()Classic &Classic:operator=(const Classic &c)if (this = &c)return *this;Cd:operator=(c);strcpy_s(main_music, 50, c.main_music);return *this;void Classic:Report()constCd:Report();cout Main Music: main_music endl;/main.cpp#include cdplayer.hvoid Bravo(const Cd &disk);int main()Cd c1(Beatles, Capito, 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 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、/cdplayer.h#ifndef CDPLAYER_H_#define CDPLAYER_H_#include #include using namespace std;class Cdprivate:char *performers;char *label;int selections;double playtime;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);class Classic :public Cdprivate:char *main_music;public:Classic()Classic(char *m, char *s1, char *s2, int n, double x);Classic(char *m, const Cd &c);Classic();Classic &operator=(const Classic &c);virtual void Report()const;#endif/cdplayer.cpp#include cdplayer.hCd:Cd(char *s1, char *s2, int n, double x)performers = new charstrlen(s1) + 1;label = new charstrlen(s2) + 1;strcpy_s(performers, strlen(s1) + 1, s1);strcpy_s(label, strlen(s2) + 1, s2);selections = n;playtime = x;Cd:Cd(const Cd &d)performers = new charstrlen(d.performers) + 1;label = new charstrlen(d.label) + 1;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()deleteperformers;deletelabel;void Cd:Report()constcout Performers: performers endl;cout Label: label endl;cout Selections: selections endl;cout Playtime: playtime endl;Cd &Cd:operator=(const Cd &d)if (this = &d)return *this;deleteperformers;deletelabel;performers = new charstrlen(d.performers) + 1;label = new charstrlen(d.label) + 1;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;Classic:Classic(char *m, char *s1, char *s2, int n, double x) :Cd(s1, s2, n, x)main_music = new charstrlen(m) + 1;strcpy_s(main_music, strlen(m) + 1, m);Classic:Classic(char *m, const Cd &c) : Cd(c)main_music = new charstrlen(m) + 1;strcpy_s(main_music, strlen(m) + 1, m);Classic:Classic()deletemain_music;Classic &Classic:operator=(const Classic &c)if (this = &c)return *this;Cd:operator=(c);deletemain_music;main_music = new charstrlen(c.main_music) + 1;strcpy_s(main_music, strlen(c.main_music) + 1, c.main_music);return *this;void Classic:Report()constCd:Report();cout Main Music: main_music endl;/main.cpp#include cdplayer.hvoid Bravo(const Cd &disk);int main()Cd c1(Beatles, Capito, 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 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();3、/dma.h#ifndef DMA_H_#define DMA_H_#include #include using namespace std;class ABCprivate:char *fullname;int level;public:ABC(const char *f = null, int l = 0);ABC(const ABC &ab);ABC &operator=(const ABC &ab);virtual ABC();virtual void View() = 0;class baseDMA :public ABCprivate:char *label;int rating;public:baseDMA(const char *l = null, int r = 0, const char *f = null, int lv = 0);baseDMA(const baseDMA &rs);baseDMA();baseDMA &operator=(const baseDMA &rs);virtual void View();class lacksDMA :public ABCprivate:enum COL_LEN = 40 ;char colorCOL_LEN;public:lacksDMA(const char *c = blank, const char *f = null, int lv = 0);lacksDMA(const char *c, const baseDMA &rs);virtual void View();class hasDMA :public ABCprivate:char *style;public:hasDMA(const char *s = none, const char *f = null, int lv = 0);hasDMA(const char *s, const ABC &rs);hasDMA(const hasDMA &hs);hasDMA();hasDMA &operator=(const hasDMA &hs);virtual void View();#endif/dma.cpp#include dma.hABC:ABC(const char *f, int lv)fullname = new charstrlen(f) + 1;strcpy_s(fullname, strlen(f) + 1, f);level = lv;ABC:ABC(const ABC &ab)fullname = new charstrlen(ab.fullname) + 1;strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);level = ab.level;ABC:ABC()deletefullname;ABC &ABC:operator=(const ABC &ab)if (this = &ab)return *this;deletefullname;fullname = new charstrlen(ab.fullname) + 1;strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);level = ab.level;return *this;void ABC:View()cout Fullname: fullname endl;cout Level: level endl;baseDMA:baseDMA(const char *l, int r, const char *f, int lv) :ABC(f, lv)label = new charstrlen(l) + 1;strcpy_s(label, strlen(l) + 1, l);rating = r;baseDMA:baseDMA(const baseDMA &rs) :ABC(rs)label = new charstrlen(rs.label) + 1;strcpy_s(label, strlen(rs.label) + 1, rs.label);rating = rs.rating;baseDMA:baseDMA()deletelabel;baseDMA &baseDMA:operator=(const baseDMA &rs)if (this = &rs)return *this;ABC:operator=(rs);deletelabel;label = new charstrlen(rs.label) + 1;strcpy_s(label, strlen(rs.label) + 1, rs.label);rating = rs.rating;return *this;void baseDMA:View()ABC:View();cout Label: label endl;cout Rating: rating endl;lacksDMA:lacksDMA(const char *c, const char *f, int lv) :ABC(f, lv)strncpy_s(color, c, 39);color39 = 0;lacksDMA:lacksDMA(const char *c, const baseDMA &rs) : ABC(rs)strncpy_s(color, c, COL_LEN - 1);colorCOL_LEN - 1 = 0;void lacksDMA:View()ABC:View();cout Color: color endl;hasDMA:hasDMA(const char *s, const char *f, int lv) :ABC(f, lv)style = new charstrlen(s) + 1;strcpy_s(style, strlen(s) + 1, s);hasDMA:hasDMA(const char *s, const ABC &ab) :ABC(ab)style = new charstrlen(s) + 1;strcpy_s(style, strlen(s) + 1, s);hasDMA:hasDMA(const hasDMA &hs) : ABC(hs)style = new charstrlen(hs.style) + 1;strcpy_s(style, strlen(hs.style) + 1, hs.style);hasDMA:hasDMA()deletestyle;hasDMA &hasDMA:operator=(const hasDMA &hs)if (this = &hs)return *this;ABC:operator=(hs);deletestyle;style = new charstrlen(hs.style) + 1;strcpy_s(style, strlen(hs.style) + 1, hs.style);return *this;void hasDMA:View()ABC:View();cout Style: style endl;/usedma.cpp#include dma.hint main()baseDMA shirt(Portabelly, 8, Jack, 1);lacksDMA balloon(red, Blimpo, 4);hasDMA map(Mercator, buffalo Keys, 5);cout Displaying baseDMA object:n;shirt.View();cout Displaying lacksDMA object:n;balloon.View();cout Displaying hasDMA object:n;map.View();lacksDMA balloon2(balloon);cout Result of lacksDMA copy:n;balloon2.View();hasDMA map2;map2 = map;cout Result of hasDMA assignment:n;map2.View();system(pause);return 0;4、/port.h#ifndef PORT_H_#define PORT_H_#include #include using namespace std;class Portprivate:char *brand;char style20;int bottles;public:Port(const char *br = none, const char *st = none, int b = 0);Port(const Port &p);virtual Port() deletebrand; 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 Portprivate:char *nickname;int year;public:VintagePort();VintagePort(const char *br, const char *st, int b, const char *nn, int y);VintagePort(const VintagePort &vp);VintagePort() deletenickname; VintagePort &operator=(const VintagePort &vp);void Show()const;friend ostream &operator(ostream &os, const VintagePort &vp);#endif/port.cpp#include port.hPort:Port(const char *br, const char *st, int b)brand = new charstrlen(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 charstrlen(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;deletebrand;brand = new charstrlen(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;return *this;Port &Port:operator-=(int b)bottles -= b;return *this;void Port:Show()constcout 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()nickname = new char1;nickname0 = 0;year = 0;VintagePort:VintagePort(const char *br, const char *st, int b, const char *nn, int y) :Port(br, st, b)nickname = new charstrlen(nn) + 1;strcpy_s(nickname, strlen(nn) + 1, nn);year = y;VintagePort:VintagePort(const VintagePort &vp) :Port(vp)nickname = new charstrlen(vp.nickname) + 1;strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);year = vp.year;VintagePort &Vintage

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论