p475.2

  1. //头文件:
  2. class Cd{
  3. private:
  4. char *performers;
  5. char *label;
  6. int selections;
  7. double playtime;
  8. public:
  9. Cd(char *s1, char *s2, int n, double x);
  10. Cd(const Cd & st);
  11. Cd();
  12. virtual ~Cd();
  13. virtual void Report()const;
  14. Cd & operator = (const Cd & st);
  15. };
  16.  
  17. class Classic : public Cd
  18. {
  19. private:
  20. char *production;
  21. public:
  22. Classic(char *s1 = "nullbody1", char *s2 = "nullbody2", char *s3 = "nullbody3", int n = , double x = );
  23. Classic(const Classic & st);
  24. virtual void Report()const;
  25. virtual ~Classic();
  26. Classic & operator=(const Classic & st);
  27. };
  28.  
  29. //方法:
  30. #include<iostream>
  31. #include<cstring>
  32. #include"classic.h"
  33.  
  34. using std::cout;
  35. using std::endl;
  36.  
  37. Cd::Cd(char *s1, char *s2, int n, double x){
  38. performers = new char[strlen(s1) + ];
  39. strcpy(performers, s1);
  40. label = new char[strlen(s2) + ];
  41. strcpy(label, s2);
  42. selections = n;
  43. playtime = x;
  44. }
  45.  
  46. Cd::Cd(const Cd & st){
  47. performers = new char[strlen(st.performers) + ];
  48. strcpy(performers, st.performers);
  49. label = new char[strlen(st.performers) + ];
  50. strcpy(label, st.label);
  51. selections = st.selections;
  52. playtime = st.playtime;
  53. }
  54.  
  55. Cd::Cd(){
  56. performers = NULL;
  57. label = NULL;
  58. selections = ;
  59. playtime = ;
  60. }
  61.  
  62. void Cd::Report()const{
  63. cout << "performers: " << performers << endl
  64. << "label: " << label << endl
  65. << "selections: " << selections << endl
  66. << "playtime: " << playtime << endl;
  67. }
  68.  
  69. Cd & Cd::operator = (const Cd & st){
  70. if (this == &st)
  71. return *this;
  72. performers = new char[strlen(st.performers) + ];
  73. strcpy(performers, st.performers);
  74. label = new char[strlen(st.performers) + ];
  75. strcpy(label, st.label);
  76. selections = st.selections;
  77. playtime = st.playtime;
  78. return *this;
  79. }
  80.  
  81. Classic::Classic(char *s1, char *s2, char *s3, int n, double x): Cd(s2, s3, n, x){
  82. production = new char[strlen(s1) + ];
  83. strcpy(production, s1);
  84. }
  85.  
  86. Classic::Classic(const Classic & st): Cd(st){
  87. production = new char[strlen(st.production) + ];
  88. strcpy(production, st.production);
  89. }
  90.  
  91. Cd::~Cd(){
  92. delete[]performers;
  93. delete[]label;
  94. }
  95.  
  96. Classic::~Classic(){
  97. delete[]production;
  98. }
  99.  
  100. void Classic::Report()const{
  101. cout << "production: " << production << endl;
  102. Cd::Report();
  103. }
  104.  
  105. Classic & Classic::operator=(const Classic & st){
  106. if (this == &st)
  107. return *this;
  108. Cd::operator=(st);
  109. production = new char[strlen(st.production) + ];
  110. strcpy(production, st.production);
  111. return *this;
  112. }
  113.  
  114. //驱动:
  115. #include<iostream>
  116. #include<cstdlib>
  117. using namespace std;
  118. #include"classic.h"
  119. void Bravo(const Cd & disk);
  120.  
  121. int main(){
  122. Cd c1("Beatles", "Capitol", , 35.5);
  123. Classic c2 = Classic("piano sonata in B flat", "Alfred Brendel", "Philips", , 57.17);
  124. Cd *pcd = &c1;
  125.  
  126. cout << "using object directly\n";
  127. c1.Report();
  128. c2.Report();
  129.  
  130. cout << "using type cd *pointer to objects:\n";
  131. pcd->Report();
  132. pcd = &c2;
  133. pcd->Report();
  134.  
  135. cout << "calling a function with a Cd reference argument:\n";
  136. Bravo(c1);
  137. Bravo(c2);
  138. cout << "testing assignment: ";
  139. Classic copy;
  140. copy = c2;
  141. copy.Report();
  142.  
  143. system("pause");
  144. return ;
  145. }
  146.  
  147. void Bravo(const Cd & disk){
  148. disk.Report();
  149. }

