用C++写一个车库车辆管理系统
时间: 2023-12-27 10:21:51 浏览: 100
C++ 车辆管理系统.doc
车库车辆管理系统是一个典型的管理系统,它可以帮助车库管理员更好地管理车辆信息。下面是一个简单的C++车库车辆管理系统的实现:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Vehicle {
public:
Vehicle(string id, string brand, string model, int year) {
this->id = id;
this->brand = brand;
this->model = model;
this->year = year;
}
string getId() {
return id;
}
string getBrand() {
return brand;
}
string getModel() {
return model;
}
int getYear() {
return year;
}
private:
string id;
string brand;
string model;
int year;
};
class Garage {
public:
void addVehicle(Vehicle vehicle) {
vehicles.push_back(vehicle);
}
void removeVehicle(string id) {
for (int i = 0; i < vehicles.size(); i++) {
if (vehicles[i].getId() == id) {
vehicles.erase(vehicles.begin() + i);
break;
}
}
}
void printVehicles() {
for (int i = 0; i < vehicles.size(); i++) {
cout << "ID: " << vehicles[i].getId() << endl;
cout << "Brand: " << vehicles[i].getBrand() << endl;
cout << "Model: " << vehicles[i].getModel() << endl;
cout << "Year: " << vehicles[i].getYear() << endl;
cout << endl;
}
}
private:
vector<Vehicle> vehicles;
};
int main() {
Garage garage;
Vehicle vehicle1("001", "Toyota", "Camry", 2018);
Vehicle vehicle2("002", "Honda", "Accord", 2019);
Vehicle vehicle3("003", "Ford", "Mustang", 2020);
garage.addVehicle(vehicle1);
garage.addVehicle(vehicle2);
garage.addVehicle(vehicle3);
garage.printVehicles();
garage.removeVehicle("002");
garage.printVehicles();
return 0;
}
```
在这个程序中,我们定义了两个类:Vehicle和Garage。Vehicle类表示车辆,包含车辆的ID、品牌、型号和年份等信息。Garage类表示车库,包含车辆的添加、删除和打印等操作。
在main函数中,我们创建了三个Vehicle对象,并将它们添加到Garage对象中。然后我们打印了所有车辆的信息,并删除了ID为002的车辆。最后再次打印所有车辆的信息。
阅读全文