十四周任务一

/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:score.cpp                           
* 作    者:   刘元龙                         
* 完成日期: 2013 年5 月 31  日
* 版本号: v1.0      
* 输入描述:
* 问题描述:
* 输出:
*/
#include<iostream>
#include<string>
using namespace std;
class animal
{
public:
  virtual void cry()
    {
      cout<<"不知哪种动物,让我如何学叫?"<<endl;
    }
protected:
    string name;
};

//定义老鼠类
class mouse :public  animal
{
  public:
      mouse(string,int);
      virtual void cry();
  protected:
      int sex;
};
mouse::mouse(string b,int a)
{
    name=b;
    sex=a;
}
 void mouse::cry()
{
    cout<<"我叫"<<name<<",是一只";
    if(sex==1)
       cout<<"男老鼠,";
    else
        cout<<"女老鼠,";

    cout<<"我的叫声是:吱吱吱!";
    cout<<endl;
}

//定义猫类
class cat :public  animal
{
  public:
      virtual void cry();
      cat(string);
};
cat::cat(string b)
{
    name=b;
}
void cat::cry()
{
    cout<<"我叫"<<name<<",是一只猫,我的叫声是:喵喵喵!";
    cout<<endl;
}

//定义狗类
class dog :public  animal
{
  public:
      virtual void cry();
      dog(string);
};

dog::dog(string b)
{
    name=b;
}
void dog::cry()
{
    cout<<"我叫"<<name<<",是一只狗,我的叫声是:旺旺旺!";
    cout<<endl;
}


//定义长颈鹿类
class giraffe :public  animal
{
  public:
      giraffe(string,int);
      virtual void cry();
  protected:
      int sex;
};

giraffe::giraffe(string b,int a)
{
    name=b;
    sex=a;
}
void giraffe::cry()
{
    cout<<"我叫"<<name<<",是一只";
    if(sex==1)
        cout<<"男长颈鹿,";
    else
        cout<<"女长颈鹿,";

    cout<<"我脖子太长,发不出声音!";
    cout<<endl;
}

int main( )
{
    animal *p;
    p = new animal();
    p->cry();
    mouse m1("Jerry",1);
    p=&m1;
    p->cry();
    mouse m2("Jemmy",2);
    p=&m2;
    p->cry();
    cat c1("Tom");
    p=&c1;
    p->cry();
    dog d1("Droopy");
    p=&d1;
    p->cry();
    giraffe g1("Gill",1);
    p=&g1;
    p->cry();
    return 0;
}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值