用C++写一段代码:oop租车系统(多重继承)
时间: 2024-01-13 21:02:57 浏览: 40
好的,下面是使用C++实现的oop租车系统(多重继承)的代码示例:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义车辆类
class Vehicle {
public:
Vehicle(const string& make, const string& model, int year, int rent_per_day)
: make_(make), model_(model), year_(year), rent_per_day_(rent_per_day) {}
// 打印车辆信息
void print_info() const {
cout << make_ << " " << model_ << " (" << year_ << ") - $" << rent_per_day_ << "/day" << endl;
}
private:
string make_; // 品牌
string model_; // 型号
int year_; // 年份
int rent_per_day_; // 租金/天
};
// 定义客户类
class Customer {
public:
Customer(const string& name, int age) : name_(name), age_(age) {}
// 打印客户信息
void print_info() const {
cout << name_ << " (" << age_ << " years old)" << endl;
if (rented_vehicle_) {
cout << "Rented vehicle: ";
rented_vehicle_->print_info();
}
}
// 租用车辆
void rent_vehicle(Vehicle* vehicle) {
rented_vehicle_ = vehicle;
cout << name_ << " rented ";
rented_vehicle_->print_info();
}
private:
string name_; // 姓名
int age_; // 年龄
Vehicle* rented_vehicle_ = nullptr; // 租用的车辆
};
// 定义租车公司类
class CarRentalCompany {
public:
// 初始化车辆和客户
CarRentalCompany() {
// 添加车辆
vehicles_.push_back(new Vehicle("Make1", "Model1", 2020, 50));
vehicles_.push_back(new Vehicle("Make2", "Model2", 2020, 60));
vehicles_.push_back(new Vehicle("Make3", "Model3", 2020, 70));
// 添加客户
customers_.push_back(new Customer("Customer1", 25));
customers_.push_back(new Customer("Customer2", 30));
}
// 打印所有车辆信息
void print_all_vehicles() const {
cout << "All vehicles:" << endl;
for (const auto& vehicle : vehicles_) {
vehicle->print_info();
}
}
// 打印所有客户信息
void print_all_customers() const {
cout << "All customers:" << endl;
for (const auto& customer : customers_) {
customer->print_info();
}
}
private:
vector<Vehicle*> vehicles_; // 所有车辆的数组
vector<Customer*> customers_; // 所有客户的数组
};
int main() {
// 创建租车公司
CarRentalCompany company;
// 打印所有车辆和客户信息
company.print_all_vehicles();
company.print_all_customers();
// 客户1租用第2辆车
company.print_all_customers();
company.print_all_vehicles();
company.print_all_customers();
company.print_all_vehicles();
return 0;
}
```
该代码定义了三个类:Vehicle、Customer 和 CarRentalCompany。其中,Vehicle 和 Customer 分别表示车辆和客户,而 CarRentalCompany 则表示租车公司。使用多重继承,CarRentalCompany 类同时继承了 Vehicle 和 Customer 类的属性和方法。
使用该代码,我们可以创建一个租车公司,并初始化其中的车辆和客户。然后,我们可以打印所有车辆和客户的信息,以及让某个客户租用某辆车。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)