用C++的类函数写车辆管理系统代码
时间: 2023-11-08 13:05:45 浏览: 76
以下是一个使用类函数实现车辆管理系统的C++代码,包括添加车辆、查看车辆信息、修改车辆信息和删除车辆等功能:
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Car {
private:
string brand;
string model;
int year;
string color;
string licensePlate;
public:
// 构造函数
Car() {}
Car(string brand, string model, int year, string color, string licensePlate) {
this->brand = brand;
this->model = model;
this->year = year;
this->color = color;
this->licensePlate = licensePlate;
}
// 成员函数
void setBrand(string brand) {
this->brand = brand;
}
string getBrand() {
return brand;
}
void setModel(string model) {
this->model = model;
}
string getModel() {
return model;
}
void setYear(int year) {
this->year = year;
}
int getYear() {
return year;
}
void setColor(string color) {
this->color = color;
}
string getColor() {
return color;
}
void setLicensePlate(string licensePlate) {
this->licensePlate = licensePlate;
}
string getLicensePlate() {
return licensePlate;
}
};
class CarManager {
private:
vector<Car> carList;
public:
void addCar() {
Car newCar;
string brand, model, color, licensePlate;
int year;
cout << "请输入品牌:";
cin >> brand;
newCar.setBrand(brand);
cout << "请输入型号:";
cin >> model;
newCar.setModel(model);
cout << "请输入生产年份:";
cin >> year;
newCar.setYear(year);
cout << "请输入颜色:";
cin >> color;
newCar.setColor(color);
cout << "请输入车牌号码:";
cin >> licensePlate;
newCar.setLicensePlate(licensePlate);
carList.push_back(newCar);
cout << "车辆信息已添加!" << endl;
}
void viewCar() {
if (carList.empty()) {
cout << "没有车辆信息!" << endl;
return;
}
cout << "车辆信息如下:" << endl;
for (int i = 0; i < carList.size(); i++) {
cout << "品牌:" << carList[i].getBrand() << endl;
cout << "型号:" << carList[i].getModel() << endl;
cout << "生产年份:" << carList[i].getYear() << endl;
cout << "颜色:" << carList[i].getColor() << endl;
cout << "车牌号码:" << carList[i].getLicensePlate() << endl;
cout << endl;
}
}
void modifyCar() {
if (carList.empty()) {
cout << "没有车辆信息!" << endl;
return;
}
string licensePlate;
cout << "请输入要修改的车牌号码:";
cin >> licensePlate;
bool found = false;
for (int i = 0; i < carList.size(); i++) {
if (carList[i].getLicensePlate() == licensePlate) {
found = true;
string brand, model, color;
int year;
cout << "请输入新的品牌:";
cin >> brand;
carList[i].setBrand(brand);
cout << "请输入新的型号:";
cin >> model;
carList[i].setModel(model);
cout << "请输入新的生产年份:";
cin >> year;
carList[i].setYear(year);
cout << "请输入新的颜色:";
cin >> color;
carList[i].setColor(color);
cout << "修改成功!" << endl;
break;
}
}
if (!found) {
cout << "未找到该车辆!" << endl;
}
}
void deleteCar() {
if (carList.empty()) {
cout << "没有车辆信息!" << endl;
return;
}
string licensePlate;
cout << "请输入要删除的车牌号码:";
cin >> licensePlate;
bool found = false;
for (vector<Car>::iterator it = carList.begin(); it != carList.end(); it++) {
if (it->getLicensePlate() == licensePlate) {
found = true;
carList.erase(it);
cout << "删除成功!" << endl;
break;
}
}
if (!found) {
cout << "未找到该车辆!" << endl;
}
}
};
int main() {
CarManager carManager;
while (true) {
cout << "请选择操作:1.添加车辆 2.查看车辆信息 3.修改车辆信息 4.删除车辆 5.退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
carManager.addCar();
break;
case 2:
carManager.viewCar();
break;
case 3:
carManager.modifyCar();
break;
case 4:
carManager.deleteCar();
break;
case 5:
return 0;
default:
cout << "无效操作!" << endl;
break;
}
}
}
```
该代码使用了类`Car`表示车辆信息,使用了类`CarManager`来管理车辆信息。在主函数中,使用了一个死循环,提供了添加车辆、查看车辆信息、修改车辆信息和删除车辆等操作。用户可以根据自己的需要进行选择。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)