用C++代码写一个火车票管理系统
时间: 2023-11-06 20:04:01 浏览: 26
以下是一个简单的火车票管理系统的C++代码:
```c++
#include<iostream>
#include<string>
using namespace std;
class Train {
private:
string trainNo;
string trainName;
string departure;
string destination;
int capacity;
int seatsBooked;
double fare;
public:
Train(string no, string name, string dep, string dest, int cap, double f) {
trainNo = no;
trainName = name;
departure = dep;
destination = dest;
capacity = cap;
seatsBooked = 0;
fare = f;
}
string getTrainNo() { return trainNo; }
string getTrainName() { return trainName; }
string getDeparture() { return departure; }
string getDestination() { return destination; }
int getCapacity() { return capacity; }
int getSeatsBooked() { return seatsBooked; }
double getFare() { return fare; }
void bookSeat(int n) {
if(seatsBooked + n <= capacity) {
seatsBooked += n;
cout << "Booking confirmed for " << n << " seats." << endl;
cout << "Total fare: $" << n*fare << endl;
} else {
cout << "Not enough seats available." << endl;
}
}
};
int main() {
Train t("001", "Express", "New York", "Washington", 100, 50.0);
cout << "Train No.: " << t.getTrainNo() << endl;
cout << "Train Name: " << t.getTrainName() << endl;
cout << "Departure: " << t.getDeparture() << endl;
cout << "Destination: " << t.getDestination() << endl;
cout << "Capacity: " << t.getCapacity() << endl;
cout << "Seats Booked: " << t.getSeatsBooked() << endl;
cout << "Fare: $" << t.getFare() << endl;
t.bookSeat(3);
cout << "Seats Booked: " << t.getSeatsBooked() << endl;
t.bookSeat(98);
cout << "Seats Booked: " << t.getSeatsBooked() << endl;
return 0;
}
```
这个系统只能管理一个火车,可以根据需要进行扩展。