Object-Oriented Programming (OOP) is a paradigm that structures software into reusable "objects" that represent real-world entities. Key principles of OOP include encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation involves bundling data (attributes) and methods (functions) together and restricting access to some details. In Python, we use private attributes (prefixed with _
) to hide data from direct access, ensuring changes happen only through controlled methods.
Example:
class Ride:
def __init__(self, name, duration):
self._name = name # Private attribute
self._duration = duration # Private attribute
def get_details(self): # Public method
return f"Ride Name: {self._name}, Duration: {self._duration} minutes"
Polymorphism allows the same operation to behave differently for different classes or objects. This is often achieved by method overriding. Subclasses redefine methods from their parent class, tailoring behavior to their needs.
Example:
class Attraction:
def description(self):
return "A fun attraction at the theme park."
class RollerCoaster(Attraction):
def description(self): # Overriding parent method
return "A thrilling roller coaster ride with loops and drops!"
class FerrisWheel(Attraction):
def description(self): # Overriding parent method
return "A relaxing ferris wheel ride with scenic views."
Your task is to design a Theme Park Adventure System using OOP principles. The system will manage attractions, staff, and visitors, using encapsulation and polymorphism effectively.
A base class for all attractions at the theme park.
Attributes:
_name
(private): Name of the attraction._capacity
(private): Maximum number of people the attraction can hold.
Methods:
__init__(name, capacity)
: Initializes the attraction's name and capacity.get_details()
: Returns a string summarizing the attraction details:
"Attraction: [name], Capacity: [capacity]"
.start()
: A generic message,"The attraction is starting."
Represents high-adrenaline rides like roller coasters.
Attributes:
- Inherits all attributes from
Attraction
. _min_height
(private): Minimum height required to ride (in cm).
Methods:
__init__(name, capacity, min_height)
: Initializes all attributes, including the new one.start()
: Overridesstart()
to return:"Thrill Ride [name] is now starting. Hold on tight!"
is_eligible(height)
: Checks if a person meets the height requirement.
Represents family-friendly rides like carousels.
Attributes:
- Inherits all attributes from
Attraction
. _min_age
(private): Minimum age to ride.
Methods:
__init__(name, capacity, min_age)
: Initializes all attributes, including the new one.start()
: Overridesstart()
to return:"Family Ride [name] is now starting. Enjoy the fun!"
is_eligible(age)
: Checks if a person meets the age requirement.
A base class for all theme park staff.
Attributes:
_name
(private): Staff name._role
(private): Role of the staff member (e.g., "Operator").
Methods:
__init__(name, role)
: Initializes the staff details.work()
: A generic message,"Staff [name] is performing their role: [role]."
A class representing visitors at the theme park.
Attributes:
_name
(private): Visitor's name._age
(private): Visitor's age._height
(private): Visitor's height in cm.
Methods:
__init__(name, age, height)
: Initializes the visitor details.ride(attraction)
: Tries to ride an attraction. Checks eligibility using the appropriate method (is_eligible
) fromThrillRide
orFamilyRide
.
- Define the
Attraction
base class with appropriate encapsulation. - Define the
ThrillRide
andFamilyRide
subclasses, overriding thestart()
method and including specific eligibility checks. - Define the
Staff
andVisitor
classes, using encapsulation for all attributes.
- Create a
ThrillRide
object (e.g., "Dragon Coaster", capacity 20, min height 140 cm). - Create a
FamilyRide
object (e.g., "Merry-Go-Round", capacity 30, min age 4). - Create a
Visitor
object and attempt to ride both attractions, demonstrating eligibility checks.
- Create multiple attractions (e.g., a thrill ride and a family ride).
- Call the
start()
method on both objects and observe the behavior.
Develop a Park Management System with the following additional features:
Add a Manager
subclass to Staff
with attributes:
_team
(private): A list ofStaff
objects reporting to the manager.
Methods:
add_staff(staff)
: Adds aStaff
object to the manager's team.get_team_summary()
: Lists all staff names and roles in the team.
Add a status
attribute to Attraction
to track whether it is open or closed.
Methods:
open_attraction()
: Marks the attraction as "Open".close_attraction()
: Marks the attraction as "Closed".- Modify
start()
to check the status before starting the ride.
Enhance the Visitor
class with a ride_history
attribute to track the rides they’ve taken.
Methods:
view_history()
: Returns a list of rides the visitor has experienced.
Example Output:
visitor = Visitor("Alice", 10, 130)
coaster = ThrillRide("Dragon Coaster", 20, 140)
print(coaster.start()) # Output: "Thrill Ride Dragon Coaster is now starting. Hold on tight!"
visitor.ride(coaster) # Output: "Alice is not eligible for Dragon Coaster."
carousel = FamilyRide("Merry-Go-Round", 30, 4)
visitor.ride(carousel) # Output: "Alice is enjoying the Merry-Go-Round!"
visitor.view_history() # Output: ["Merry-Go-Round"]