c++ primer plus 习题答案(8)
p475.2
- //头文件:
- class Cd{
- private:
- char *performers;
- char *label;
- int selections;
- double playtime;
- public:
- Cd(char *s1, char *s2, int n, double x);
- Cd(const Cd & st);
- Cd();
- virtual ~Cd();
- virtual void Report()const;
- Cd & operator = (const Cd & st);
- };
- class Classic : public Cd
- {
- private:
- char *production;
- public:
- Classic(char *s1 = "nullbody1", char *s2 = "nullbody2", char *s3 = "nullbody3", int n = , double x = );
- Classic(const Classic & st);
- virtual void Report()const;
- virtual ~Classic();
- Classic & operator=(const Classic & st);
- };
- //方法:
- #include<iostream>
- #include<cstring>
- #include"classic.h"
- using std::cout;
- using std::endl;
- Cd::Cd(char *s1, char *s2, int n, double x){
- performers = new char[strlen(s1) + ];
- strcpy(performers, s1);
- label = new char[strlen(s2) + ];
- strcpy(label, s2);
- selections = n;
- playtime = x;
- }
- Cd::Cd(const Cd & st){
- performers = new char[strlen(st.performers) + ];
- strcpy(performers, st.performers);
- label = new char[strlen(st.performers) + ];
- strcpy(label, st.label);
- selections = st.selections;
- playtime = st.playtime;
- }
- Cd::Cd(){
- performers = NULL;
- label = NULL;
- selections = ;
- playtime = ;
- }
- void Cd::Report()const{
- cout << "performers: " << performers << endl
- << "label: " << label << endl
- << "selections: " << selections << endl
- << "playtime: " << playtime << endl;
- }
- Cd & Cd::operator = (const Cd & st){
- if (this == &st)
- return *this;
- performers = new char[strlen(st.performers) + ];
- strcpy(performers, st.performers);
- label = new char[strlen(st.performers) + ];
- strcpy(label, st.label);
- selections = st.selections;
- playtime = st.playtime;
- return *this;
- }
- Classic::Classic(char *s1, char *s2, char *s3, int n, double x): Cd(s2, s3, n, x){
- production = new char[strlen(s1) + ];
- strcpy(production, s1);
- }
- Classic::Classic(const Classic & st): Cd(st){
- production = new char[strlen(st.production) + ];
- strcpy(production, st.production);
- }
- Cd::~Cd(){
- delete[]performers;
- delete[]label;
- }
- Classic::~Classic(){
- delete[]production;
- }
- void Classic::Report()const{
- cout << "production: " << production << endl;
- Cd::Report();
- }
- Classic & Classic::operator=(const Classic & st){
- if (this == &st)
- return *this;
- Cd::operator=(st);
- production = new char[strlen(st.production) + ];
- strcpy(production, st.production);
- return *this;
- }
- //驱动:
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- #include"classic.h"
- void Bravo(const Cd & disk);
- int main(){
- Cd c1("Beatles", "Capitol", , 35.5);
- Classic c2 = Classic("piano sonata in B flat", "Alfred Brendel", "Philips", , 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 ;
- }
- void Bravo(const Cd & disk){
- disk.Report();
- }
p475.3
- //头文件:
- #include<iostream>
- #ifndef BASE_H_
- #define BASE_H_
- class baseABC
- {
- private:
- char *label;
- int rating;
- public:
- baseABC(const char *s1="null", int r = );
- baseABC(const baseABC &st);
- virtual ~baseABC() = ;
- baseABC & operator=(const baseABC &rs);
- friend std::ostream & operator<<(std::ostream & os, const baseABC & rs);
- };
- class baseDMA : public baseABC
- {
- public:
- baseDMA(const char *s1="null", int r = );
- baseDMA(const baseDMA &st);
- baseDMA & operator=(const baseDMA &rs);
- friend std::ostream & operator<<(std::ostream & os, const baseDMA & rs);
- };
- class lacksDMA : public baseABC
- {
- private:
- enum{COL_LEN=};
- char color[COL_LEN];
- public:
- lacksDMA(const char *pt = "blank", const char *ct = "null", int r = );
- lacksDMA(const char *pt, const baseABC & st);
- friend std::ostream & operator<<(std::ostream & os, const lacksDMA & st);
- };
- class hasDMA : public baseABC
- {
- private:
- char *style;
- public:
- hasDMA(const char *pt = "none", const char *ct = "null", int r = );
- hasDMA(const char *ct, const baseABC &st);
- hasDMA(const hasDMA &st);
- virtual ~hasDMA();
- hasDMA & operator=(const hasDMA & st);
- friend std::ostream & operator <<(std::ostream &os, const hasDMA &st);
- };
- #endif
- //方法:
- #include<iostream>
- #include<cstring>
- #include"classic.h"
- using std::endl;
- baseABC::baseABC(const char *s1, int r){
- label = new char[strlen(s1) + ];
- strcpy(label, s1);
- rating = r;
- }
- baseABC::baseABC(const baseABC &st){
- label = new char[strlen(st.label) + ];
- strcpy(label, st.label);
- rating = st.rating;
- }
- baseABC::~baseABC(){
- delete[]label;
- }
- baseABC & baseABC::operator=(const baseABC &rs){
- if (this == &rs)
- return *this;
- label = new char[strlen(rs.label) + ];
- strcpy(label, rs.label);
- rating = rs.rating;
- return *this;
- }
- std::ostream & operator<<(std::ostream & os, const baseABC & rs){
- os << "label: " << rs.label << endl
- << "rating: " << rs.rating << endl;
- return os;
- }
- baseDMA::baseDMA(const char *s1, int r): baseABC(s1, r){
- }
- baseDMA::baseDMA(const baseDMA &st): baseABC(st){
- }
- baseDMA & baseDMA::operator=(const baseDMA &rs){
- if (this == &rs)
- return *this;
- baseABC::operator=(rs);
- return *this;
- }
- std::ostream & operator<<(std::ostream & os, const baseDMA & rs){
- os << (const baseABC &)rs;
- return os;
- }
- lacksDMA::lacksDMA(const char *pt, const char *ct, int r): baseABC(ct, r){
- strncpy(color, pt, COL_LEN);
- color[COL_LEN - ] = '\0';
- }
- lacksDMA::lacksDMA(const char *pt, const baseABC & st): baseABC(st){
- strncpy(color, pt, COL_LEN);
- color[COL_LEN - ] = '\0';
- }
- std::ostream & operator<<(std::ostream & os, const lacksDMA & st){
- os << (const baseABC &)st<<endl
- <<"color: "<<st.color;
- return os;
- }
- hasDMA::hasDMA(const char *pt, const char *ct, int r): baseABC(ct, r){
- style = new char[strlen(pt) + ];
- strcpy(style, pt);
- }
- hasDMA::hasDMA(const char *ct, const baseABC &st): baseABC(st){
- style = new char[strlen(ct) + ];
- strcpy(style, ct);
- }
- hasDMA::hasDMA(const hasDMA &st): baseABC(st){
- style = new char[strlen(st.style) + ];
- strcpy(style, st.style);
- }
- hasDMA::~hasDMA(){
- delete[]style;
- }
- hasDMA & hasDMA::operator=(const hasDMA & st){
- if (this == &st)
- return *this;
- delete[]style;
- baseABC::operator=(st);
- style = new char[strlen(st.style) + ];
- strcpy(style, st.style);
- return *this;
- }
- std::ostream & operator <<(std::ostream &os, const hasDMA &st){
- os << (const baseABC &)st<<endl
- <<"style: "<< st.style << endl;
- return os;
- }
- //驱动:
- #include<iostream>
- #include<cstdlib>
- #include"classic.h"
- const int CLIENTS = ;
- const int LEN = ;
- int main(){
- using namespace std;
- int i;
- baseABC *pt[CLIENTS];
- for (i = ; i < CLIENTS; i++){
- char temp[LEN];
- cout << "enter the label\n";
- cin.get(temp, LEN).get();
- int r;
- cout << "enter the rating\n";
- cin >> r;
- while (cin.get() != '\n')
- continue;
- char kind;
- cout << "enter 1/2/3 to choice a model to fit baseDMA/lacksDMA/hasDMA\n";
- cin >> kind;
- while (cin.get() != '\n')
- continue;
- if (kind == '')
- pt[i] = new baseDMA(temp, r);
- else if (kind == ''){
- char ar[];
- cout << "enter a color\n";
- cin.get(ar, ).get();
- baseDMA test1(temp, r);
- pt[i] = new lacksDMA(ar, test1);
- }
- else if (kind == ''){
- char ptr[];
- cout << "enter a style\n";
- cin.get(ptr, ).get();
- pt[i] = new hasDMA(ptr, temp, r);
- }
- }
- for (i = ; i < CLIENTS; i++)
- cout << *pt[i] << endl;
- system("pause");
- return ;
- }
c++ primer plus 习题答案(8)的更多相关文章
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- c++ primer plus 习题答案(7)
p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...
- c++ primer plus 习题答案(6)
p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(5)
p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(4)
p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...
- c++ primer plus 习题答案(3)
p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...
- c++ primer plus 习题答案(2)
p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- shell 学习笔记1501-1800
.巧用bash的{}扩展备份目录: cp file.txt{,.bak} .利用at执行一次性命令: echo "ls -l" | at midnight #Execute a c ...
- CSV 客座文章系列: Pruffi 通过 Windows Azure 挖掘社交媒体的强大招聘潜能
编辑人员注释:今天这篇文章由 Pruffi 创始人 Alena Vladimirskaya 和 Pruffi 的 CTO Alexander Ivanov 联合撰写,介绍了该公司如何使用 Window ...
- poj1799---解析几何
sin(a)=r/R-r,反三角asin(r/R-r),乘以2n=2pi,去化简,得到r 收获:define pi acos(-1) 这样pi的精度会高很多<math.h>(cos,sin ...
- #include <algorithm>
1 adjacent_find 查找重复的元素 2 find_if 查找符合条件的第一个元素 3 find_if_not 查找不符合条件的第一个元素 4 for_each 可以遍历每一个元素 5 pa ...
- Oracle基础(二)---操作命令
接上篇博客介绍Oracle基本概要.以下将介绍数据库的操作指令. Sql*plus经常使用命令 连接命令 1. conn[ect] 使用方法 connusername/password@网路服务名[a ...
- Spring MVC详细示例实战教程【转】
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- NSURLConnect 的简单实用(iOS8淘汰)
Demo_1 NSRULConnection NSRULConnection 苹果公司在ios8已经抛弃了,但是我还是要讲一下,因为这和后面的NSSession有着密切的联系 下面开始使用步骤: 1. ...
- WPF事件,路由事件
直接事件模型或CLR事件模型 1事件拥有者 2事件响应者 3事件订阅关系 例如 Window窗口中的控件Button 事件:拥有者Button 事件:Button.Click 事件响应者:Window ...
- JSP page include taglib
page include taglib 语法:<%@ 指令名称 属性=值 属性=值 -%> ------------------- page 1.language 默认值java 2.ex ...
- javascript--时钟
<head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" ...