希望能满足lz的需要: #include #include using namespace std; class Vehicle { public: int maxSpeed; int zl; Vehicle(int _maxSpeed, int _zl) { maxSpeed = _maxSpeed; zl = _zl; } }; class Bicycle:public Vehicle { int high; string xh; public: Bicycle(int _high, string _xh, int a, int b):Vehicle(a, b) { high = _high; xh = _xh; } void show() { cout << "bicycle:" << endl; cout << "高度:" << high << endl; cout << "型号:" << xh << endl; cout << "最大速度: " << maxSpeed << endl; cout << "重量: " << zl << endl; } }; class Motorcar:public Vehicle { public: int people; int lts;//轮胎数 int mls;// 马力 string cj; //厂家 string cz;//车主属性 Motorcar(int _people, int _lts, int _mls, string _cj, string _cz, int a, int b): Vehicle(a, b) { people = _people; lts = _lts; mls = _mls; cj = _cj; cz = _cz; } }; class Car:public Motorcar { int hyl;//耗油量 public: Car(int _hyl, int _people, int _lts, int _mls, string _cj, string _cz, int a, int b): Motorcar( _people, _lts, _mls, _cj, _cz, a, b) { hyl = _hyl; } void show() { cout << "car:" << endl; cout << "耗油量:" << hyl << endl; cout << "乘坐人数:" << people << endl; cout << "轮胎数:" << lts << endl; cout << "马力:" << mls << endl; cout << "生产厂家:" << cj << endl; cout << "车主属性:" << cz << endl; cout << "最大属性:" << maxSpeed << endl; cout << "重量:" << zl << endl; } }; class Bus: public Motorcar { int number; public: Bus(int _number, int _people, int _lts, int _mls, string _cj, string _cz, int a, int b): Motorcar( _people, _lts, _mls, _cj, _cz, a, b) { number = _number; } void show() { cout << "bus:" << endl; cout << "车厢数:" << number << endl; cout << "乘坐人数:" << people << endl; cout << "轮胎数:" << lts << endl; cout << "马力:" << mls << endl; cout << "生产厂家:" << cj << endl; cout << "车主属性:" << cz << endl; cout << "最大属性:" << maxSpeed << endl; cout << "重量:" << zl << endl; } }; class Truck: public Motorcar { int maxWeight; public: Truck(int _maxWeight, int _people, int _lts, int _mls, string _cj, string _cz, int a, int b): Motorcar( _people, _lts, _mls, _cj, _cz, a, b) { maxWeight = _maxWeight; } void show() { cout << "truck:" << endl; cout << "最大承载量:" << maxWeight << endl; cout << "乘坐人数:" << people << endl; cout << "轮胎数:" << lts << endl; cout << "马力:" << mls << endl; cout << "生产厂家:" << cj << endl; cout << "车主属性:" << cz << endl; cout << "最大属性:" << maxSpeed << endl; cout << "重量:" << zl << endl; } }; void menu() { Bicycle *bi; Car *ca; Bus *bu; Truck *tr; char ch; do { cout << "********车辆管理********" << endl; cout << "1, 车辆信息输入: " << endl; cout << "2、自行车信息输出:" << endl; cout << "3、小汽车信息输出: " << endl; cout << "4、公共汽车信息输出: " << endl; cout << "5、卡车信息输出: " << endl; cout << "0、程序结束: " << endl; int x; cin >> x; if (x == 1) { //void enter(); cout << "输入自行车的信息:高度 型号 最大速度 重量 "; int _high; string _xh; int a; int b; cin >> _high >> _xh >> a >> b; bi = new Bicycle(_high, _xh, a, b); cout << "输入car的信息:耗油量 载人数 轮胎数 马力 生产厂家 车主属性 最大速度 重量"; int _hyl; int _people1; int _lts1; int _mls1;string _cj1; string _cz1; int a1; int b1; cin >> _hyl >> _people1 >> _lts1 >> _mls1>> _cj1 >> _cz1 >> a1 >> b1; ca = new Car(_hyl,_people1, _lts1, _mls1, _cj1, _cz1, a1, b1); cout << "输入bus的信息:车厢节数 载人数 轮胎数 马力 生产厂家 车主属性 最大速度 重量"; int _number; int _people2; int _lts2; int _mls2;string _cj2; string _cz2; int a2; int b2; cin >> _number >> _people2 >> _lts2 >> _mls2 >> _cj2 >> _cz2 >> a2 >> b2; bu = new Bus(_number,_people2, _lts2, _mls2, _cj2, _cz2, a2, b2); cout << "输入truck的信息:最大载重量 载人数 轮胎数 马力 生产厂家 车主属性 最大速度 重量"; int _maxWeight; int _people3; int _lts3; int _mls3;string _cj3; string _cz3; int a3; int b3; cin >> _maxWeight >> _people3 >> _lts3 >> _mls3 >> _cj3 >> _cz3 >> a3 >> b3; tr = new Truck(_maxWeight,_people3, _lts3, _mls3, _cj3, _cz3, a3, b3); } if (x == 2) { bi->show(); } if (x == 3) { ca->show(); } if (x == 4) { bu->show(); } if (x == 5) { tr->show(); } if (x == 0) { cout << "end!"; exit(0); } cout << "continue?(y/n):"; cin >> ch; }while ('y'== ch); } void main() { menu(); }