OOP_lab_activity_06
OOP_lab_activity_06
● 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.
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
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
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;
}
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;
}
};
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
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 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];
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
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
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:
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);
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class Trip {
private:
int tripID;
string destination;
double price;
bool isBooked;
public:
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);
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
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;
public:
void displayAccountDetails() {
cout << "\nAccount Number: " << accountNumber
<< "\nAccount Holder: " << accountHolder
<< "\nCurrent Balance: $" << balance << "\n\n";
}
};
int main() {
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
*****