请用c++编写这个代码
时间: 2024-02-20 18:00:55 浏览: 22
好的,以下是用C++编写的汽车租赁系统代码,根据题目要求使用了继承和多态的知识点:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
public:
virtual string getType() = 0;
virtual string getBrand() = 0;
virtual string getModel() = 0;
virtual int getSeats() = 0;
virtual int getRent() = 0;
};
class Car : public Vehicle {
private:
string type = "Car";
string brand;
string model;
int seats;
int rent;
public:
Car(string brand, string model, int seats, int rent) {
this->brand = brand;
this->model = model;
this->seats = seats;
this->rent = rent;
}
string getType() {
return type;
}
string getBrand() {
return brand;
}
string getModel() {
return model;
}
int getSeats() {
return seats;
}
int getRent() {
return rent;
}
};
class SUV : public Vehicle {
private:
string type = "SUV";
string brand;
string model;
int seats;
int rent;
public:
SUV(string brand, string model, int seats, int rent) {
this->brand = brand;
this->model = model;
this->seats = seats;
this->rent = rent;
}
string getType() {
return type;
}
string getBrand() {
return brand;
}
string getModel() {
return model;
}
int getSeats() {
return seats;
}
int getRent() {
return rent;
}
};
class Bus : public Vehicle {
private:
string type = "Bus";
string brand;
string model;
int seats;
int rent;
public:
Bus(string brand, string model, int seats, int rent) {
this->brand = brand;
this->model = model;
this->seats = seats;
this->rent = rent;
}
string getType() {
return type;
}
string getBrand() {
return brand;
}
string getModel() {
return model;
}
int getSeats() {
return seats;
}
int getRent() {
return rent;
}
};
int main() {
int days;
string vehicleType, brand, model;
int seats, rent;
cout << "Enter number of days rented: ";
cin >> days;
cout << "Enter vehicle type (car/suv/bus): ";
cin >> vehicleType;
if (vehicleType == "car") {
cout << "Enter car brand: ";
cin >> brand;
cout << "Enter car model: ";
cin >> model;
cout << "Enter number of seats: ";
cin >> seats;
cout << "Enter daily rent: ";
cin >> rent;
Car car(brand, model, seats, rent);
cout << car.getType() << " - " << car.getBrand() << " " << car.getModel() << " (" << car.getSeats() << " seats)" << endl;
cout << "Rent per day: $" << car.getRent() << endl;
cout << "Total rent: $" << car.getRent() * days << endl;
} else if (vehicleType == "suv") {
cout << "Enter SUV brand: ";
cin >> brand;
cout << "Enter SUV model: ";
cin >> model;
cout << "Enter number of seats: ";
cin >> seats;
cout << "Enter daily rent: ";
cin >> rent;
SUV suv(brand, model, seats, rent);
cout << suv.getType() << " - " << suv.getBrand() << " " << suv.getModel() << " (" << suv.getSeats() << " seats)" << endl;
cout << "Rent per day: $" << suv.getRent() << endl;
cout << "Total rent: $" << suv.getRent() * days << endl;
} else if (vehicleType == "bus") {
cout << "Enter bus brand: ";
cin >> brand;
cout << "Enter bus model: ";
cin >> model;
cout << "Enter number of seats: ";
cin >> seats;
cout << "Enter daily rent: ";
cin >> rent;
Bus bus(brand, model, seats, rent);
cout << bus.getType() << " - " << bus.getBrand() << " " << bus.getModel() << " (" << bus.getSeats() << " seats)" << endl;
cout << "Rent per day: $" << bus.getRent() << endl;
cout << "Total rent: $" << bus.getRent() * days << endl;
} else {
cout << "Invalid vehicle type!" << endl;
}
return 0;
}
```
在上述代码中,我们定义了一个基类 `Vehicle`,其中包含了汽车的类型、品牌、型号、座位数和租金信息等。然后,我们派生出了三个子类 `Car`、`SUV` 和 `Bus`,分别代表了小轿车、SUV 和客车。在每个子类中,我们实现了基类中定义的虚函数,以便获取各自的具体信息。在 `main` 函数中,我们根据用户的输入选择具体的车型,并创建相应的实例,输出车辆信息和租金信息等。
注意,在实现 `Vehicle` 类时,我们使用了纯虚函数,即 `virtual string getType() = 0;` 等,这是为了使得 `Vehicle` 类成为一个抽象类,只能用来作为其他类的基类而不能直接实例化。