Project · C++ Programming (modern C++ and DSA)
Project: train seat booking with a waiting list
The brief
Model a train booking system like the one behind IRCTC: coach classes with their own fares, tickets with PNRs, a waiting list that moves up when someone cancels, refunds after a cancellation charge, and a chart. You use inheritance, virtual functions, operator overloading, the STL and smart pointers.
Why it matters
Railway, airline and bus booking systems are classic C++ and Java back ends: many classes of seat, fares that differ by class, and waiting lists that must never lose or double-book a seat. The same design questions (base classes and overrides, which container for which lookup, who owns each object) come up in interviews at product companies and in real reservation code.
Skills you practise
- abstract base classes and override
- operator<< for your own type
- std::map and std::deque
- std::unique_ptr in containers
- function templates
- sanitizers
You need
- g++ 11 or newer (MinGW-w64 via MSYS2 on Windows, build-essential on Ubuntu, Xcode command line tools on macOS, where g++ runs Apple clang)
- For step 7: Linux, macOS or WSL on Windows (MinGW g++ has no AddressSanitizer)
What you will have at the end
booking.cpp: a train booking system with coach classes, printable tickets, a waiting list with promotion and refunds, a chart and safe PNR lookups, clean under -Wall -Wextra -Wpedantic and the sanitizers.
How it is marked (rubric, 100 points)
| Correct behaviourEvery check passes: fares, waiting numbers, the refund and promotion, the chart and unknown PNRs. | 40 |
|---|---|
| Class designAn abstract Coach with override in each class, a Ticket printed by operator<<, and a Train that owns its data. | 25 |
| Right containers and ownershipmap for PNR lookup in order, deque for the waiting list, unique_ptr for coaches; no raw new/delete. | 20 |
| Safety and explanationClean with -Wpedantic and the sanitizers; you can explain why the destructor of Coach is virtual. | 15 |
0/7 steps done
Step 1 of 7
Coach classes with their own fares
For 1450 km the program prints SL Rs 745, 3A Rs 1925 and 2A Rs 2805, from a vector of unique_ptr<Coach>.
Every class of coach has the same questions (its code, rate per km, reservation and cancellation charge) with different answers, so make Coach an abstract base class with pure virtual functions and a virtual destructor. Sleeper, AC3 and AC2 override them; mark each with override so the compiler catches a wrong signature. fare(km) lives in the base class: km * paisePerKm() / 100 + reservationCharge(). Keep rates in paise as int, and multiply before dividing: paisePerKm() / 100 alone is integer division. In main, put one of each in a std::vector<std::unique_ptr<Coach>> and print each fare for 1450 km.
class AC3 : public Coach {
public:
std::string code() const override { return "3A"; }
int paisePerKm() const override { return 130; }
int reservationCharge() const override { return 40; }
int cancellationCharge() const override { return 180; }
};$ g++ -std=c++17 -Wall -Wextra -Wpedantic -o booking booking.cpp; ./bookingYour notes: booking.cpp
Write down each command you ran. It goes into your project summary.