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

OOP_lab_activity_06

This document outlines a lab activity for CSE203 Object Oriented Programming, focusing on classes and objects in C++. It includes instructions for submission, topics covered, and detailed problem definitions requiring C++ programs for tasks such as a taxi fare calculator, person details, point representation, book management, rectangle area calculation, marks handling, and a car rental system. Each task is accompanied by example code and expected outputs.
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)
17 views

OOP_lab_activity_06

This document outlines a lab activity for CSE203 Object Oriented Programming, focusing on classes and objects in C++. It includes instructions for submission, topics covered, and detailed problem definitions requiring C++ programs for tasks such as a taxi fare calculator, person details, point representation, book management, rectangle area calculation, marks handling, and a car rental system. Each task is accompanied by example code and expected outputs.
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/ 27

CSE203 Object Oriented Programming

Lab Activity 6: Classes and Objects in C++

Instructions and Important Notes:

●​ Submit your lab activity solution on lms.ahduni.edu.in on or before February 25, 2025.
●​ A plagiarism check will be done for the code you submitted.
●​ Submit your file as: RollNo_LabActivity _No.pdf
●​ Submit a PDF file consisting of the source code and sample outputs. Any other format
will be rejected.

Topics covered: Classes and Objects

Roll No.AU2440032​ Name: Krishna Patel​ Section No.: _____

Write C++ programs for the following problem definitions:

1.​ Taxi Fare Calculator: Sheetal, a taxi service owner, wants a program to calculate taxi
fares. Implement a function to calculate the fare based on distance traveled and a
default rate per kilometer.

Inputs:
Enter the distance traveled (in km): 15

Output:
Base fare: 300
Total fare: 300

Constraints:
-​ Assume the default rate per kilometer is ₹20.
-​ Input validation: Ensure the distance and time values are within realistic limits.
====
Bonus Part: Enhance the program to take the time of the passenger's journey as
input. Surcharge gets applied in night ride. Based on the time, apply the following
surcharges to the fare.
8:00 PM to 10:00 PM: 3% surcharge
10:00 PM to 12:00 AM: 5% surcharge
12:00 AM to 7:00 AM: 10% surcharge
The program should then print the total fare the customer has to pay.

Inputs:
Enter the distance traveled (in km): ---
Enter the time of travel: ---

Output:
Base fare: ---
Surcharge: ---
Total fare: —

#include <iostream>
using namespace std;
double calculateFare(double d, double t)
{
double basefare = d * 20;
double surcharge;
if (t >= 20 && t < 22)
{
surcharge = 0.03 * basefare;
}
else if (t >= 22 && t < 24)
{
surcharge = 0.05 * basefare;
}
else if (t >= 0 && t < 7)
{
surcharge = 0.1 * basefare;
}
cout<<"\n";
cout << "Base fare: " << basefare << endl;
cout << "Surcharge: " << surcharge << endl;
return basefare + surcharge;
}
int main()
{
double d, t;
cout << "Enter the distance traveled (in km): ";
cin >> d;
cout << "Enter the time of travel in 24 hour format: ";
cin >> t;
double totalfare = calculateFare(d, t);
cout << "Total fare: " << totalfare << endl;
return 0;
}
DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 1.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe
Enter the distance traveled (in km): 13
Enter the time of travel in 24 hour format: 22

Base fare: 260


Surcharge: 13
Total fare: 273

2.​ Define a class Person with members: name and age. Include functions to input and
display Person details. Create an object of the class and demonstrate its usage.
#include <iostream>
using namespace std;
class person
{
private:
string n;
float a;
public:
void input(){
cout<<"Enter your name:";
getline(cin, n);
cout<<"Enter your age:";
cin>>a;
}
void display(){
cout<< "Name : "<< n <<endl;
cout<<"Age : "<<a<<endl;
}
};

int main(){
person a;
a.input();
a.display();
return 0;
}
DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 2.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe
Enter your name:Krishna patel
Enter your age:15
Name : Krishna patel
Age : 15

3.​ Define a class Point that represents a point in 2D space with members x and y
coordinates. Include methods to set the coordinates of the point and display the
coordinates. Demonstrate the usage of these methods by creating an object of the
class.

#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;

public:
void input()
{
cout << "Enter x coordinate: ";
cin >> x;
cout << "Enter y coordinate: ";
cin >> y;
}
void display()
{
cout << "\nX: " << x << "\nY: " << y << endl;
cout << "Point: (" << x << "," << y << ")" << endl;
}
};
int main()
{
Point p;
p.input();
p.display();
return 0;
}

DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 3.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe
Enter x coordinate: 23
Enter y coordinate: 20

X: 23
Y: 20
Point: (23,20)

4.​ Define a class Book with members: title, author, and price. Write a program to create
an array of Book objects and input and display details of multiple Books.
#include<iostream>
using namespace std;
class books{
private:
string title;
string author;
float price;
int n;
public:
void input(){
cout<<"Enter number of books you wanna enter:";
cin>> n;
cout<<"Enter following details for "<<n<<" books:";
cout<<"\n";
for (int i = 0; i < n; i++)
{
cin.ignore();
cout<<"Enter title of the book "<<i+1<<" :";
getline(cin, title);
cout<<"Enter author of the book "<<i+1<<" :";
getline(cin, author);
cout<<"Enter price of the book "<<i+1<<" :";
cin>>price;
cout<<"\n";
}

}
void display(){
cout<<"Details of the "<<n<<" books :";
cout<<"\n";
for (int i = 0; i < n; i++)
{
cout<<"Title (book "<<i+1<<") :"<<title<<endl;
cout<<"Author (book "<<i+1<<") :"<<author<<endl;
cout<<"Price (book "<<i+1<<") :"<<price<<endl;
cout<<"\n";
}

}
};
int main(){
books b;
b.input();
b.display();
return 0;
}

DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 4.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe
Enter number of books you wanna enter:3
Enter following details for 3 books:
Enter title of the book 1 :cheat sheet
Enter author of the book 1 :Krishna patel
Enter price of the book 1 :200

Enter title of the book 2 :I hope this doesn't find you


Enter author of the book 2 :Krishna patel
Enter price of the book 2 :838

Enter title of the book 3 :God of war


Enter author of the book 3 :Rina kent
Enter price of the book 3 :600
Details of the 3 books :
Title (book 1) :God of war
Author (book 1) :Rina kent
Price (book 1) :600

Title (book 2) :God of war


Author (book 2) :Rina kent
Price (book 2) :600

Title (book 3) :God of war


Author (book 3) :Rina kent
Price (book 3) :600

5.​ Define a class Rectangle with members: length and width. Write a program to create
an object of the class using a pointer and calculate the area of the rectangle.

#include <iostream>
using namespace std;
class Rectangle
{
private:
double length;
double width;

public:
void input()
{
cout << "Enter length of rectangle: ";
cin >> length;
cout << "Enter width of rectangle: ";
cin >> width;
}
void calculateArea()
{
double area = length * width;
cout << "Area of the rectangle: " << area << endl;
}
};
int main()
{
Rectangle r;
Rectangle *ptr = &r;
ptr->input();
ptr->calculateArea();
return 0;
}

DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 5.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe
Enter length of rectangle: 12
Enter width of rectangle: 13
Area of the rectangle: 156

6.​ Define a class Marks with an array member to store marks of five subjects. Include
functions to input and display the marks. Calculate the total marks and average.
#include <iostream>
using namespace std;
class marks
{
private:
double m[5];
public:
void input(){
cout<<"Enter marks for 5 subjects:"<<endl;

for (int i = 0; i < 5; i++)


{
cout<<"Enter marks for subject "<<i+1<<" :";
cin>>m[i];
}

}
double total(){
double s = 0;
for (int i = 0; i < 5; i++)
{
s += m[i];
}
return s;
}
double average(){
return total()/5;
}
void display(){
cout<<"Marks for 5 subjects:"<<endl;

for (int i = 0; i < 5; i++)


{
cout<<"Marks for subject "<<i+1<<" is "<<m[i]<<endl;
}
cout<<"\n";
cout<<"The total marks for the five subjects are
"<<total()<<endl;
cout<<"The average marks for the five subjects are "<<
average()<<endl;

}
};
int main(){
marks m;
m.input();
m.total();
m.average();
cout<<"\n";
m.display();
return 0;

DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 6.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe
Enter marks for 5 subjects:
Enter marks for subject 1 :99
Enter marks for subject 2 :98
Enter marks for subject 3 :97
Enter marks for subject 4 :96
Enter marks for subject 5 :95

Marks for 5 subjects:


Marks for subject 1 is 99
Marks for subject 2 is 98
Marks for subject 3 is 97
Marks for subject 4 is 96
Marks for subject 5 is 95

The total marks for the five subjects are 485


The average marks for the five subjects are 97

7.​ Car Rental System: Anil, a car rental service owner, needs a program to manage car
rentals. Define a class Car with members: car model, registration number, and rental
rate. Write functions to rent a car, return a car, and display the rental details. Use an
array of Car objects to manage multiple cars.

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

class Car
{
private:
string model;
string regNumber;
double rentalRate;
bool isRented;

public:
Car() : model(""), regNumber(""), rentalRate(0.0), isRented(false) {}

void setCarDetails(string mod, string reg, double rate)


{
model = mod;
regNumber = reg;
rentalRate = rate;
isRented = false;
}

void rentCar()
{
if (isRented)
{
cout << "Car " << model << " (" << regNumber << ") is already
rented.\n";
}
else
{
isRented = true;
cout << "Car " << model << " (" << regNumber << ") has been
rented successfully.\n";
}
}

void returnCar()
{
if (!isRented)
{
cout << "Car " << model << " (" << regNumber << ") is not
currently rented.\n";
}
else
{
isRented = false;
cout << "Car " << model << " (" << regNumber << ") has been
returned successfully.\n";
}
}

void displayDetails()
{
cout << "Model: " << model << "\n"
<< "Registration Number: " << regNumber << "\n"
<< "Rental Rate: $" << rentalRate << " per day\n"
<< "Status: " << (isRented ? "Rented" : "Available") <<
"\n\n";
}

bool isAvailable()
{
return !isRented;
}
};
int main()
{
const int numCars = 3;
Car cars[numCars];

cars[0].setCarDetails("Audi", "ABC123", 40.0);


cars[1].setCarDetails("BMW", "XYZ456", 45.0);
cars[2].setCarDetails("Ferrai", "LMN789", 50.0);

int choice;
do
{
cout << "\n===== Car Rental System =====\n";
cout << "1. Display Available Cars\n";
cout << "2. Rent a Car\n";
cout << "3. Return a Car\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "\nAvailable Cars:\n";
for (int i = 0; i < numCars; i++)
{
cars[i].displayDetails();
}
break;

case 2:
{
cout << "\nEnter the registration number of the car to rent:
";
string regNum;
cin >> regNum;
bool found = false;
for (int i = 0; i < numCars; i++)
{
if (cars[i].isAvailable() && cars[i].isAvailable())
{
cars[i].rentCar();
found = true;
break;
}
}
if (!found)
{
cout << "Car not found or already rented.\n";
}
break;
}

case 3:
{
cout << "\nEnter the registration number of the car to return:
";
string regNum;
cin >> regNum;
bool found = false;
for (int i = 0; i < numCars; i++)
{
if (!cars[i].isAvailable())
{
cars[i].returnCar();
found = true;
break;
}
}
if (!found)
{
cout << "Car not found or is not rented.\n";
}
break;
}

case 4:
cout << "Exiting the Car Rental System. Thank you!\n";
break;
default:
cout << "Invalid choice! Please enter a valid option.\n";
}
} while (choice != 4);

return 0;
}

DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 7.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe

===== Car Rental System =====


1. Display Available Cars
2. Rent a Car
3. Return a Car
4. Exit
Enter your choice: 1

Available Cars:
Model: Audi
Registration Number: ABC123
Rental Rate: $40 per day
Status: Available

Model: BMW
Registration Number: XYZ456
Rental Rate: $45 per day
Status: Available

Model: Ferrai
Registration Number: LMN789
Rental Rate: $50 per day
Status: Available

===== Car Rental System =====


1. Display Available Cars
2. Rent a Car
3. Return a Car
4. Exit
Enter your choice: 2

Enter the registration number of the car to rent: LMN789


Car Audi (ABC123) has been rented successfully.

===== Car Rental System =====


1. Display Available Cars
2. Rent a Car
3. Return a Car
4. Exit
Enter your choice: 4
Exiting the Car Rental System. Thank you!

8.​ Travel Agency Management: Ashok, a travel agent, needs a program to manage trip
bookings. Define a class Trip with members: trip ID, destination, and price. Include
methods to book a trip, cancel a booking, and display trip details. Use pointers for
dynamic memory allocation of trip objects.

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

class Trip {
private:
int tripID;
string destination;
double price;
bool isBooked;

public:

Trip(int id, string dest, double cost) : tripID(id),


destination(dest), price(cost), isBooked(false) {}

void bookTrip() {
if (isBooked) {
cout << "Trip to " << destination << " (ID: " << tripID << ")
is already booked.\n";
} else {
isBooked = true;
cout << "Trip to " << destination << " (ID: " << tripID << ")
has been booked successfully.\n";
}
}

void cancelTrip() {
if (!isBooked) {
cout << "Trip to " << destination << " (ID: " << tripID << ")
is not booked.\n";
} else {
isBooked = false;
cout << "Trip to " << destination << " (ID: " << tripID << ")
has been canceled.\n";
}
}

void displayTrip() {
cout << "Trip ID: " << tripID << "\nDestination: " << destination
<< "\nPrice: $" << price
<< "\nStatus: " << (isBooked ? "Booked" : "Available") <<
"\n\n";
}

int getTripID() {
return tripID;
}
};

int main() {
vector<Trip*> trips;
int choice;

do {
cout << "\n===== Travel Agency Management =====\n";
cout << "1. Add a Trip\n";
cout << "2. Book a Trip\n";
cout << "3. Cancel a Trip\n";
cout << "4. Display Trips\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
int id;
string destination;
double price;
cout << "Enter Trip ID: ";
cin >> id;
cout << "Enter Destination: ";
cin.ignore();
getline(cin, destination);
cout << "Enter Price: ";
cin >> price;
trips.push_back(new Trip(id, destination, price));
cout << "Trip added successfully!\n";
break;
}

case 2: {
int id;
cout << "Enter Trip ID to book: ";
cin >> id;
bool found = false;
for (Trip* trip : trips) {
if (trip->getTripID() == id) {
trip->bookTrip();
found = true;
break;
}
}
if (!found) cout << "Trip not found!\n";
break;
}

case 3: {
int id;
cout << "Enter Trip ID to cancel: ";
cin >> id;
bool found = false;
for (Trip* trip : trips) {
if (trip->getTripID() == id) {
trip->cancelTrip();
found = true;
break;
}
}
if (!found) cout << "Trip not found!\n";
break;
}

case 4:
cout << "\nAvailable Trips:\n";
for (Trip* trip : trips) {
trip->displayTrip();
}
break;

case 5:
cout << "Exiting Travel Agency Management System. Thank
you!\n";
break;

default:
cout << "Invalid choice! Please enter a valid option.\n";
}
} while (choice != 5);

for (Trip* trip : trips) {


delete trip;
}
trips.clear();

return 0;
}


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

class Trip {
private:
int tripID;
string destination;
double price;
bool isBooked;

public:

Trip(int id, string dest, double cost) : tripID(id),


destination(dest), price(cost), isBooked(false) {}

void bookTrip() {
if (isBooked) {
cout << "Trip to " << destination << " (ID: " << tripID << ")
is already booked.\n";
} else {
isBooked = true;
cout << "Trip to " << destination << " (ID: " << tripID << ")
has been booked successfully.\n";
}
}

void cancelTrip() {
if (!isBooked) {
cout << "Trip to " << destination << " (ID: " << tripID << ")
is not booked.\n";
} else {
isBooked = false;
cout << "Trip to " << destination << " (ID: " << tripID << ")
has been canceled.\n";
}
}

void displayTrip() {
cout << "Trip ID: " << tripID << "\nDestination: " << destination
<< "\nPrice: $" << price
<< "\nStatus: " << (isBooked ? "Booked" : "Available") <<
"\n\n";
}

int getTripID() {
return tripID;
}
};

int main() {
vector<Trip*> trips;
int choice;

do {
cout << "\n===== Travel Agency Management =====\n";
cout << "1. Add a Trip\n";
cout << "2. Book a Trip\n";
cout << "3. Cancel a Trip\n";
cout << "4. Display Trips\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
int id;
string destination;
double price;
cout << "Enter Trip ID: ";
cin >> id;
cout << "Enter Destination: ";
cin.ignore();
getline(cin, destination);
cout << "Enter Price: ";
cin >> price;
trips.push_back(new Trip(id, destination, price));
cout << "Trip added successfully!\n";
break;
}

case 2: {
int id;
cout << "Enter Trip ID to book: ";
cin >> id;
bool found = false;
for (Trip* trip : trips) {
if (trip->getTripID() == id) {
trip->bookTrip();
found = true;
break;
}
}
if (!found) cout << "Trip not found!\n";
break;
}

case 3: {
int id;
cout << "Enter Trip ID to cancel: ";
cin >> id;
bool found = false;
for (Trip* trip : trips) {
if (trip->getTripID() == id) {
trip->cancelTrip();
found = true;
break;
}
}
if (!found) cout << "Trip not found!\n";
break;
}

case 4:
cout << "\nAvailable Trips:\n";
for (Trip* trip : trips) {
trip->displayTrip();
}
break;
case 5:
cout << "Exiting Travel Agency Management System. Thank
you!\n";
break;

default:
cout << "Invalid choice! Please enter a valid option.\n";
}
} while (choice != 5);

