0% found this document useful (0 votes)
15 views

Vehicle Rental System CPP

The document presents a C++ implementation of a Vehicle Rental System using inheritance and polymorphism. It defines a base class 'Vehicle' and derived classes 'Car', 'Bike', and 'Truck', each with specific rental calculations and display methods. The main function demonstrates the creation and management of different vehicle objects and their rental details for a specified number of days.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Vehicle Rental System CPP

The document presents a C++ implementation of a Vehicle Rental System using inheritance and polymorphism. It defines a base class 'Vehicle' and derived classes 'Car', 'Bike', and 'Truck', each with specific rental calculations and display methods. The main function demonstrates the creation and management of different vehicle objects and their rental details for a specified number of days.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Vehicle Rental System - C++ Inheritance & Polymorphism

C++ Code for Vehicle Rental System


#include <iostream>
#include <string>
using namespace std;

// Base class: Vehicle


class Vehicle {
protected:
string vehicleNumber;
string brand;
double rentalRate;

public:
Vehicle(string vNum, string br, double rate)
: vehicleNumber(vNum), brand(br), rentalRate(rate) {}

virtual double calculateRent(int days) {


return rentalRate * days;
}

virtual void display(int days) {


cout << "Vehicle Number: " << vehicleNumber << endl;
cout << "Brand: " << brand << endl;
cout << "Rental Rate per Day: $" << rentalRate << endl;
cout << "Total Rent for " << days << " days: $" << calculateRent(days) << endl;
}

virtual ~Vehicle() {}
};

// Derived class: Car


class Car : public Vehicle {
private:
double luxuryTax;

public:
Car(string vNum, string br, double rate, double tax)
: Vehicle(vNum, br, rate), luxuryTax(tax) {}

double calculateRent(int days) override {


return (rentalRate * days) + luxuryTax;
}

void display(int days) override {


Vehicle::display(days);
cout << "Luxury Tax: $" << luxuryTax << endl;
}
};
Vehicle Rental System - C++ Inheritance & Polymorphism

// Derived class: Bike


class Bike : public Vehicle {
private:
double helmetCharge;

public:
Bike(string vNum, string br, double rate, double charge)
: Vehicle(vNum, br, rate), helmetCharge(charge) {}

double calculateRent(int days) override {


return (rentalRate * days) + helmetCharge;
}

void display(int days) override {


Vehicle::display(days);
cout << "Helmet Charge: $" << helmetCharge << endl;
}
};

// Derived class: Truck


class Truck : public Vehicle {
private:
double loadCapacityCharge;

public:
Truck(string vNum, string br, double rate, double charge)
: Vehicle(vNum, br, rate), loadCapacityCharge(charge) {}

double calculateRent(int days) override {


return (rentalRate * days) + loadCapacityCharge;
}

void display(int days) override {


Vehicle::display(days);
cout << "Load Capacity Charge: $" << loadCapacityCharge << endl;
}
};

// Main function
int main() {
int days = 5;

Vehicle* vehicles[3];
vehicles[0] = new Car("CAR123", "Toyota", 100, 50);
vehicles[1] = new Bike("BIKE456", "Yamaha", 30, 10);
vehicles[2] = new Truck("TRUCK789", "Volvo", 150, 100);

cout << "--- Vehicle Rent Details for " << days << " Days ---\n";
for (int i = 0; i < 3; ++i) {
vehicles[i]->display(days);
Vehicle Rental System - C++ Inheritance & Polymorphism
cout << "----------------------------\n";
}

for (int i = 0; i < 3; ++i) {


delete vehicles[i];
}

return 0;
}

You might also like