zoukankan      html  css  js  c++  java
  • 虚基类练习 动物1

    /*长期的物种进化使两栖动物既能活跃在陆地上,又能游动于水中。

    利用虚基类建立一个类的多重继承。包含动物(animal,属性有体长,体重和性别), 陆生动物(ter_animal,属性添加了奔跑速度)。水生动物(aqu_animal。 属性添加了游泳速度)和两栖动物(amp_animal)。

    当中两栖动物保留了陆生动物和水生动物的属性。 Input 两栖动物的体长,体重。性别,游泳速度,奔跑速度(running_speed) Output 初始化的两栖动物的体长,体重,性别,游泳速度。奔跑速度(running_speed) 和输入的两栖动物的体长,体重。性别。游泳速度,奔跑速度(running_speed)*/ #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){} }; class aqu_animal:virtual 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){} };class ter_animal:virtual 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) {} }; class amp_animal:public aqu_animal,public ter_animal { public: amp_animal(){} amp_animal(int h,int w,char s,int s_p,int r_p):animal(h,w,s),aqu_animal(h,w,s,s_p),ter_animal(h,w,s,r_p){} void input() cin>>height>>weight>>sex>>swimming_speed>>running_speed; void show() { cout<<"height:"<<height<<endl; cout<<"weight:"<<weight<<endl; cout<<"sex:"<<sex<<endl; cout<<"swimming_speed:"<<swimming_speed<<endl; cout<<"running_speed:"<<running_speed<<endl; } }; int main() { amp_animal a1(50,20,'m',100,120); amp_animal a2; a2.input(); a1.show(); cout<<endl; a2.show(); return 0; }


  • 相关阅读:
    P4718 [模板]Pollard-Rho算法
    python爬虫模板
    Codeforces1248F. Catowice City
    P3980 [NOI2008]志愿者招募 (费用流)
    P2805 [NOI2009]植物大战僵尸 (拓扑排序 + 最小割)
    P3157 [CQOI2011]动态逆序对
    P2634 [国家集训队]聪聪可可 (点分治)
    HDU6703 array (线段树)
    Codeforces750E. New Year and Old Subsequence (线段树维护DP)
    Codeforces301D. Yaroslav and Divisors
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6747493.html
Copyright © 2011-2022 走看看