欢迎来到天天文库
浏览记录
ID:1971576
大小:202.50 KB
页数:26页
时间:2017-11-14
《第十四周实验内容1》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、第十四周实验内容:继承与派生1.编程【问题描述】:开发一个名为Vehicle的类的层次体系。创建两个类Taxi和Truck,均以公有模式从类Vehicle中继承而来。Taxi类中应包含一个数据成员说明其是否载客。Truck类应包含一个数据成员说明其是否载货。添加必要的函数来控制和访问类的数据。编写一段测试程序,将Truck对象和Taxi对象打印到屏幕。【实例输出】【程序模板】共7个文件(Vehicle.h、Vehicle.cpp、Taxi.h、Taxi.cpp、Truck.h、Truck.cpp、driver.cpp)//vehicle
2、.h#ifndefVEHICLE_H#defineVEHICLE_H#includeusingnamespacestd;classVehicle{public:Vehicle(constintdoors,constintcylinders,charcolor,doubleinitialFuel,constinttransmission);~Vehicle();voidsetColor(charcolor);voidsetFuelLevel(doubleamount);constchargetColor()const;
3、doublegetFuelLevel()const;constintgetTransmissionType()const;constintgetNumberOfDoors()const;constintgetNumberOfCylinders()const;voidsetClassName(constchar);constchargetClassName()const;voidprintVehicle();private:constintnumberOfDoors;//门的个数constintnumberOfCylinders;//汽缸
4、的个数charvehicleColor;//交通工具的颜色doublefuelLevel;//constinttransmissionType;charclassName;};#endif//vehicle.cpp#include#include#include"vehicle.h"usingnamespacestd;Vehicle::Vehicle(constintdoors,constintcylinders,charcolor,doubleinitialFuel,constinttransmi
5、ssion):numberOfDoors(doors),numberOfCylinders(cylinders),transmissionType(transmission)/实现Vehicle类的构造函数定义/Vehicle::~Vehicle(){delete[]vehicleColor;delete[]className;}voidVehicle::printVehicle(){cout<6、ers:"<0.0&&amount<=20.0)fuelLevel=amount;elsefuelLevel=5.7、0;}//callerisresponsiblefordeletingmemoryconstcharVehicle::getColor()const{returnvehicleColor;}doubleVehicle::getFuelLevel()const{returnfuelLevel;}constintVehicle::getTransmissionType()const{returntransmissionType;}constintVehicle::getNumberOfDoors()const{returnnumberOfD8、oors;}constintVehicle::getNumberOfCylinders()const{returnnumberOfCylinders;}voidVehicle::setClassName(c
6、ers:"<0.0&&amount<=20.0)fuelLevel=amount;elsefuelLevel=5.
7、0;}//callerisresponsiblefordeletingmemoryconstcharVehicle::getColor()const{returnvehicleColor;}doubleVehicle::getFuelLevel()const{returnfuelLevel;}constintVehicle::getTransmissionType()const{returntransmissionType;}constintVehicle::getNumberOfDoors()const{returnnumberOfD
8、oors;}constintVehicle::getNumberOfCylinders()const{returnnumberOfCylinders;}voidVehicle::setClassName(c
此文档下载收益归作者所有