第十三周--动物怎么叫(1)
2015-06-02 10:43
162 查看
#include<iostream> #include<string> using namespace std; class Animal { public: virtual void cry() { cout<<"不知哪种动物,让我如何学叫?"<<endl; } }; class Mouse:public Animal { public: Mouse(string n,char s):name(n),sex(s){}; virtual void cry() { cout<<"我叫"<<name<<", 是一只"<<((sex=='m')?"男":"女")<<"老鼠,我的叫声是:吱吱吱"<<endl; } private: string name; char sex; }; class Cat:public Animal { public: Cat(string n):name(n){} virtual void cry() { cout<<"我叫"<<name<<",是一只猫, 我的叫声是:喵喵喵"<<endl; } private: string name; }; class Dog:public Animal { public: Dog(string n):name(n){} virtual void cry() { cout<<"我叫"<<name<<",是一只狗, 我的叫声是:汪汪汪"<<endl; } private: string name; }; class Giraffe:public Animal { public: Giraffe(string n,char s):name(n),sex(s){} virtual void cry() { cout<<"我叫"<<name<<", 是一只"<<((sex=='m')?"男":"女")<<"长颈鹿,我的脖子太长发不出声音来!"<<endl; } private: 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; }
相关文章推荐
- 日经春秋 20150602
- 日经社説 20150602 バター不足が迫る酪農改革
- 为什么程序员应该从现在就开始看书
- 【VS2013学习】
- Raft一致性算法
- c 函数指针
- TRIZ系列-创新原理-22-变害为利原理
- Android源代码下载方法具体解释
- Graph database_neo4j 底层存储结构分析(7)
- C++二分查找在搜索引擎多文档求交的应用分析
- list_for_each_entry宏函数解析(上)
- 数学之美札记:谈谈中文分词
- python中os.path.isfile()和os.path.isdir()均返回false的问题
- 天声人語 20150602
- Java 并发编程框架(一)
- Java 排序算法
- 【转】【java】java中的byte,以及它与int的转化
- 快速排序
- 年月日时分秒加星期的即时显示的JS
- UIKit 框架之UISearchController