• Day 6 C++


    1. #include
    2. //不同种类的动物,如狮子、大象、猴子等。现在,动物园里有一位讲解员,他会为每种动物表演做简单的介
    3. //绍。定义一个基类 Animal,其中有一个虛函数perform(),用于在子类中实现不同的表演行为
    4. using namespace std;
    5. class Animal
    6. {
    7. private:
    8. string name;
    9. int weight;
    10. string color;
    11. public:
    12. Animal(){}
    13. Animal(string name,int w,string c):name(name),weight(w),color(c)
    14. {}
    15. virtual void perform()
    16. {
    17. cout << "动物讲解开始......" << endl ;
    18. }
    19. void show()
    20. {
    21. cout << name << " " << weight << " " << color << " " <
    22. }
    23. void over()
    24. {
    25. cout << "动物讲解结束......" << endl ;
    26. }
    27. };
    28. class Lion :public Animal
    29. {
    30. private:
    31. string sound;
    32. int age;
    33. public:
    34. Lion(){}
    35. Lion(string sound,int age,string name,int weight,string color):Animal(name,weight,color),sound(sound),age(age)
    36. {}
    37. void perform()
    38. {
    39. cout << "狮子喷火......." << endl;
    40. }
    41. void show()
    42. {
    43. cout << sound << " " << age << " " ;
    44. }
    45. };
    46. class Elephant :public Animal
    47. {
    48. private:
    49. string nose;
    50. int high;
    51. public:
    52. Elephant(){}
    53. Elephant(string nose,int h,string name,int weight, string color):Animal(name,weight,color),nose(nose),high(h)
    54. {}
    55. void perform()
    56. {
    57. cout << "大象跳圈圈......." << endl;
    58. }
    59. void show()
    60. {
    61. cout << nose << " " << high << " ";
    62. }
    63. };
    64. class Monkey :public Animal
    65. {
    66. private:
    67. string breed;
    68. int birth;
    69. public:
    70. Monkey(){}
    71. Monkey(string breed,int birth,string name,int weight, string color):Animal(name,weight,color),breed(breed),birth(birth)
    72. {}
    73. void perform()
    74. {
    75. cout << "猴子蹦极......." << endl;
    76. }
    77. void show()
    78. {
    79. cout << breed << " "<< birth << " " ;
    80. }
    81. };
    82. int main()
    83. { Animal f;
    84. f.perform();
    85. Animal *p;
    86. Lion A("怒吼",5,"狮子",300,"棕色");
    87. Elephant B("大长鼻子",20,"大象",500,"灰色");
    88. Monkey C("金丝猴",2023,"猴子",36,"金色");
    89. cout << "________________________" << endl;
    90. cout << endl;
    91. A.show();
    92. A.Animal::show();
    93. p=&A;
    94. p->perform();
    95. cout << endl;
    96. cout << endl;
    97. B.show();
    98. B.Animal::show();
    99. p=&B;
    100. p->perform();
    101. cout << endl;
    102. cout << endl;
    103. C.show();
    104. C.Animal::show();
    105. p=&C;
    106. p->perform();
    107. cout << endl;
    108. cout << "________________________" << endl;
    109. f.over();
    110. }

  • 相关阅读:
    TypeScript详解十四:自定义工具类型
    每日一练 bm3 bm2
    知识管理系统中的在线编辑,让共享协作更简便
    加油站的良好出发点
    汇编语言(2)基础知识
    CF33b-B. String Problem
    华为机试 - 面试
    jvm中对象内存空间的分配与回收
    Vue3 源码阅读(12):组件化 —— KeepAlive
    21JVM-类加载和垃圾回收算法
  • 原文地址:https://blog.csdn.net/weixin_42019010/article/details/133798161