有一个汽车类Vehicle,将它作为基类派生公共汽车类Bus、卡车类Truck,其中Bus包含速度、重量和乘客数,Truck包含速度、重量和载货量,根据以下代码设计相关的类。 #include <iostream> using namespace std; //在此补全相关类 void test(Vehicle &ref) { ref.ShowMember(); cout<<endl; } int main() { float speed;//交通工具速度 float weight;//交通工具重量 int passagers ;//公共汽车乘客数 float load;//卡车载重量 cin>>speed>>weight>>passagers; Bus b(speed,weight,passagers); cin>>speed>>weight>>load; Truck t(speed,weight,load); test(b); test(t); return 1; } 【输入形式】 【输出形式】 【样例输入】 100 5 50 150 10 20 【样例输出】 speed=100,weight=5,passagers=50 speed=150,weight=10,load=20c++
时间: 2023-06-27 18:04:56 浏览: 136
```
#include <iostream>
using namespace std;
class Vehicle
{
protected:
float speed;
float weight;
public:
Vehicle(){}
Vehicle(float s, float w): speed(s), weight(w){}
virtual void ShowMember() const
{
cout << "speed=" << speed << ",weight=" << weight;
}
};
class Bus : public Vehicle
{
private:
int passagers;
public:
Bus(){}
Bus(float s, float w, int p): Vehicle(s, w), passagers(p){}
virtual void ShowMember() const
{
Vehicle::ShowMember();
cout << ",passagers=" << passagers;
}
};
class Truck : public Vehicle
{
private:
float load;
public:
Truck(){}
Truck(float s, float w, float l): Vehicle(s, w), load(l){}
virtual void ShowMember() const
{
Vehicle::ShowMember();
cout << ",load=" << load;
}
};
void test(Vehicle &ref)
{
ref.ShowMember();
cout << endl;
}
int main()
{
float speed;
float weight;
int passagers;
float load;
cin >> speed >> weight >> passagers;
Bus b(speed, weight, passagers);
cin >> speed >> weight >> load;
Truck t(speed, weight, load);
test(b);
test(t);
return 1;
}
```
阅读全文