根据下面图示给出的Vehicle、Car、Boat、AmphibianCar类的继承关系及程序测试主函数编写完整程序源代码使程序输出如图所示的结果 交通工具 小汽车 小船 水陆两用汽车 Vehicle类对象构造成功!Car类对象构造成功!Boat类对象构造成功!载入AmphibianCar类对
#include
class Vehicle { public: Vehicle() { cout << "Vehicle类对象构造成功!" << endl; } void SetWeight(int weight) { this->weight = weight; cout << "重置重量:" << this->weight << endl; } void ShowMe() { cout << "I'm Vehicle!" << endl; } protected: int weight; };
class Car : public Vehicle { public: Car() { cout << "Car类对象构造成功!" << endl; } void ShowMe() { cout << "I'm car!" << endl; } protected: int aird; };
class Boat : public Vehicle { public: Boat() { cout << "Boat类对象构造成功!" << endl; } void ShowMe() { cout << "I'm boat!" << endl; } protected: float tonnage; };
class AmphibianCar : public Car, public Boat { public: AmphibianCar(int weight, int aird, float tonnage) { cout << "载入AmphibianCar类对象构造成功!" << endl; this->weight = weight; this->aird = aird; this->tonnage = tonnage; } void ShowMe() { cout << "I'm amphibianCar!" << endl; } void ShowMembers() { cout << "重量:" << weight << "顿,空气排量:" << aird << "CC,排水量:" << tonnage << "顿" << endl; } };
int main() { AmphibianCar a(4, 200, 1.35f); a.ShowMe(); a.ShowMembers(); a.SetWeight(3); a.ShowMembers();
return 0;
原文地址: http://www.cveoy.top/t/topic/drZW 著作权归作者所有。请勿转载和采集!