c++肉牛和奶牛
2018-02-01 18:12 zlj1817 阅读(460) 评论(0) 编辑 收藏 举报#include<iostream.h> class niu { protected: int TiZhong; int ChanNaiLiang; public: niu(int tz=0,int cnl=0) { TiZhong=tz; ChanNaiLiang=cnl; } int getTiZhong() { return TiZhong; } int getChanNaiLiang() { return ChanNaiLiang; } virtual bool IsGood()=0; virtual void showHG()=0; virtual void inputCNL()=0; virtual void inputTZ()=0; }; class NaiNiu: public niu { public: NaiNiu(int cnl=0):niu(0,cnl) { inputCNL(); } bool IsGood() { if(getChanNaiLiang()>10) return true; else return false; } void showHG() { if(IsGood()) cout<<"奶牛合格"<<endl; } void inputCNL() { cout<<"请输入产奶量"; cin>>ChanNaiLiang; } void inputTZ(){} }; class RouNiu: public niu { public: RouNiu(int tz=0):niu(tz,0) { inputTZ(); } bool IsGood() { if(getTiZhong()>1000) return true; else return false; } void showHG() { if(IsGood()) cout<<"肉牛合格"<<endl; } void inputCNL(){} void inputTZ() { cout<<"请输入体重"; cin>>TiZhong; } }; void main() { niu* p[100];/////// int i=0; int cnt=0; int type=0;//0表示肉牛,1表示奶牛 cout<<"请输入你想要的牛的数量"; cin>>cnt; for(i=0;i<cnt;i++) { cout<<"请选择肉牛(0),还是奶牛(1):"; cin>>type; if(type==0) p[i]=new RouNiu();////////// else p[i]=new NaiNiu();////////// } for(i=0;i<cnt;i++) { p[i]->showHG(); delete p[i]; } }