Vehicle Rental System - OOP Project in C++
Vehicle Rental System - Full C++ Code
// Vehicle Rental System - Fully Annotated with OOP Concepts
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// ================= Encapsulation & Abstraction ===================
class Vehicle {
protected:
string brand;
string model;
const int year; // Constant member variable (must be initialized in constructor)
static int vehicleCount; // Static member shared by all instances
public:
Vehicle() : brand("Unknown"), model("Default"), year(2000) {
vehicleCount++;
}
Vehicle(string b, string m, int y) : brand(b), model(m), year(y) {
vehicleCount++;
}
Vehicle(const Vehicle& v) : brand(v.brand), model(v.model), year(v.year) {
vehicleCount++;
}
virtual ~Vehicle() { vehicleCount--; }
void setBrand(string b) { brand = b; }
void setModel(string m) { model = m; }
string getBrand() const { return brand; }
string getModel() const { return model; }
int getYear() const { return year; }
static void showVehicleCount() {
cout << "Total vehicles in system: " << vehicleCount << endl;
}
virtual void display() const {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year <<
endl;
}
};
int Vehicle::vehicleCount = 0;
// ================== Inheritance ====================
class Car : public Vehicle {
protected:
float dailyRate;
Vehicle Rental System - OOP Project in C++
public:
Car() : Vehicle(), dailyRate(100.0f) {}
Car(string b, string m, int y, float rate) : Vehicle(b, m, y), dailyRate(rate) {}
float getDailyRate() const { return dailyRate; }
void setDailyRate(float r) { dailyRate = r; }
void display() const override {
Vehicle::display();
cout << "Daily Rate: $" << dailyRate << endl;
}
};
class LuxuryCar : public Car {
string features;
public:
LuxuryCar(string b, string m, int y, float rate, string f)
: Car(b, m, y, rate), features(f) {}
void display() const override {
Car::display();
cout << "Luxury Features: " << features << endl;
}
};
class Insurance {
protected:
bool isInsured;
public:
Insurance(bool ins = false) : isInsured(ins) {}
void showInsurance() const {
cout << "Insurance Status: " << (isInsured ? "Yes" : "No") << endl;
}
};
class Bike : public Vehicle, public Insurance {
bool hasHelmet;
public:
Bike(string b, string m, int y, bool h, bool ins)
: Vehicle(b, m, y), Insurance(ins), hasHelmet(h) {}
void display() const override {
Vehicle::display();
cout << "Helmet Provided: " << (hasHelmet ? "Yes" : "No") << endl;
showInsurance();
}
};
class Payment {
Vehicle Rental System - OOP Project in C++
protected:
float amount;
public:
Payment(float amt = 0.0f) : amount(amt) {}
void showPayment() const {
cout << "Payment Amount: $" << amount << endl;
}
};
class Booking : public Car, public Payment {
int days;
string customerName;
public:
Booking(string name, string b, string m, int y, float rate, int d)
: Car(b, m, y, rate), Payment(rate * d), days(d), customerName(name) {}
void showBooking() const {
cout << "Customer: " << customerName << endl;
display();
cout << "Days: " << days << endl;
showPayment();
}
};
class Fleet {
vector<Vehicle*> vehicles;
public:
void addVehicle(Vehicle* v) {
vehicles.push_back(v);
}
void showFleet() const {
cout << "\n-- Fleet Vehicles --\n";
for (auto v : vehicles) {
v->display();
cout << "--------------------\n";
}
}
~Fleet() {
for (auto v : vehicles) {
delete v;
}
vehicles.clear();
}
};
void showVehicleInfo(const Vehicle& v) {
v.display();
Vehicle Rental System - OOP Project in C++
int main() {
Car c1("Toyota", "Corolla", 2020, 50.0);
LuxuryCar l1("BMW", "7-Series", 2023, 200.0, "Massage Seats, Sunroof");
Bike b1("Yamaha", "FZ", 2019, true, true);
Booking book1("Alice", "Honda", "Civic", 2021, 75.0, 4);
Fleet fleet;
fleet.addVehicle(new Car("Nissan", "Sentra", 2019, 45.0));
fleet.addVehicle(new LuxuryCar("Audi", "A8", 2023, 180.0, "Premium Audio,
Navigation"));
fleet.addVehicle(new Bike("Suzuki", "Gixxer", 2020, true, false));
cout << "\n--- Individual Vehicles ---\n";
c1.display();
l1.display();
b1.display();
cout << "\n--- Booking Info ---\n";
book1.showBooking();
cout << "\n--- Fleet Info ---\n";
fleet.showFleet();
cout << "\n--- Const Argument Function ---\n";
showVehicleInfo(c1);
cout << "\n--- Vehicle Count ---\n";
Vehicle::showVehicleCount();
return 0;
}