Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

semester project oop

The document outlines a project for a 3rd semester Object Oriented Programming course, focusing on designing a real-time navigation system for autonomous vehicles using C++. It includes the implementation of classes for Vehicle, Road, and Intersection, along with specific functions for managing vehicle data and road information. Additionally, it describes a banking system with an abstract Account class and derived classes for Current and Savings Accounts, emphasizing the use of inheritance and polymorphism.

Uploaded by

hasimalikym15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

semester project oop

The document outlines a project for a 3rd semester Object Oriented Programming course, focusing on designing a real-time navigation system for autonomous vehicles using C++. It includes the implementation of classes for Vehicle, Road, and Intersection, along with specific functions for managing vehicle data and road information. Additionally, it describes a banking system with an abstract Account class and derived classes for Current and Savings Accounts, emphasizing the use of inheritance and polymorphism.

Uploaded by

hasimalikym15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Project No : Semester

Course title : Object Oriented Programming

Student name : Hasnain Malik


Registration no : BCS242046
Semester : 3rd
Lab Instructor: SIR ALI RAZ
Submission Date: 22/Jan/2025
Q1: You are tasked with designing a real-time navigation system for autonomous vehicles in
C++. This system must handle vehicles to efficiently manage intersections and the roads
connecting them.
1. Implement a class Vehicle that stores the vehicle's No (string), speed (float),
company(string) and status (active/inactive) (bool).
2. Implement a class Road that stores the road's Name (string), lengthInMeters (float),
and dynamic array of vehicles.
3. Implement a class Intersection that stores the intersection's ID (string) and a
dynamic array of roads ('Road' objects) connecting to it.
Write a C++ code whether you would use composition or aggregation for the relationships
between Vehicle, Intersection and Road based on your chosen associations.
Vehicle Class:
1) Add a copy constructor and destructors if required, think on it.
2) addVehicle(const Vehicle& v1)
3) removeVehicle(const std::string& id)
4) Implement a function to keep track of the total number of active vehicles and print
them.
Road Class:
1) calculateAverageSpeed() const: that calculates the average speed of all active vehicles on the
road.
2) highSpeed() that finds and returns the vehicle having maximum speed than others.
Note: use pointer notation in the above two functions (Half marks for array notation).
Intersection Class:
1) Implement add Road (const Road& amp;)
2) Implement sort Roads () const that returns a sorted list of all roads based on road length.
Solution:
#include <iostream>

#include <string>

using namespace std;

// Class Car

