/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 6 月 2 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:
* 程序输出:
*/
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal(string nam):name(nam){}
virtual void cry()=0;
protected:
string name;
};
class Cat:public Animal
{
public:
Cat(string nam):Animal(nam){}
virtual void cry()
{
cout<<"我叫"<<name<<"我是一只猫,我的叫声是:喵喵喵!"<<endl;
}
};
class Dog:public Animal
{
public:
Dog(string nam):Animal(nam){}
virtual void cry()
{
cout<<"我叫"<<name<<"我是一只狗,我的叫声是:汪汪汪!"<<endl;
}
};
class Mouse:public Animal
{
public:
Mouse(string nam,char s):Animal(nam),sex(s){}
void cry()
{
cout<<"我叫"<<name<<"我是一只";
if(sex=='f')
cout<<"女";
else
cout<<"男";
cout<<"老鼠,我的叫声是:吱吱吱!"<<endl;
}
protected:
char sex;
};
class Giraffe:public Animal
{
public:
Giraffe(string nam,char s):Animal(nam),sex(s){}
void cry()
{
cout<<"我叫"<<name<<"我是一只";
if(sex=='f')
cout<<"女";
else
cout<<"男";
cout<<"长颈鹿,我的脖子太长,发不出声音来!"<<endl;
}
protected:
char sex;
};
int main( )
{
Animal *p;
//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;
}
运行结果: