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

Project

The document discusses object oriented programming concepts like classes, inheritance, polymorphism and templates. It describes a cars management system with classes like Car, ElectricCar, GasolineCar, CarLot and CarRental to demonstrate OOP concepts.

Uploaded by

Eman Rajput
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)
14 views

Project

The document discusses object oriented programming concepts like classes, inheritance, polymorphism and templates. It describes a cars management system with classes like Car, ElectricCar, GasolineCar, CarLot and CarRental to demonstrate OOP concepts.

Uploaded by

Eman Rajput
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

OBJECT ORIENTED PROGRAMMING

 Topic: Cars Management System

Object Oriented
Programming
(PROJECT)
Submitted to Respected Ma’am: Nosheen Manzoor

Submitted by: Sana Iqbal (F2022105195)


Eman Ali (F2022105118)
OBJECT ORIENTED PROGRAMMING

#include <iostream>
#include <string>

using namespace std;

class Car
{
protected:
string brand;
string model;

public:

Car(const string& brand, const string& model) : brand(brand), model(model) {}

string getBrand()
{
return brand;
}

string getModel()
{
return model;
}

virtual void display() const {


cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
}

virtual ~Car() {}
};

class ElectricCar : public Car {


private:
double batteryCapacity;

public:
ElectricCar(const string& brand, const string& model, double batteryCapacity)
: Car(brand, model), batteryCapacity(batteryCapacity) {}

void display() const override {


cout << "Electric Car" << endl;

Car::display();
cout << "Battery Capacity: " << batteryCapacity << " kWh" << endl;
OBJECT ORIENTED PROGRAMMING
}

~ElectricCar() {}
};

class GasolineCar : public Car {


private:
double mileage;

public:
GasolineCar(const string& brand, const string& model, double mileage)
: Car(brand, model), mileage(mileage) {}

void display() const override


{
cout << "Gasoline Car" << endl;
Car::display();
cout << "Mileage: " << mileage << " km/l" << endl;
}

~GasolineCar() {}
};

class CarLot
{
private:
Car** cars;
int numCars;
int capacity;

public:
CarLot(int capacity)
: numCars(0), capacity(capacity) {
cars = new Car*[capacity];
}

void addCar(Car* car) {


if (numCars < capacity) {
cars[numCars] = car;
numCars++;
} else {
cout << "Car lot is full. Cannot add more cars." << endl;
}

}
OBJECT ORIENTED PROGRAMMING
void displayAllCars() const {
for (int i = 0; i < numCars; i++) {
cars[i]->display();
cout<<"------------------------"<<endl;
}
}

Car* findCar(const string& brand, const string& model)


{
for (int i = 0; i < numCars; i++) {
if (cars[i]->getBrand() == brand && cars[i]->getModel() == model) {
return cars[i];
}
}

}
~CarLot() {
for (int i = 0; i < numCars; i++) {
delete cars[i];
}
delete[] cars;
}
};

template <typename T>


T getMaxValue(const T& a, const T& b) {
return (a > b) ? a : b;
}

class CarRental {
private:
string renterName;
Car* rentedCar;

public:
CarRental(const string& renterName, Car* rentedCar)
: renterName(renterName), rentedCar(rentedCar) {}

string getRenterName() const {


return renterName;
}

Car* getRentedCar() const {

return rentedCar;
}
OBJECT ORIENTED PROGRAMMING

friend ostream& operator<<(ostream& os, const CarRental& rental)


{
os << "Renter Name: " << rental.renterName << endl;
os << "Rented Car Details: " << endl;
rental.rentedCar->display();
return os;
}
~CarRental() {}
};

int main() {

CarLot carLot(4);

ElectricCar ec1("Tesla", "Model S",45.2 );


ElectricCar ec2("Nissan", "Leaf", 30.0);
GasolineCar gc1("Toyota", "Corolla", 15.0);
GasolineCar gc2("Honda", "Civic", 18.0);

carLot.addCar(&ec1);
carLot.addCar(&ec2);
carLot.addCar(&gc1);
carLot.addCar(&gc2);

cout << "All Cars in the Car Lot: " << endl;
carLot.displayAllCars();

string brand="Tesla";
string model="Model S";
Car* foundCar=carLot.findCar(brand, model);
if (foundCar)
{
cout<<"Found Car: "<<endl;
foundCar->display();
}
else
{
cout<<"Car not found."<<endl;
}

double maxValue = getMaxValue(10.5, 7.2);

cout<<"Maximum Value: "<<maxValue;


cout<<endl;
OBJECT ORIENTED PROGRAMMING
CarRental rental(" Abdullah ", &ec1);
cout << "Car Rental Details: " << endl;
cout<<rental;
cout<<endl;
}

You might also like