zoukankan      html  css  js  c++  java
  • [YTU]_2576( 虚函数练习:动物2)

    题目描述

    长期的物种进化使自然界出现了生活在陆地上的陆生动物和生活在水中的水生动物。根据已有主函数编写动物类,陆生动物类和水生动物类。

    输入

    动物的体长,体重,性别;

    水生动物的体长,体重,性别,游泳速度;

    陆生动物的体长,体重,性别,奔跑速度;

    输出

    动物的体长,体重,性别;

    水生动物的体长,体重,性别,游泳速度;

    陆生动物的体长,体重,性别,奔跑速度;

    样例输入

    52 22 f
    62 32 m 122
    72 42 m 102

    样例输出

    height:52
    weight:22
    sex:f
    height:62
    weight:32
    sex:m
    swimming_speed:122
    height:72
    weight:42
    sex:m
    running_speed:102
    #include <iostream>
    using namespace std;
    class animal
    {
        public:animal(int h,int w,char s):height(h),weight(w),sex(s){}
        virtual void display()
        {
            cout<<"height:"<<height<<endl<<"weight:"<<weight<<endl<<"sex:"<<sex<<endl;
        }
    protected:
            int height,weight;
            char sex;
    };
    class aqu_animal:public animal
    {
        public:
            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<<"weight:"<<weight<<endl<<"sex:"<<sex<<endl<<"swimming_speed:"<<swimming_speed<<endl;
            }
        protected:
            int swimming_speed;
    };
    class ter_animal:public animal
    {
    public:
        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<<"weight:"<<weight<<endl<<"sex:"<<sex<<endl<<"running_speed:"<<running_speed<<endl;
        }
    protected:
      int running_speed;
    };
    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;
    }

  • 相关阅读:
    AJAX
    前端上传文件 后端PHP获取文件
    PHP基础语法
    JS错误记录
    JS学习笔记
    python利用xlrd读取excel文件始终报错原因
    安装xlwt和xlrd
    编程菜鸟的日记-Linux无处不在
    编程菜鸟的日记-《软件测试》Ron Patton著-读书笔记
    编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习9
  • 原文地址:https://www.cnblogs.com/sxy201658506207/p/7586319.html
Copyright © 2011-2022 走看看