/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved
* 文件名称:score.cpp
* 作 者:赵兰
* 完成日期: 2013年 6月11 日
* 版本号: v1.0
* 输入描述:略
* 问题描述:根据给出的基类Animal和main()函数
* 输出:如下
*/
// aaa.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include<string>
using namespace std;
class Animal //声明抽象基类Animal
{
public:
virtual void cry()const=0; //纯虚函数
};
class Mouse:public Animal //Mouse是Animal的公用派生类
{
public:
Mouse(string n,char s):name(n),sex(s){}//定义一个有参的构造函数,用参数的初始化表对数据成员初始化
void AnimalName(){cout<<"Mouse ";}
void cry()const{cout<<"我是小老鼠我的名字叫"<<name<<"我的性别是"<<sex<<"我的叫声是吱吱吱"<<endl;}
protected:
string name;
char sex;
};
class Cat:public Animal //Cat是Animal的公用派生类
{
public:
Cat(string n):name(n){}
void AnimalName(){cout<<"Cat ";}
void cry()const{cout<<"我是小猫我的名字叫"<<name<<"我的叫声是“喵喵喵”";}
protected:
string name;
};
class Dog:public Animal
{
public:
Dog(string n):name(n){}
void AnimalName(){cout<<"Dog ";}
void cry()const{cout<<"我是小狗我的名字叫"<<name<<"我的叫声是“汪汪汪”";}
protected:
string name;
};
class Giraffe :public Animal
{
public:
Giraffe(string n, char s):name (n),sex(s){}
void AnimalName(){cout<<"Mouse ";}
void cry()const{cout<<"我是长颈鹿我的名字叫"<<name<<"我的性别是" <<sex<<"我是小哑巴";}
protected:
string name; char sex;
};
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;
}