Day - 14 Assignment solutions
Day - 14 Assignment solutions
##OOPS
"""
Object-Oriented Programming (OOP) is a programming paradigm that organizes
software design around objects rather than functions and logic. Objects represent
real-
world entities, encapsulating data (attributes) and behavior (methods) into a single
unit
"""
## Task2
class Employee:
def __init__(self, emp_id, name, department, salary):
self.emp_id = emp_id
self.name = name
self.department = department
self.salary = salary
def __str__(self):
return f"Employee[ID: {self.emp_id}, Name: {self.name}, Department:
{self.department}, Salary: {self.salary}]"
"""
#Benefits of the OOP Approach
Encapsulation: The Employee class groups all relevant data and behavior,
improving data integrity and modularity.
Scalability: We can extend the Employee class with new attributes and methods
(e.g., calculate_bonus(), update_salary()).
Abstraction: Users of the Employee class do not need to know its internal
implementation, just how to interact with it.
"""
"""
The __str__() method provides a meaningful string representation when printing an
Employee object, making debugging and logging easier.
#Task3
class Employee:
company_name = "TechCorp" # Class Variable
@staticmethod
def company_policy():
return "Follow ethical practices" # Static Variable
# Creating Instances
emp1 = Employee("Alice", 25)
emp2 = Employee("Bob", 30)
print(emp1.name)
print(emp2.company_name)
print(Employee.company_policy())
class BankAccount:
def __init__(self, initial_balance=0):
self.__balance = initial_balance
def check_balance(self):
return f"Current Balance: {self.__balance}"
# Testing the BankAccount class
account = BankAccount(1000)
account.deposit(500)
account.withdraw(300)
print(account.check_balance())
class School:
school_name = "Greenwood High" # Class Variable
@classmethod
def change_school(cls, new_name):
cls.school_name = new_name # Modifies class variable
@staticmethod
def school_motto():
return "Education for All"