0% found this document useful (0 votes)
27 views6 pages

Oop Lab 8

Uploaded by

Huzaifa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

Oop Lab 8

Uploaded by

Huzaifa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB ASSIGNMENT: #8

Problem Statement:
Design a program for a car rental service that demonstrates the use of
composition and static data members. The program should manage details
about the rental cars, customers, and rental transactions. The classes involved
are as follows:
1. CarRentalService:
This class represents the rental service and includes information about the
company’s name and address. It contains a list of cars available for rent.
2. Car:
This class represents a rental car and includes attributes like model, license
plate number, and rental rate per day. Each Car should have a CarDetails
object for specific details about the car.
3. CarDetails:
This class provides specific information about each car, such as make, year,
and type (SUV, sedan, etc.). This class will be composed within Car.
4. Customer:
This class represents a customer renting a car, with attributes for name,
driver’s license number, and contact information.
5. Static Member:
Use a static data member in the CarRentalService class to keep track of the
total number of cars rented out by the service. Each time a rental transaction
occurs, this static member should be updated. Requirements:
• Use composition by including a CarDetails object within each Car.
• Add record of few cars using vector.
• Implement a method in the CarRentalService class to rent a car to a Customer.
When a car is rented, update the static member totalRentedCars.
• Provide methods to display the total number of cars rented out and show details
about each car and customer. Expected Output:
• Each time a rental transaction occurs, display the updated total number of cars
rented.
• Display details about the car rented, customer information, and car-specific
details using composition.

CODE:
#include <iostream>
#include <vector>
#include <string>

// CarDetails class
class CarDetails {
public:
std::string make;
int year;
std::string type;

CarDetails(const std::string& make, int year, const std::string& type)


: make(make), year(year), type(type) {}
};

// Car class
class Car {
public:
std::string model;
std::string licensePlate;
double rentalRatePerDay;
CarDetails details;
Car(const std::string& model, const std::string& licensePlate, double
rentalRatePerDay, const CarDetails& details)
: model(model), licensePlate(licensePlate),
rentalRatePerDay(rentalRatePerDay), details(details) {}
};

// Customer class
class Customer {
public:
std::string name;
std::string driversLicenseNumber;
std::string contactInfo;

Customer(const std::string& name, const std::string& driversLicenseNumber,


const std::string& contactInfo)
: name(name), driversLicenseNumber(driversLicenseNumber),
contactInfo(contactInfo) {}
};

// CarRentalService class
class CarRentalService {
public:
std::string companyName;
std::string address;
std::vector<Car> cars;
static int totalRentedCars;
CarRentalService(const std::string& companyName, const std::string& address)
: companyName(companyName), address(address) {}

void addCar(const Car& car) {


cars.push_back(car);
}

void rentCar(const Customer& customer, const std::string& licensePlate) {


for (auto& car : cars) {
if (car.licensePlate == licensePlate) {
totalRentedCars++;
std::cout << "Car rented to " << customer.name << ":\n";
std::cout << "Model: " << car.model << ", License Plate: " <<
car.licensePlate << "\n";
std::cout << "Make: " << car.details.make << ", Year: " <<
car.details.year << ", Type: " << car.details.type << "\n";
std::cout << "Total cars rented: " << totalRentedCars << "\n";
return;
}
}
std::cout << "Car with license plate " << licensePlate << " not found.\n";
}

void displayTotalRentedCars() const {


std::cout << "Total cars rented: " << totalRentedCars << "\n";
}
};

int CarRentalService::totalRentedCars = 0;

int main() {
CarRentalService service("Khan Rentals", "123 Main St");

CarDetails details1("Honda", 2000, "sedan");


Car car1("CIVIC ", "ACQ-491", 50.0, details1);
service.addCar(car1);

CarDetails details2("Honda", 2019, "SUV");


Car car2("charade", "Lou-4688", 60.0, details2);
service.addCar(car2);

Customer customer1("Huzaifa Ali Khan", "D1234567",


"[email protected]");
service.rentCar(customer1, "ACQ-491");

service.displayTotalRentedCars();

return 0;
}

You might also like