/*
* 程序的版权和版本声明部分
* 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;
}
运行结果: