第十三周项目2-动物这样叫

*文件名称:test.cpp 
 *作   者:陈文浩
 *完成日期:2016年6月8日 
 */
问题及代码:
下面是给出的基类Animal声明和main()函数。
class Animal
{
public:
  virtual void cry()
    {
      cout<<"不知哪种动物,让我如何学叫?"<<endl;
    }
};
int main( )
{
    Animal *p;
    p = new Animal();
    p->cry(); 
    Mouse m1("Jerry",'m'); 
    p=&m1;
    p->cry(); 
    Mouse m2("Jemmy",'f');
    p=&m2;
    p->cry(); 
    Cat c1("Tom");
    p=&c1;
    p->cry(); 
    Dog d1("Droopy");
    p=&d1;
    p->cry(); 
    Giraffe g1("Gill",'m');
    p=&g1;
    p->cry(); 
    return 0;
}

1、根据给出的main()函数和运行结果的提示,设计出相关的各个类,注意观察运行结果,提取出每个类中需要的数据成员,并匹配上需要的成员函数。 

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "iostream"  
  2. #include<string>  
  3. using namespace std;  
  4. class Animal  
  5. {  
  6. public:  
  7.     virtual void cry()  
  8.     {  
  9.         cout<<"不知哪种动物,让我如何学叫?"<<endl;  
  10.     }  
  11. };  
  12.   
  13. class Mouse : public Animal  
  14. {  
  15. private:  
  16.     string name;  
  17.     char sex;  
  18. public:  
  19.     Mouse(string nam, char s):name(nam),sex(s) {}  
  20.     virtual void cry()  
  21.     {  
  22.         cout<<"我叫"<<name<<",是一只"<<((sex=='m')?"男":"女")<<"老鼠,我的叫声是:吱吱吱!"<<endl;  
  23.     }  
  24. };  
  25.   
  26. class Cat : public Animal  
  27. {  
  28. private:  
  29.     string name;  
  30. public:  
  31.     Cat(string nam):name(nam) {}  
  32.     virtual void cry()  
  33.     {  
  34.         cout<<"我叫"<<name<<",是一只猫,我的叫声是:喵喵喵!"<<endl;  
  35.     }  
  36. };  
  37.   
  38. class Dog : public Animal  
  39. {  
  40. private:  
  41.     string name;  
  42. public:  
  43.     Dog(string nam):name(nam) {}  
  44.     virtual void cry()  
  45.     {  
  46.         cout<<"我叫"<<name<<",是一条狗,我的叫声是:汪汪汪!"<<endl;  
  47.     }  
  48. };  
  49.   
  50. class Giraffe : public Animal  
  51. {  
  52. private:  
  53.     string name;  
  54.     char sex;  
  55. public:  
  56.     Giraffe(string nam,char s):name(nam), sex(s) {}  
  57.     virtual void cry()  
  58.     {  
  59.         cout<<"我叫"<<name<<",是"<<((sex=='m')?"男":"女")<<"长颈鹿,我的脖子太长,发不出声音来!"<<endl;  
  60.     }  
  61. };  
  62.   
  63. int main( )  
  64. {  
  65.     Animal *p;  
  66.     p = new Animal();  
  67.     p->cry(); //输出: 不知哪种动物,让我如何学叫?  
  68.     Mouse m1("Jerry",'m');  
  69.     p=&m1;  
  70.     p->cry(); //输出: 我叫Jerry,是一只男老鼠,我的叫声是:吱吱吱!  
  71.     Mouse m2("Jemmy",'f');  
  72.     p=&m2;  
  73.     p->cry(); //输出: 我叫Jemmy,是一只女老鼠,我的叫声是:吱吱吱!  
  74.     Cat c1("Tom");  
  75.     p=&c1;  
  76.     p->cry(); //输出: 我叫Tom,是一只猫猫,我的叫声是:喵喵喵!  
  77.     Dog d1("Droopy");  
  78.     p=&d1;  
  79.     p->cry(); //输出: 我叫Droopy,是一条狗狗,我的叫声是:汪汪汪!  
  80.     Giraffe g1("Gill",'m');  
  81.     p=&g1;  
  82.     p->cry(); //输出: 我叫Gill,是男长颈鹿,脖子太长,发不出声音来!  
  83.     return 0;  
  84. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值