第13周项目1动物这样叫(3)
2015-05-29 15:45
239 查看
#include <iostream> #include <string> using namespace std; class Animal { public: virtual void cry()=0; Animal(string nam):name(nam){} protected: string name; }; class Mouse : public Animal { private: char sex; public: Mouse(string nam, char s):Animal(nam),sex(s) {} virtual void cry() { cout<<"我叫"<<name<<",是一只"<<((sex=='m')?"男":"女")<<"老鼠,我的叫声是:吱吱吱!"<<endl; } }; 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 Giraffe : public Animal { private: char sex; public: Giraffe(string nam,char s):Animal(nam), sex(s) {} virtual void cry() { cout<<"我叫"<<name<<",是"<<((sex=='m')?"男":"女")<<"长颈鹿,我的脖子太长,发不出声音来!"<<endl; } }; 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; }
相关文章推荐
- hive使用技巧(一)自动化动态分配表分区及修改hive表字段名称
- 基于wait和notify的生产者消费者实例
- rsync自动删除传输完的文件
- PHP HTTP
- android shape的使用
- SQL2008全部数据导出导入两种方法
- EF – 7.一对多关联
- STL系列之vector的常见功能使用
- 我与学院的点点滴滴
- MySQL多表查询
- Storm高级原语(二) — DRPC
- IOS单例的两种实现方式
- 软件测试基础知识
- Z - Bandwidth
- CSS3 greyscale 滤镜实现
- Linq 查询某个字段为null的数据
- windows下安装jira6.04,汉化,破解
- LFS6.3安装全过程(四)
- json使用时需要导入的包
- 四维空间解密