/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:week13-1-2.cpp
*作者:高赞
*完成日期:2015年 5 月 29 日
*版本号:v1.1
*
*问题描述:使Animal设计为抽象类
*/
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void cry()=0;
};
class Mouse:public Animal
{
public:
Mouse() {}
Mouse (string a,char b):MouseName(a),MouseSex(b)
{
string sex;
if(MouseSex=='m')
sex="公";
else sex="母";
cout<<"我叫"<<MouseName<<",是一只"<<sex<<"老鼠,我的叫声:";
}
void cry()
{
cout<<"吱吱吱!"<<endl;
}
private:
string MouseName;
char MouseSex;
};
class Cat:public Animal
{
public:
Cat() {}
Cat(string a):CatName(a)
{
cout<<"我叫"<<CatName<<",是一只猫,我的叫声:";
}
void cry()
{
cout<<"喵喵喵!"<<endl;
}
private:
string CatName;
};
class Dog:public Animal
{
public:
Dog() {}
Dog(string a):DogName(a)
{
cout<<"我叫"<<DogName<<",是一只狗,我的叫声:";
}
void cry()
{
cout<<"汪汪汪!"<<endl;
}
private:
string DogName;
};
class Giraffe:public Animal
{
public:
Giraffe() {}
Giraffe (string a,char b):GiraffeName(a),GiraffeSex(b)
{
string sex;
if(GiraffeSex=='m')
sex="公";
else sex="母";
cout<<"我叫"<<GiraffeName<<",是一头"<<sex<<"长颈鹿,";
}
void cry()
{
cout<<"我不能发声!"<<endl;
}
private:
string GiraffeName;
char GiraffeSex;
};
int main( )
{
cout<<"不知哪种动物,让我如何学叫?"<<endl;
Animal *p;
//p = new Animal();抽象类不能定义对象故不能用指针开辟空间
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;
}
抽象类不能定义对象故不能用指针开辟空间