写一段垂直车位泊出的C++代码
时间: 2023-12-01 10:55:50 浏览: 65
以下是一个简单的垂直车位泊出的C++代码示例:
```
#include <iostream>
using namespace std;
class ParkingSpace {
private:
int spaceNumber;
bool isOccupied;
public:
ParkingSpace(int num) {
spaceNumber = num;
isOccupied = false;
}
void occupySpace() {
if (isOccupied) {
cout << "ERROR: Space " << spaceNumber << " is already occupied." << endl;
}
else {
isOccupied = true;
cout << "Space " << spaceNumber << " is now occupied." << endl;
}
}
void vacateSpace() {
if (isOccupied) {
isOccupied = false;
cout << "Space " << spaceNumber << " is now vacant." << endl;
}
else {
cout << "ERROR: Space " << spaceNumber << " is already vacant." << endl;
}
}
};
int main() {
ParkingSpace space1(1);
ParkingSpace space2(2);
ParkingSpace space3(3);
space1.occupySpace();
space2.occupySpace();
space3.occupySpace();
space2.vacateSpace();
space3.occupySpace();
return 0;
}
```
在这个示例中,我们定义了一个 `ParkingSpace` 类,该类具有两个私有变量:`spaceNumber` 表示车位编号,`isOccupied` 表示车位是否被占用。该类还具有两个公共方法:`occupySpace()` 和 `vacateSpace()`,分别用于占用和释放车位。在主函数中,我们创建了三个 `ParkingSpace` 对象,并对它们进行了一系列操作,模拟了车辆的泊出过程。
阅读全文