Define a Vehicle class and its subclasses Bike and Car. The detailed requirements are: (1) Data members: a) Vehicle’s data members include: wheels(indicates the number of tires) and weight(indicates the weight of the vehicle). b) Bike has an additional data member: price(indicates the weight of the vehicle). c) Car has an additional data members: passenger_load(indicates the load of the vehicle). (2) Member functions: a) Each class should have a constructor to initialize its data members. b) Define in each class a function show() to display its data members. (3) A global function showVehicle(Vehicle &vehicle) to call a Vehicle object’s show() function to display its content. (4) The main() function: a) Define a Car object car and an array bikes to store two Bike objects. b) Use the function showVehicle to display the information of car. c) Define a Vehicle pointer p_bikes to point to the array and display each bike’s information with a loop(Call show () in the bike subclass)
时间: 2024-03-05 11:55:02 浏览: 19
Sure, here is a possible implementation in C++:
```cpp
#include <iostream>
using namespace std;
class Vehicle {
protected:
int wheels;
int weight;
public:
Vehicle(int w, int ww) : wheels(w), weight(ww) {}
virtual void show() {
cout << "Wheels: " << wheels << ", Weight: " << weight << endl;
}
};
class Bike : public Vehicle {
private:
int price;
public:
Bike(int w, int ww, int p) : Vehicle(w, ww), price(p) {}
void show() override {
cout << "Bike: ";
Vehicle::show();
cout << "Price: " << price << endl;
}
};
class Car : public Vehicle {
private:
int passenger_load;
public:
Car(int w, int ww, int pl) : Vehicle(w, ww), passenger_load(pl) {}
void show() override {
cout << "Car: ";
Vehicle::show();
cout << "Passenger load: " << passenger_load << endl;
}
};
void showVehicle(Vehicle& vehicle) {
vehicle.show();
}
int main() {
Car car(4, 1000, 5);
Bike bikes[2] = {Bike(2, 20, 100), Bike(2, 25, 120)};
showVehicle(car);
Vehicle* p_bikes = bikes;
for (int i = 0; i < 2; ++i) {
p_bikes[i].show(); // call show() of the Bike subclass
}
return 0;
}
```
In this implementation, the `Vehicle` class is defined with two data members `wheels` and `weight`, and a virtual `show()` function to display them. The `Bike` class is defined as a subclass of `Vehicle` with an additional data member `price`, and overrides the `show()` function to print out the bike's information with a "Bike" prefix. Similarly, the `Car` class is defined as a subclass of `Vehicle` with an additional data member `passenger_load`, and overrides the `show()` function to print out the car's information with a "Car" prefix.
The `showVehicle()` function is defined as a global function that takes a reference to a `Vehicle` object and calls its `show()` function. This function is used to display the information of `car` in the `main()` function.
In the `main()` function, a `Car` object `car` and an array of two `Bike` objects `bikes` are defined and initialized with constructor calls. The `showVehicle()` function is called with `car` to display its information. Then, a `Vehicle` pointer `p_bikes` is defined to point to the `bikes` array, and a loop is used to call the `show()` function of each `Bike` object through `p_bikes[i]`.
When running the program, the output should be:
```
Car: Wheels: 4, Weight: 1000
Passenger load: 5
Bike: Wheels: 2, Weight: 20
Price: 100
Bike: Wheels: 2, Weight: 25
Price: 120
```