class Car {

public:

string no, company;

double speed;

bool status;

// Parameterized Constructor

Car(const string& no, const string& company, double speed, bool

status)

: no(no), company(company), speed(speed), status(status) {}

// Destructor

~Car() {}

// Display Car Info

void displayCarInfo() const {

cout << "Car No: " << no << ", Company: " << company

<< ", Speed: " << speed << " km/h, Status: "

<< (status ? "Active" : "Inactive") << endl;

// Avg Speed

static double calculateAverageSpeed(const Car& car1, const Car& car2)

return (car1.speed + car2.speed) / 2;

}
// Compare speed

static void compareSpeed(const Car& car1, const Car& car2) {

if (car1.speed > car2.speed) {

cout << car1.no << " has the highest speed: " << car1.speed <<

" km/h" << endl;

} else if (car1.speed < car2.speed) {

cout << car2.no << " has the highest speed: " << car2.speed <<

" km/h" << endl;

} else {

cout << "Both cars have the same speed: " << car1.speed << "km/h" << endl;

};

// Class Road

class Road {

public:

double* lengths;

int numRoads;

// Road Constructor

Road(int roadCount) : numRoads(roadCount) {

lengths = new double[numRoads];

// Destructor

~Road() {

delete[] lengths;

// Length Function

void setRoadLengths(const double* roadLengths) {

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


lengths[i] = roadLengths[i];

// No of roads

void displayRoadInfo() const {

cout << "Road lengths: ";

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

cout << lengths[i] << " ";

cout << endl;

// Sorting Roads

void findLongestRoad() const {

double maxLength = lengths[0];

for (int i = 1; i < numRoads; i++) {

if (lengths[i] > maxLength) {

maxLength = lengths[i];

cout << "The longest road is: " << maxLength << " meters" << endl;

// Function to detect the number of active cars on the road

int countActiveCars(const Car* cars, int numCars) const {

int activeCount = 0;

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

if (cars[i].status) {

activeCount++;

return activeCount;
}

};

// Class Intersection

class Intersection : public Road {

public:

string id;

// Constructor

Intersection(int roadCount, const string& intersectionID)

: Road(roadCount), id(intersectionID) {}

// Intersection Id

void displayIntersectionInfo() const {

cout << "Intersection ID: " << id << endl;

displayRoadInfo();

};

int main() {

// Define two cars

Car car1("A123", "Toyota", 120.5, true);

Car car2("B456", "Honda", 130.0, true);

// Display car information

car1.displayCarInfo();

car2.displayCarInfo();

// Calculate and display average speed

double avgSpeed = Car::calculateAverageSpeed(car1, car2);

cout << "Average speed of the two cars: " << avgSpeed << " km/h" <<

endl;

// Compare and display the highest speed

Car::compareSpeed(car1, car2);

// Define roads for the intersection

int roadCount = 3;
double roadLengths[] = {150.5, 200.3, 180.8};

// Create an Intersection object

Intersection intersection(roadCount, "Intersection1");

// Set road lengths and display road info

intersection.setRoadLengths(roadLengths);

intersection.displayIntersectionInfo();

intersection.findLongestRoad();

// Count and display the number of active cars

Car cars[] = {car1, car2};

int numActiveCars = intersection.countActiveCars(cars, 2);

cout << "Number of active cars on the road: " << numActiveCars <<

endl;

return 0;

Output:

Q2: Capital trust Bank needs to implement an account management system that can handle
various types of bank accounts, specifically Current Accounts and Savings Accounts. The
system should be designed using object-oriented principles, leveraging inheritance and
polymorphism to manage account operations effectively. You are tasked with designing and
implementing this system with the following requirements:
1. Create an abstract class called Account having data members:
▸ Private Account Number and Account Balance.
▸ Add suitable setter/getter for data.
▸ Add Debit(float), Credit(float) as member functions (Pure Virtual).
▸ Add Print () function (Virtual) o Override Print Debit and Credit functions according to
derived classes.
2. Create a class called CurrentAccount i-e: CurrentAccount(is-a) Account having data
members:
 Service Charges (To be charged during credit if account balance is less than min
balance)
 Minimum Balance

Override print() as created in above class which displays:


 Account Number, Account Balance, Minimum Balance, Service Charges
 Modify the definition of the print() so that it displays a suitable message containing
above info.
 Similarly override credit(float), debit(float) functions such that credit(float) simply a
amount to the Account Balance and debit(float) checks if the amount to be debited
within the range of Account Balance, and further if the amount is account balance
less than min balance standard charges would also be deducted.
3. Create a class called SavingAccount i-e: SavingAccount (is-a) Account having data members:
Interest Rate.
 Override print() as created in parent class which displays:
 Account Number, Account Balance, Interest Rate.

 Modify the definition of the print() so that it displays a suitable message containing above
info.
 Similarly override credit(float), debit(float) functions such that credit(float) simply add
amount to the Account Balance and debit(float) checks if the amount to be debited is within
the range of Account Balance.
4. In main function:
Create an array of Account type Pointers, of size 3.
 Assign Account object to index0, CurrentAccount object to index 1 and SavingAccount object
to index 2.
 Have you encountered any problem? Report the problem and change the size and
elements of the array accordingly
Now Credit and Debit the CurrentAccount and SavingAccount in main function
Although things seem to be fine on the surface, there is a problem in the program we just
wrote. To observe this problem, we must add destructors for all classes. Paste the following
inline definitions of the destructors in their corresponding classes, execute the program and
paste the output below. Your destructors should delete the child classes first before
destroying parent class. There should not be any memory leaks.
Solution:

#include<iostream>

#include<string>

using namespace std;


class Account{

public:

int pan;

double balance;

Account(){}

~Account(){}

virtual void get()=0;

virtual void set()=0;

virtual double debit()=0;

virtual double cr()=0;

virtual void print()=0;

};

class cra:public Account{

public:

const double servicecharges=1200;

const double minbalance=100;

int A;

cra(){

this->pan=pan;

this->balance=balance;

this->A=A;

this->minbalance;

this->servicecharges;

void get(){

cout<<"Enter your account no : "<<endl;

cin>>pan;
cout<<"Enter your balance of account :"<<endl;

cin>>balance;

void set(){

this->pan;

this->balance;

cout<<"Your ac no:"<<pan<<endl;

cout<<"it has balance :"<<balance<<endl;

void print(){

if(balance>minbalance||balance==minbalance){

else {

cout<<"Due to low balance you have been surcharged servicefee"<<""<<"of amount


"<<servicecharges<<endl;

double debit(){

cout<<"Enter amount you want to withdraw : "<<endl;

cin>>A;

if(A>minbalance||A==minbalance){

cout<<"You have withdrawn : "<<A<<endl;

cout<<"The remaing balance is :"<<balance-A;

else {
cout<<"You have low balance :"<<endl;

};

double cr(){

cout<<"Enter amount you want to credit : "<<endl;

cin>>A;

cout<<"You have deposite : "<<A<<endl;

cout<<"The remaing balance is :"<<balance+A;

~cra(){}

};

class sva:public Account{

public:

string irate="25 %";

const double servicecharges=1200;

const double minbalance=100;

int A;

sva(){

this->pan=pan;

this->balance=balance;

this->A=A;

this->minbalance;

this->servicecharges;

void get(){

this->pan;
this->balance;

cout<<"Enter your account no : "<<endl;

cin>>pan;

cout<<"Enter your balance of account :"<<endl;

cin>>balance;

void set(){

cout<<"Your ac no:"<<pan<<endl;

cout<<"it has balance :"<<balance<<endl;

cout<<"Your intresr rate on account is 25% :"<<endl;

void print(){

if(balance>minbalance||balance==minbalance){

else {

cout<<"Due to low balance you have been surcharged servicefee"<<""<<"of amount


"<<servicecharges<<endl;

double debit(){

cout<<"Enter amount you want to withdraw : "<<endl;

cin>>A;

if(A<balance||A==balance){
cout<<"You have withdrawn : "<<A<<endl;

cout<<"The remaing balance is :"<<balance-A;

else {

cout<<"You have low balance :"<<endl;

cout<<balance-servicecharges;

};

double cr(){

cout<<"Enter amount you want to credit : "<<endl;

cin>>A;

cout<<"You have deposite : "<<A<<endl;

cout<<"The remaing balance is :"<<balance+A;

~sva(){}

};

int main(){

Account *v;

Account*v1=new cra();

Account *v2=new sva();

Account*arr[3]={v,v1,v2};

//

//arr[1]->get();

// arr[1]->set();
// arr[1]->print();

// arr[1]->debit();

// delete arr[1];

arr[2]->get();

arr[2]->set();

arr[2]->print();

arr[2]->cr();

arr[2]->debit();

delete arr[2];

delete arr[0];

return 0;

Output:

You might also like