第十三周 任务二

Bertha 。 2022-06-12 09:47 620阅读 0赞
  1. /* (程序头部注释开始)
  2. * 程序的版权和版本声明部分
  3. * Copyright (c) 2012, 烟台大学计算机学院学生
  4. * All rights reserved.
  5. * 文件名称: 抽象类
  6. * 作 者: 薛广晨
  7. * 完成日期: 2012 年 5 月 11 日
  8. * 版 本 号: x1.0
  9. * 对任务及求解方法的描述部分
  10. * 输入描述: 使用抽象类
  11. * 问题描述: 【任务2】下面给出了基类Animal和main()函数。
  12. (任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。
  13. (任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p = new Animal();将出错,将此行删除。
  14. (任务2.3)每一个Animal的派生类都有一个“名字”数据成员,改造上面的程序,将这一数据成员作为抽象类Animal数据成员被各派生类使用。
  15. * 程序输出: ......
  16. * 程序头部的注释结束
  17. */
  18. //(任务2.1)根据main()函数给出的注释提示,设计出相关的各个类。
  19. #include "iostream"
  20. #include<string>
  21. using namespace std;
  22. class Animal
  23. {
  24. public:
  25. virtual void cry() {cout << "不知哪种动物,让我如何学叫?" << endl;}
  26. };
  27. class Mouse : public Animal
  28. {
  29. public:
  30. Mouse(string name)
  31. {
  32. this->name = name;
  33. }
  34. virtual void cry();
  35. private:
  36. string name;
  37. };
  38. class Cat : public Animal
  39. {
  40. public:
  41. Cat(string name)
  42. {
  43. this->name = name;
  44. }
  45. virtual void cry();
  46. private:
  47. string name;
  48. };
  49. class Dog : public Animal
  50. {
  51. public:
  52. Dog(string name)
  53. {
  54. this->name = name;
  55. }
  56. virtual void cry();
  57. private:
  58. string name;
  59. };
  60. class Giraffe : public Animal
  61. {
  62. public:
  63. Giraffe(string name)
  64. {
  65. this->name = name;
  66. }
  67. virtual void cry();
  68. private:
  69. string name;
  70. };
  71. void Mouse::cry()
  72. {
  73. cout << "我叫" << name << ",是一只老鼠,我的叫声是:吱吱吱!" << endl;
  74. }
  75. void Cat::cry()
  76. {
  77. cout << "我叫" << name << ",是一只猫,我的叫声是:喵喵喵!" << endl;
  78. }
  79. void Dog::cry()
  80. {
  81. cout << "我叫" << name << ",是一条狗,我的叫声是:汪汪汪!" << endl;
  82. }
  83. void Giraffe::cry()
  84. {
  85. cout << "我叫" << name << ",是长颈鹿,脖子太长,发不出声音来!" << endl;
  86. }
  87. int main( )
  88. {
  89. Animal *p;
  90. p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处)
  91. Mouse m("Jerry"); p = &m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!
  92. Cat c("Tom"); p = &c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!
  93. Dog d("Droopy"); p = &d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!
  94. Giraffe g("Gill"); p = &g; p->cry(); //输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来!
  95. system("pause");
  96. return 0;
  97. }
  98. /*(任务2.2)显然,Animal设计为抽象类更合适,Animal不需要能够实例化,是专门作基类使用的。改造程序,使Animal设计为抽象类,这时main()函数中p = new Animal();将出错,将此行删除。*/
  99. #include "iostream"
  100. #include<string>
  101. using namespace std;
  102. class Animal
  103. {
  104. public:
  105. virtual void cry() const = 0;//{cout << "不知哪种动物,让我如何学叫?" << endl;}
  106. };
  107. class Mouse : public Animal
  108. {
  109. public:
  110. Mouse(string name)
  111. {
  112. this->name = name;
  113. }
  114. virtual void cry() const;
  115. private:
  116. string name;
  117. };
  118. class Cat : public Animal
  119. {
  120. public:
  121. Cat(string name)
  122. {
  123. this->name = name;
  124. }
  125. virtual void cry() const;
  126. private:
  127. string name;
  128. };
  129. class Dog : public Animal
  130. {
  131. public:
  132. Dog(string name)
  133. {
  134. this->name = name;
  135. }
  136. virtual void cry() const;
  137. private:
  138. string name;
  139. };
  140. class Giraffe : public Animal
  141. {
  142. public:
  143. Giraffe(string name)
  144. {
  145. this->name = name;
  146. }
  147. virtual void cry() const;
  148. private:
  149. string name;
  150. };
  151. void Mouse::cry() const
  152. {
  153. cout << "我叫" << name << ",是一只老鼠,我的叫声是:吱吱吱!" << endl;
  154. }
  155. void Cat::cry() const
  156. {
  157. cout << "我叫" << name << ",是一只猫,我的叫声是:喵喵喵!" << endl;
  158. }
  159. void Dog::cry() const
  160. {
  161. cout << "我叫" << name << ",是一条狗,我的叫声是:汪汪汪!" << endl;
  162. }
  163. void Giraffe::cry() const
  164. {
  165. cout << "我叫" << name << ",是长颈鹿,脖子太长,发不出声音来!" << endl;
  166. }
  167. int main( )
  168. {
  169. Animal *p;
  170. //p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处)
  171. Mouse m("Jerry"); p = &m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!
  172. Cat c("Tom"); p = &c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!
  173. Dog d("Droopy"); p = &d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!
  174. Giraffe g("Gill"); p = &g; p->cry(); //输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来!
  175. system("pause");
  176. return 0;
  177. }
  178. /*(任务2.3)每一个Animal的派生类都有一个“名字”数据成员,改造上面的程序,将这一数据成员作为抽象类Animal数据成员被各派生类使用。*/
  179. #include "iostream"
  180. #include<string>
  181. using namespace std;
  182. class Animal
  183. {
  184. public:
  185. virtual void cry() const = 0;//{cout << "不知哪种动物,让我如何学叫?" << endl;}
  186. Animal(string name);
  187. protected:
  188. string name;
  189. };
  190. Animal::Animal(string name)
  191. {
  192. this->name = name;
  193. }
  194. class Mouse : public Animal
  195. {
  196. public:
  197. Mouse(string name):Animal(name){}
  198. virtual void cry() const;
  199. };
  200. class Cat : public Animal
  201. {
  202. public:
  203. Cat(string name):Animal(name){}
  204. virtual void cry() const;
  205. };
  206. class Dog : public Animal
  207. {
  208. public:
  209. Dog(string name):Animal(name){}
  210. virtual void cry() const;
  211. };
  212. class Giraffe : public Animal
  213. {
  214. public:
  215. Giraffe(string name):Animal(name){}
  216. virtual void cry() const;
  217. };
  218. void Mouse::cry() const
  219. {
  220. cout << "我叫" << name << ",是一只老鼠,我的叫声是:吱吱吱!" << endl;
  221. }
  222. void Cat::cry() const
  223. {
  224. cout << "我叫" << name << ",是一只猫,我的叫声是:喵喵喵!" << endl;
  225. }
  226. void Dog::cry() const
  227. {
  228. cout << "我叫" << name << ",是一条狗,我的叫声是:汪汪汪!" << endl;
  229. }
  230. void Giraffe::cry() const
  231. {
  232. cout << "我叫" << name << ",是长颈鹿,脖子太长,发不出声音来!" << endl;
  233. }
  234. int main( )
  235. {
  236. Animal *p;
  237. //p = new Animal(); p->cry(); //输出: 不知哪种动物,让我如何学叫?(问题出自此处)
  238. Mouse m("Jerry"); p = &m; p->cry(); //输出: 我叫Jerry,是一只老鼠,我的叫声是:吱吱吱!
  239. Cat c("Tom"); p = &c; p->cry(); //输出: 我叫Tom,是一只猫,我的叫声是:喵喵喵!
  240. Dog d("Droopy"); p = &d; p->cry(); //输出: 我叫Droopy,是一条狗,我的叫声是:汪汪汪!
  241. Giraffe g("Gill"); p = &g; p->cry(); //输出: 我叫Gill,是长颈鹿,脖子太长,发不出声音来!
  242. system("pause");
  243. return 0;
  244. }

1336734666_3562.JPG

发表评论

表情:
评论列表 (有 0 条评论,620人围观)

还没有评论,来说两句吧...

相关阅读

    相关 任务

    实验目的:学会使用循环控制语句解决实际问题 实验内容:编写多分支选择结构程序,根据个人月收入总额,计算出应缴税款和税后收入。 include <iostrea