p475.3

  1. //头文件:
  2. #include<iostream>
  3.  
  4. #ifndef BASE_H_
  5. #define BASE_H_
  6. class baseABC
  7. {
  8. private:
  9. char *label;
  10. int rating;
  11. public:
  12. baseABC(const char *s1="null", int r = );
  13. baseABC(const baseABC &st);
  14. virtual ~baseABC() = ;
  15. baseABC & operator=(const baseABC &rs);
  16. friend std::ostream & operator<<(std::ostream & os, const baseABC & rs);
  17. };
  18.  
  19. class baseDMA : public baseABC
  20. {
  21. public:
  22. baseDMA(const char *s1="null", int r = );
  23. baseDMA(const baseDMA &st);
  24. baseDMA & operator=(const baseDMA &rs);
  25. friend std::ostream & operator<<(std::ostream & os, const baseDMA & rs);
  26. };
  27.  
  28. class lacksDMA : public baseABC
  29. {
  30. private:
  31. enum{COL_LEN=};
  32. char color[COL_LEN];
  33. public:
  34. lacksDMA(const char *pt = "blank", const char *ct = "null", int r = );
  35. lacksDMA(const char *pt, const baseABC & st);
  36. friend std::ostream & operator<<(std::ostream & os, const lacksDMA & st);
  37. };
  38.  
  39. class hasDMA : public baseABC
  40. {
  41. private:
  42. char *style;
  43. public:
  44. hasDMA(const char *pt = "none", const char *ct = "null", int r = );
  45. hasDMA(const char *ct, const baseABC &st);
  46. hasDMA(const hasDMA &st);
  47. virtual ~hasDMA();
  48. hasDMA & operator=(const hasDMA & st);
  49. friend std::ostream & operator <<(std::ostream &os, const hasDMA &st);
  50. };
  51.  
  52. #endif
  53.  
  54. //方法:
  55. #include<iostream>
  56. #include<cstring>
  57. #include"classic.h"
  58.  
  59. using std::endl;
  60.  
  61. baseABC::baseABC(const char *s1, int r){
  62. label = new char[strlen(s1) + ];
  63. strcpy(label, s1);
  64. rating = r;
  65. }
  66.  
  67. baseABC::baseABC(const baseABC &st){
  68. label = new char[strlen(st.label) + ];
  69. strcpy(label, st.label);
  70. rating = st.rating;
  71. }
  72.  
  73. baseABC::~baseABC(){
  74. delete[]label;
  75. }
  76.  
  77. baseABC & baseABC::operator=(const baseABC &rs){
  78. if (this == &rs)
  79. return *this;
  80. label = new char[strlen(rs.label) + ];
  81. strcpy(label, rs.label);
  82. rating = rs.rating;
  83. return *this;
  84. }
  85.  
  86. std::ostream & operator<<(std::ostream & os, const baseABC & rs){
  87. os << "label: " << rs.label << endl
  88. << "rating: " << rs.rating << endl;
  89. return os;
  90. }
  91.  
  92. baseDMA::baseDMA(const char *s1, int r): baseABC(s1, r){
  93. }
  94.  
  95. baseDMA::baseDMA(const baseDMA &st): baseABC(st){
  96. }
  97.  
  98. baseDMA & baseDMA::operator=(const baseDMA &rs){
  99. if (this == &rs)
  100. return *this;
  101. baseABC::operator=(rs);
  102. return *this;
  103. }
  104.  
  105. std::ostream & operator<<(std::ostream & os, const baseDMA & rs){
  106. os << (const baseABC &)rs;
  107. return os;
  108. }
  109.  
  110. lacksDMA::lacksDMA(const char *pt, const char *ct, int r): baseABC(ct, r){
  111. strncpy(color, pt, COL_LEN);
  112. color[COL_LEN - ] = '\0';
  113. }
  114.  
  115. lacksDMA::lacksDMA(const char *pt, const baseABC & st): baseABC(st){
  116. strncpy(color, pt, COL_LEN);
  117. color[COL_LEN - ] = '\0';
  118. }
  119.  
  120. std::ostream & operator<<(std::ostream & os, const lacksDMA & st){
  121. os << (const baseABC &)st<<endl
  122. <<"color: "<<st.color;
  123. return os;
  124. }
  125.  
  126. hasDMA::hasDMA(const char *pt, const char *ct, int r): baseABC(ct, r){
  127. style = new char[strlen(pt) + ];
  128. strcpy(style, pt);
  129. }
  130.  
  131. hasDMA::hasDMA(const char *ct, const baseABC &st): baseABC(st){
  132. style = new char[strlen(ct) + ];
  133. strcpy(style, ct);
  134. }
  135.  
  136. hasDMA::hasDMA(const hasDMA &st): baseABC(st){
  137. style = new char[strlen(st.style) + ];
  138. strcpy(style, st.style);
  139. }
  140.  
  141. hasDMA::~hasDMA(){
  142. delete[]style;
  143. }
  144.  
  145. hasDMA & hasDMA::operator=(const hasDMA & st){
  146. if (this == &st)
  147. return *this;
  148. delete[]style;
  149. baseABC::operator=(st);
  150. style = new char[strlen(st.style) + ];
  151. strcpy(style, st.style);
  152. return *this;
  153. }
  154.  
  155. std::ostream & operator <<(std::ostream &os, const hasDMA &st){
  156. os << (const baseABC &)st<<endl
  157. <<"style: "<< st.style << endl;
  158. return os;
  159. }
  160.  
  161. //驱动:
  162. #include<iostream>
  163. #include<cstdlib>
  164. #include"classic.h"
  165.  
  166. const int CLIENTS = ;
  167. const int LEN = ;
  168.  
  169. int main(){
  170. using namespace std;
  171. int i;
  172. baseABC *pt[CLIENTS];
  173.  
  174. for (i = ; i < CLIENTS; i++){
  175. char temp[LEN];
  176. cout << "enter the label\n";
  177. cin.get(temp, LEN).get();
  178. int r;
  179. cout << "enter the rating\n";
  180. cin >> r;
  181. while (cin.get() != '\n')
  182. continue;
  183. char kind;
  184. cout << "enter 1/2/3 to choice a model to fit baseDMA/lacksDMA/hasDMA\n";
  185. cin >> kind;
  186. while (cin.get() != '\n')
  187. continue;
  188. if (kind == '')
  189. pt[i] = new baseDMA(temp, r);
  190. else if (kind == ''){
  191. char ar[];
  192. cout << "enter a color\n";
  193. cin.get(ar, ).get();
  194. baseDMA test1(temp, r);
  195. pt[i] = new lacksDMA(ar, test1);
  196. }
  197. else if (kind == ''){
  198. char ptr[];
  199. cout << "enter a style\n";
  200. cin.get(ptr, ).get();
  201. pt[i] = new hasDMA(ptr, temp, r);
  202. }
  203. }
  204. for (i = ; i < CLIENTS; i++)
  205. cout << *pt[i] << endl;
  206.  
  207. system("pause");
  208. return ;
  209. }

