C++多态性实验报告含代码和结果截图.docVIP

  • 63
  • 0
  • 约2.1千字
  • 约 6页
  • 2017-08-21 发布于重庆
  • 举报

C++多态性实验报告含代码和结果截图.doc

C++多态性实验报告含代码和结果截图.doc

实验报告 课程: 面向对象技术 学号: 姓名: 班级: 教师: 计算机科学与技术系 实验六 多态性 一、实验目的及要求 二、实验环境、实验内容 四、实验结果 五、总结、附录(源程序清单)#includeiostream using namespace std; int sign=0; class vehicle { public: vehicle(float m,float w) { if(m240m0) MaxSpeed=m; else { cout汽车超速!endl; sign=1; return; } if(w500w0) Weight=w; else { cout汽车超重!endl; sign=1; return; } cout构造了一个vehicle对象endl; } virtual void Run() { coutvehicle Run 函数被调用endl;} virtual void Stop(){ coutvehicle Stop 函数被调用endlendl;} private: float MaxSpeed; float Weight; } class bicycle:virtual public vehicle { public: bicycle(float h,float m,float w):vehicle(m,w) { if(h1.5h0) Height=h; else { cout自行车超高!endl; sign=1; return; } cout构造了一个bicycle对象endl; } void Run() { coutbicycle Run 函数被调用endl;} void Stop(){ coutbicycle Stop 函数被调用endlendl;} private: float Height; } class motorcar:virtual public vehicle { public: motorcar(float s,float m,float w):vehicle(m,w) { if(s2s0) SeatNum=s; else { cout摩托车超载!endl; sign=1; return; } cout构造了一个motorcar对象endl; } void Run() { coutmotorcar Run 函数被调用endl;} void Stop(){ coutmotorcar Stop 函数被调用endlendl;} private: float SeatNum; } class motorcycle:public bicycle,public motorcar { public: motorcycle(float h,float s,float m,float w):bicycle(h,m,w),motorcar(s,m,w),vehicle(m,w) { if(sign==0) { cout构造了一个motorcycle对象endl; } } void Run() { coutmotorcycle Run 函数被调用endl;} void Stop(){ coutmotorcycle Stop 函数被调用endlendl;} }; void main () { float m,w,h,s; int p; do{ sign=0; cout请输入参数:endlendl; cout汽车最高时速(km/h); cinm; cout汽车重量(t); cinw; cout自行车高度(m); cinh; cout摩托车座位(个); cins; motorcycle car(h,s,m,w); if(sign==0) { car.Run(); car.Stop(); } else { cout1—重新输入 2——结束程序; cinp; if(p==2) return; else coutendlendl; } }while(sign==1); } 3

文档评论(0)

1亿VIP精品文档

相关文档