声明一个车(vehicle)基类,具有MaSpeed、weight等成员变量有Run、Stop等成员函数.pdfVIP

  • 68
  • 0
  • 约6.63千字
  • 约 14页
  • 2021-10-11 发布于上海
  • 举报

声明一个车(vehicle)基类,具有MaSpeed、weight等成员变量有Run、Stop等成员函数.pdf

实验九 多态性与虚函数(2) 一、实验目的和要求 1. 了解多态的概念; 2. 了解虚函数的作用及使用方法; 3. 了解静态关联和动态关联的概念和用法; 4. 了解纯虚函数和抽象类的概念和用法 二、实验内容和结果 1. 声明一个车(vehicle)基类,具有MaxSpeed、weight等成员变量有Run、Stop等成员 函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,自行车(bicycle)类有 高度(Height)等属性,汽车(motorcar)有座位数(SeatNum)等属性,类从 bicycle 和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数,使用虚 函数完成。(各个类都必须定义构造函数和析构函数) #include iostream using namespace std; class vehicle { protected : floa MaxSpeed; floa weight; public : ~vehicle() {} vehicle(floa m,floa w):MaxSpeed(m),weight(w) {} virtual void Run () { cou 车已经启动啦!!!endlendl; } virtual void Stop () { coutvehicle已经停啦!!!endl; } }; class bicycle: virtual public vehicle { protected : float Height; public : ~bicycle() {} bicycle(float m,float w,float h):vehicle( m,w),Height(h) {} void Run () { coutThe height of bicycle is:Heightendlendl; } void Stop () { coutbicycle已经停啦!!!endl; } }; class motorcar: virtual public vehicle { protected : int SeatNum; public : ~motorcar() {} motorcar(float m,float w,float s):vehicle( m,w),SeatNum(s) {} void Run () { coutThe number of motorcars seat is:SeatNumendlendl; } void Stop () { coutmotorcar已经停啦!!!endl; } }; class motorcycle: public bicycle, public motorcar { public : ~motorcycle() {} motorcycle(float m,float w,float h,float s):vehicle( m,w),bicycle( m,w,h), motorcar(m,w,s) {} void Run () { coutMaxSpeed:MaxSpeedendl; coutweight:weightendl; coutThe height is:Heightendl; coutThe number of seat is:SeatNumendl; } void Stop () { coutmotorcycle已经停啦!!!endl; } }; int main() {

文档评论(0)

1亿VIP精品文档

相关文档