


C++
#include <iostream>
using namespace std;
class Vehicle {
private:
float maxSpeed, weight;
public:
Vehicle(float m, float w) : maxSpeed(m), weight(w) {}
float getMaxSpeed() const { return maxSpeed; }
float getWeight() const { return weight; }
virtual void run() { cout << "交通工具开始运行" << endl; }
virtual void stop() { cout << "交通工具停止运行" << endl; }
};
class Bicycle : virtual public Vehicle {
private:
float height;
public:
Bicycle(float m, float w, float h) : Vehicle(m, w), height(h) {}
float getHeight() const { return height; }
void run() override { cout << "自行车开始运行" << endl; }
void stop() override { cout << "自行车停止运行" << endl; }
};
class Motorcar : virtual public Vehicle {
private:
int seatNum;
public:
Motorcar(float m, float w, int s) : Vehicle(m, w), seatNum(s) {}
int getSeatNum() const { return seatNum; }
void run() override { cout << "汽车开始运行" << endl; }
void stop() override { cout << "汽车停止运行" << endl; }
};
class Motorcycle : public Bicycle, public Motorcar {
public:
Motorcycle(float m, float w, float h, int s)
: Vehicle(m, w), Bicycle(m, w, h), Motorcar(m, w, s) {}
void run() override { cout << "摩托车开始运行" << endl; }
void stop() override { cout << "摩托车停止运行" << endl; }
};
int main() {
float maxSpeed, weight, height;
int seatNum;
cin >> maxSpeed >> weight >> height >> seatNum;
Motorcycle mc(maxSpeed, weight, height, seatNum);
cout << mc.getMaxSpeed() << endl;
cout << mc.getWeight() << endl;
cout << mc.getHeight() << endl;
cout << mc.getSeatNum() << endl;
mc.run();
mc.stop();
return 0;
}评论 (1)


