/*
*Copyright (c) 2013 ,烟台大学计算机学院
*All rights reserved.
*作者:张凤宁
*完成日期:2014年6月8
*版本号:v1.0
*问题描述:
*样例输入:
*样例输出:
*问题分析:用简单的方法,学会活学活用
*/
#include <iostream>
#include <cstring>
using namespace std;
class Animal
{
public:
virtual void cry()
{
cout<<"不知哪种动物,让我如何学叫?"<<endl;
}
};
//老鼠
class Mouse:public Animal
{
public:
Mouse(string nam,char s):name(nam),sex(s) {};
void cry();
private:
string name;
char sex;
};
void Mouse::cry()
{
cout<<"我叫"<<name<<",是一只"<<((sex=='m')?"男":"女")<<"老鼠,"<<"我的叫声是:吱吱吱!"<<endl;
}
//猫
class Cat:public Animal
{
public:
Cat(string nam):name(nam) {};
void cry();
private:
string name;
};
void Cat::cry()
{
cout<<"我叫"<<name<<",是一只猫,"<<"我的叫声是:喵喵喵!"<<endl;
}
//狗
class Dog:public Animal
{
public:
Dog(string nam):name(nam) {};
void cry();
private:
string name;
};
void Dog::cry()
{
cout<<"我叫"<<name<<",是一条狗,"<<"我的叫声是:汪汪汪!"<<endl;
}
//长颈鹿
class Giraffe:public Animal
{
public:
Giraffe(string nam,char s):name(nam),sex(s) {};
void cry();
private:
string name;
char sex;
};
void Giraffe::cry()
{
cout<<"我叫"<<name<<",是"<<((sex=='m')?"男":"女")<<"长颈鹿,我的脖子太长,发不出声音来!"<<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;
}
运行结果:
<img src="https://img-blog.csdn.net/20140608193352281?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM2OTc1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />