YTU-OJ-Problem H: 虚函数练习:动物2
来源:互联网 发布:三菱m70网络传输 设置 编辑:程序博客网 时间:2024/05/22 01:36
Problem H: 虚函数练习:动物2
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 425 Solved: 289
[Submit][Status][Web Board]
Description
长期的物种进化使自然界出现了生活在陆地上的陆生动物和生活在水中的水生动物。根据已有主函数编写动物类,陆生动物类和水生动物类。
Input
动物的体长,体重,性别;
水生动物的体长,体重,性别,游泳速度;
陆生动物的体长,体重,性别,奔跑速度;
Output
动物的体长,体重,性别;
水生动物的体长,体重,性别,游泳速度;
陆生动物的体长,体重,性别,奔跑速度;
Sample Input
52 22 f62 32 m 12272 42 m 102
Sample Output
height:52weight:22sex:fheight:62weight:32sex:mswimming_speed:122height:72weight:42sex:mrunning_speed:102
HINT
主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
/*c++代码*/
int main()
{
int a,b,s,r;
char c;
animal *p;
cin>>a>>b>>c;
animal pa(a,b,c);
p=&pa;
p->display();
cin>>a>>b>>c>>s;
aqu_animal pb(a,b,c,s);
p=&pb;
p->display();
cin>>a>>b>>c>>r;
ter_animal pc(a,b,c,r);
p=&pc;
p->display();
return 0;
}
#include <iostream>using namespace std;class animal{protected: int height; int weight; char sex;public: animal() {} animal(int h,int w,char s): height(h),weight(w),sex(s) {} void virtual display() { cout<<"height:"<<height<<endl; cout<<"weight:"<<weight<<endl; cout<<"sex:"<<sex<<endl; }};class aqu_animal:public animal{protected: int swimming_speed;public: aqu_animal() {} aqu_animal(int h,int w,char s,int s_p): animal(h,w,s),swimming_speed(s_p) {} void display() { cout<<"height:"<<height<<endl; cout<<"weight:"<<weight<<endl; cout<<"sex:"<<sex<<endl; cout<<"swimming_speed:"<<swimming_speed<<endl; }};class ter_animal:public animal //陆生动物{protected: int running_speed; //游泳速度public: ter_animal() {} ter_animal(int h,int w,char s,int r_p): animal(h,w,s),running_speed(r_p) {} void display() { cout<<"height:"<<height<<endl; cout<<"weight:"<<weight<<endl; cout<<"sex:"<<sex<<endl; cout<<"running_speed:"<<running_speed<<endl; }};int main(){ int a,b,s,r; char c; animal *p; cin>>a>>b>>c; animal pa(a,b,c); p=&pa; p->display(); cin>>a>>b>>c>>s; aqu_animal pb(a,b,c,s); p=&pb; p->display(); cin>>a>>b>>c>>r; ter_animal pc(a,b,c,r); p=&pc; p->display(); return 0;}
0 0
- YTU-OJ-Problem H: 虚函数练习:动物2
- [YTU]_2576( 虚函数练习:动物2)
- YTU-OJ-虚基类练习:动物
- 虚函数练习:动物2
- [YTU]_2566( 虚基类练习:动物)
- OJ第三批——Problem M: 虚基类练习:动物
- YTU-OJ-Problem D: C++习题 虚函数-计算图形面积
- YTU-OJ-Problem E: 类的应用
- YTU-OJ-Problem G: 交通工具信息
- YTU-OJ-Problem J: B3 指向基类的指针访问派生类的成员函数
- 虚基类练习 动物2
- YTU-OJ-Problem K: 熟悉题型——代码完善(输出日期时间--友元函数)
- YTU-OJ-Problem A: 最小节点(线性表)
- YTU-OJ-Problem B: 抽象一个形状类
- YTU-OJ-Problem C: C++习题 抽象基类
- YTU-OJ-Problem O: A类设计--正方形
- YTU-OJ-Problem Q: A改错题--体检情况分析
- YTU-OJ-Problem A: A代码完善--向量的运算
- 检测网线插拔状态
- YTU-OJ-Problem E: 类的应用
- Objective-C学习笔记之协议和代理
- YTU-OJ-Problem G: 交通工具信息
- 关于node.js中引入文件的问题
- YTU-OJ-Problem H: 虚函数练习:动物2
- 线性链表的创建
- Android提高(11)——第一章 什么是Handler
- 导致View的OnClick事件无效或者失灵的原因
- IOS音频架构之Audio Unit
- 详解Android中AsyncTask的使用
- UDP编程·server
- JavDroider的作品展示
- YTU-OJ-Problem I: 熟悉题型——类设计( 矩形类定义【C++】)