c++ primer plus 习题答案(8)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(7)

    p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...

  3. c++ primer plus 习题答案(6)

    p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  4. c++ primer plus 习题答案(5)

    p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...

  6. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  7. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. shell 学习笔记1501-1800

    .巧用bash的{}扩展备份目录: cp file.txt{,.bak} .利用at执行一次性命令: echo "ls -l" | at midnight #Execute a c ...

  2. CSV 客座文章系列: Pruffi 通过 Windows Azure 挖掘社交媒体的强大招聘潜能

    编辑人员注释:今天这篇文章由 Pruffi 创始人 Alena Vladimirskaya 和 Pruffi 的 CTO Alexander Ivanov 联合撰写,介绍了该公司如何使用 Window ...

  3. poj1799---解析几何

    sin(a)=r/R-r,反三角asin(r/R-r),乘以2n=2pi,去化简,得到r 收获:define pi acos(-1) 这样pi的精度会高很多<math.h>(cos,sin ...

  4. #include <algorithm>

    1 adjacent_find 查找重复的元素 2 find_if 查找符合条件的第一个元素 3 find_if_not 查找不符合条件的第一个元素 4 for_each 可以遍历每一个元素 5 pa ...

  5. Oracle基础(二)---操作命令

    接上篇博客介绍Oracle基本概要.以下将介绍数据库的操作指令. Sql*plus经常使用命令 连接命令 1. conn[ect] 使用方法 connusername/password@网路服务名[a ...

  6. Spring MVC详细示例实战教程【转】

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  7. NSURLConnect 的简单实用(iOS8淘汰)

    Demo_1 NSRULConnection NSRULConnection 苹果公司在ios8已经抛弃了,但是我还是要讲一下,因为这和后面的NSSession有着密切的联系 下面开始使用步骤: 1. ...

  8. WPF事件,路由事件

    直接事件模型或CLR事件模型 1事件拥有者 2事件响应者 3事件订阅关系 例如 Window窗口中的控件Button 事件:拥有者Button 事件:Button.Click 事件响应者:Window ...

  9. JSP page include taglib

    page include taglib 语法:<%@ 指令名称 属性=值 属性=值 -%> ------------------- page 1.language 默认值java 2.ex ...

  10. javascript--时钟

    <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" ...