for (Trip* trip : trips) {


delete trip;
}
trips.clear();

return 0;
}

DISPLAY
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 8.cpp
PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe

===== Travel Agency Management =====


1. Add a Trip
2. Book a Trip
3. Cancel a Trip
4. Display Trips
5. Exit
Enter your choice: 1
Enter Trip ID: 123
Enter Destination: Goa
Enter Price: 13000
Trip added successfully!

===== Travel Agency Management =====


1. Add a Trip
2. Book a Trip
3. Cancel a Trip
4. Display Trips
5. Exit
Enter your choice: 1
Enter Trip ID: 567
Enter Destination: Sikkim
Enter Price: 16000
Trip added successfully!

===== Travel Agency Management =====


1. Add a Trip
2. Book a Trip
3. Cancel a Trip
4. Display Trips
5. Exit
Enter your choice: 2
Enter Trip ID to book: 123
Trip to Goa (ID: 123) has been booked successfully.

===== Travel Agency Management =====


1. Add a Trip
2. Book a Trip
3. Cancel a Trip
4. Display Trips
5. Exit
Enter your choice: 5
Exiting Travel Agency Management System. Thank you!

9.​ Define a class BankAccount with private members: account number, account holder
name, and balance. Include public functions to deposit and withdraw money. Ensure
proper validation in private functions.
#include <iostream>
#include <string>
using namespace std;

class BankAccount {
private:
string accountNumber;
string accountHolder;
double balance;

bool validateWithdrawal(double amount) {


if (amount > balance) {
cout << "Insufficient balance! Withdrawal failed.\n";
return false;
}
return true;
}

bool validateDeposit(double amount) {


if (amount <= 0) {
cout << "Invalid deposit amount! Must be greater than
zero.\n";
return false;
}
return true;
}

public:

BankAccount(string accNum, string holder, double initialBalance) {


accountNumber = accNum;
accountHolder = holder;
balance = (initialBalance >= 0) ? initialBalance : 0;
}

void deposit(double amount) {


if (validateDeposit(amount)) {
balance += amount;
cout << "Deposit successful! New balance: $" << balance <<
"\n";
}
}

void withdraw(double amount) {


if (validateWithdrawal(amount)) {
balance -= amount;
cout << "Withdrawal successful! Remaining balance: $" <<
balance << "\n";
}
}

void displayAccountDetails() {
cout << "\nAccount Number: " << accountNumber
<< "\nAccount Holder: " << accountHolder
<< "\nCurrent Balance: $" << balance << "\n\n";
}
};

int main() {

BankAccount myAccount("987654321", "KRISHNA PATEL", 1000.0);

int choice;
double amount;

do {
cout << "\n===== Bank Account Menu =====\n";
cout << "1. Deposit Money\n";
cout << "2. Withdraw Money\n";
cout << "3. View Account Details\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter amount to deposit: $";
cin >> amount;
myAccount.deposit(amount);
break;

case 2:
cout << "Enter amount to withdraw: $";
cin >> amount;
myAccount.withdraw(amount);
break;

case 3:
myAccount.displayAccountDetails();
break;

case 4:
cout << "Thank you for using our banking system!\n";
break;

default:
cout << "Invalid choice! Please enter a valid option.\n";
}
} while (choice != 4);

return 0;
}


DISPLAY

PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> g++ 9.cpp


PS C:\Users\jdgtk\OneDrive\Desktop\for Cpp\session_4> .\a.exe

===== Bank Account Menu =====


1. Deposit Money
2. Withdraw Money
3. View Account Details
4. Exit
Enter your choice: 1
Enter amount to deposit: $12000
Deposit successful! New balance: $13000

===== Bank Account Menu =====


1. Deposit Money
2. Withdraw Money
3. View Account Details
4. Exit
Enter your choice: 2
Enter amount to withdraw: $600
Withdrawal successful! Remaining balance: $12400

===== Bank Account Menu =====


1. Deposit Money
2. Withdraw Money
3. View Account Details
4. Exit
Enter your choice: 3

Account Number: 987654321


Account Holder: KRISHNA PATEL
Current Balance: $12400

===== Bank Account Menu =====


1. Deposit Money
2. Withdraw Money
3. View Account Details
4. Exit
Enter your choice: 4
Thank you for using our banking system!

*****

You might also like