Lab 7 Lab 3c
Lab 7 Lab 3c
CL-217
Object Oriented Programming
Lab
Objectives:
• Classes and objects
• Data members
• Member function
• Data encapsulation
• Member access specifier (private , public , protected)
• Constructor, Destructor
• Copy Constructor
• This pointer
• Constant data members
• Inheritance
• Single Level inheritance
• Multi-level inheritance
• Multiple inheritance
• Diamond Problem
• Function Overriding
Note: Carefully read the following instructions (Each instruction contains a weightage)
1. There must be a block of comments at start of every question's code by students; the
block should contain brief description about functionality of code.
2. Comment on every function about its functionality.
3. Use understandable name of variables.
4. Proper indentation of code is essential
5. Write a C++ statement(s) for each of the following task one after the other, in the same
order.
6. Make a Microsoft Word file and paste all of your C++ code with all possible screenshots
of every task outputs in MS word and do not submit .cpp file with word file.
7. First think about statement problems and then write/draw your logic on copy.
8. After copy pencil work, code the problem statement on MS Studio C++ compiler.
9. At the end when you done your tasks, attached C++ created files in MS word file and make
your submission on Microsoft teams. (Make sure your submission is completed).
10. Please submit your file in this format 19F1234_L9.
11. Do not submit your assignment after deadline.
12. Do not copy code from any source otherwise you will be penalized with negative marks.
National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
Problem 1: | : (Inheritance)
You are developing a system to manage data for an online shopping platform. You need a class
UserData to store personal information about the users, and another class
PremiumCustomerData that is specific to premium customers. Premium customers have a
unique membership ID and the option to receive special offers via email.
Here’s the breakdown:
• Class UserData:
o Member variables:
▪ FirstName: User’s first name (string).
▪ LastName: User’s last name (string).
▪ Address: User’s home address (string).
o Getter and setter functions for these member variables.
• Class PremiumCustomerData (inherits from UserData):
o Member variables:
▪ MembershipID: A unique integer assigned to each premium customer.
▪ ReceiveSpecialOffers: A boolean that indicates whether the customer wants
to receive special offers via email (true or false).
o Getter and setter functions for the new member variables.
▪ InputCustomerData(): Collect all relevant data for a premium customer.
▪ DisplayCustomerData(): Display all the stored information for the premium
customer.
Scenario: You are developing a system to manage professionals in a tech consultancy. The system
tracks general individuals, employees working for the consultancy, freelancers, and tech business
owners.
Here’s the breakdown:
• Class Person:
o Attributes:
▪ Name: Represents the individual’s name (string).
▪ Age: Represents the individual’s age (integer).
▪ Gender: Represents the individual’s gender (string).
o This class serves as the base class.
• Class EmployedTechProfessional (inherits from Person):
o Inherits only Name from Person.
o Has an additional attribute:
Page 2
National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
▪ EmployeeID: A unique ID number for the employed tech professional
(integer).
o Member function WorkOnProject(): Outputs "Hi, I am an Employee from the
EmployedTechProfessional Class, working on a project".
• Class Freelancer (inherits from Person):
o Inherits only Name from Person.
o No additional attributes, but its constructor outputs "Hi, I am a Freelancer from the
Freelancer Class".
• Class TechEntrepreneur (inherits from EmployedTechProfessional):
o Inherits only EmployeeID from EmployedTechProfessional.
o Inherits the WorkOnProject() function.
o Has a Display() function that displays all attributes of the TechEntrepreneur.
Page 3
National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
You are tasked with designing a software system for a Healthcare Organization that handles various types of
individuals, including patients, doctors, and administrative staff. Each individual in the system will have some
common attributes and behaviors, but there are specific additional attributes and behaviors that differ based on
their role. You need to use multilevel inheritance to structure the classes effectively.
Requirements:
• Base Class: Person
o Represents a generic person in the healthcare system and contains common attributes and
behaviors.
o Attributes:
▪ Name (string)
▪ Age (int)
▪ Gender (string)
o Behaviors:
▪ Display() function to print all personal details.
• Derived Class: Patient (inherits from Person)
o Represents a patient and inherits common attributes and behaviors from Person.
o Additional Attributes:
▪ PatientID (int)
▪ Ailment (string)
▪ TreatmentPlan (string)
o Additional Behaviors:
▪ DisplayPatientDetails() function to print patient-specific details along with inherited
person details.
• Derived Class: InPatient (inherits from Patient)
o Represents an inpatient, which is a specific type of patient. It inherits from Patient.
o Additional Attributes:
▪ RoomNumber (int)
▪ AttendingDoctor (string)
o Additional Behaviors:
▪ DisplayInPatientDetails() function to print all details, including room number and
attending doctor.
• Derived Class: Doctor (inherits from Person)
o Represents a doctor and inherits common attributes and behaviors from Person.
o Additional Attributes:
▪ EmployeeID (int)
▪ Specialization (string)
▪ Salary (double)
o Additional Behaviors:
▪ DisplayDoctorDetails() function to print doctor-specific details along with inherited
person details.
Task:
1. Create the class hierarchy as described above using multilevel inheritance (Person -> Patient -> InPatient).
2. Create appropriate constructors for each class that initialize the attributes, including default constructors
for each.
3. Write setter and getter methods for the attributes of each class.
Page 4
National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
4. Implement the Display() function in the Person class to output basic information (name, age, gender), and
override this function in the Patient and InPatient classes to show relevant details.
5. In the main() function:
o Create an object of type InPatient, input values for its attributes, and call the
DisplayInPatientDetails() method to display all its details.
o Create an object of type Doctor, input values for its attributes, and call the DisplayDoctorDetails()
method to display all its details.
6. Bonus: Add a class AdministrativeStaff that inherits from Person and includes attributes like StaffID and
Department, along with appropriate methods.
You are tasked with designing a Vehicle Management System for a transportation company. In
addition to managing vehicles (cars, trucks, and electric cars), the system will also perform
calculations related to fuel consumption, rental cost, and electric vehicle efficiency. You need
to use multilevel inheritance to structure the classes effectively and add appropriate
calculations to each type of vehicle.
Requirements:
• Base Class: Vehicle
o Attributes:
▪ Make (string)
▪ Model (string)
▪ Year (int)
▪ BaseRentalCostPerDay (double)
o Behaviors:
▪ Display() function to print basic vehicle details.
▪ CalculateRentalCost() function to calculate the total rental cost based on
the number of days rented.
• Derived Class: Car (inherits from Vehicle)
o Additional Attributes:
▪ NumberOfDoors (int)
▪ FuelType (string)
▪ FuelConsumptionPer100Km (double) // Fuel consumption in liters per
100 km
o Additional Behaviors:
▪ CalculateFuelConsumption() function to calculate fuel consumed for a
given distance (in km).
▪ DisplayCarDetails() function to print car-specific details along with
inherited vehicle details.
Page 5
National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
• Derived Class: ElectricCar (inherits from Car)
o Additional Attributes:
▪ BatteryCapacity (double) // in kWh
▪ Range (int) // in kilometers
o Additional Behaviors:
▪ CalculateElectricEfficiency() function to calculate efficiency (kilometers
per kWh).
▪ DisplayElectricCarDetails() function to print all details, including the
efficiency.
• Derived Class: Truck (inherits from Vehicle)
o Additional Attributes:
▪ CargoCapacity (double) // in tons
▪ NumberOfAxles (int)
▪ FuelConsumptionPer100Km (double) // Fuel consumption in liters per
100 km
o Additional Behaviors:
▪ CalculateFuelConsumption() function for trucks to calculate fuel
consumed for a given distance.
▪ DisplayTruckDetails() function to print truck-specific details along with
inherited vehicle details.
Task:
1. Create the class hierarchy as described above using multilevel inheritance (Vehicle ->
Car -> ElectricCar and Vehicle -> Truck).
2. Create appropriate constructors for each class that initializes the attributes, including
default constructors.
3. Write setter and getter methods for all attributes.
4. Implement the following functions:
o CalculateRentalCost(int days) in Vehicle: This function calculates the rental cost
for a vehicle based on the number of days.
o CalculateFuelConsumption(double distance) in Car and Truck: This function
calculates the fuel consumed based on the distance driven and the fuel
consumption rate.
o CalculateElectricEfficiency() in ElectricCar: This function calculates how many
kilometers the electric car can travel per kWh of battery capacity.
5. In the main() function:
o Create an object of type ElectricCar, input values for its attributes, calculate the
electric efficiency and total rental cost for a specified number of days, and
Page 6
National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
display all the details.
o Create an object of type Truck, input values for its attributes, calculate the fuel
consumption for a specified distance, and display all the details.
Page 7