Method inheritance
In this exercise, you'll extend the Manager
class (which is inherited from the Employee
class), created in the previous exercise, by creating a method called give_raise()
. It will be similar to the method of the same name in the Employee
class, but include an additional argument called bonus
.
The Manager
class you previously created has been provided for you in script.py
.
This exercise is part of the course
Introduction to Object-Oriented Programming in Python
Exercise instructions
- Add a
give_raise()
method toManager
that accepts the same parameters asEmployee.give_raise()
, plus abonus
argument with the default value of1.05
(bonus of 5%). - Within the method, calculate
new_amount
by multiplyingamount
bybonus
- Within the method, use the
Employee
's method to raise salary bynew_amount
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class Employee:
def __init__(self, name, salary=30000):
self.name = name
self.salary = salary
def give_raise(self, amount):
self.salary += amount
class Manager(Employee):
def display(self):
print("Manager ", self.name)
def __init__(self, name, salary=50000, project=None):
Employee.__init__(self, name, salary)
self.project = project
# Add a give_raise method
____:
____
____
mngr = Manager("Ashta Dunbar", 78500)
mngr.give_raise(2000, bonus=1.03)
print(mngr.salary)