Year 1 Computer Programming - Lecture 7
Year 1 Computer Programming - Lecture 7
Computer Programming
Lecture – 7
(Based on Gaddis, T., Starting out with Python 3e, Pearson Education)
Inheritance
Rectangle
height: float
width: float
get_perim():float
is_square():boolean
get_width():float
get_length():float
get_area():float
class Rectangle:
def __init__(self, h, w):
self.__height = h
self.__width = w
6-10
class Rectangle:
def __init__(self, h, w):
self.__height = h
self.__width = w
def get_width(self):
return self.__width
def get_height(self):
return self.__height
class Rectangle:
def __init__(self, h, w):
self.__height = h
self.__width = w
def perim(self):
return (2 * self.__height) + (2 * self.__width)
def area(self):
return self.__height * self.__width
def isSquare(self):
return (self.__height == self.__width)
def get_width(self):
return self.__width
def get_height(self):
return self.__height
box = Rectangle(3,5)
A Rectangle object
The box
variable holds height: 3
the address of address
the Rectangle width: 5
object.
box.set_height(10.0)
6-19
box = Rectangle(3,5)
print ("Perimitar value is %.2f" %box.perim())
print ("Rectangle total area is %.2f" %box.area())
print (box.is_square())
print ("Rectangle height %.2f and width %.2f" %(box.get_height(),
box.get_width()))
box.set_height(9)
Using mutator methods to change
box.set_width(9)
print ("Perimitar value is %.2f" %box.perim())
the instance variables (height and
print ("Rectangle total area is %.2f" %box.area())
width)
print ("is it a Square? : ", box.is_square())
print ("Rectangle height %.2f and width %.2f"
%(box.get_height(), box.get_width()))
class Salary:
• Salary Class is
def __init__(self, hrs, rate): independent
self.monthly_worked_hours = hrs
self.pay_rate = rate • Employee Class is
independent, but needs
def get_annual_salary(self): Salary when calculating
return (self.monthly_worked_hours * self.pay_rate * 12) employee salary with bonus
class Employee:
def __init__(self, na, add, bon):
self.name = na
self.address = add
self.bonus = bon
obj_sal = Salary(40, 10.50) # 40 hours per month, and the pay rate is £10.50
obj_emp=Employee("Thomas", "50 Curzon St.", 2500)
print (obj_emp.annual_salary_with_bonus(obj_sal.get_annual_salary()))
class Salary:
def __init__(self, hrs, rate): • Salary Class is
independent
self.monthly_worked_hours = hrs
self.pay_rate = rate
• Employee needs Salary to
exist as its part of its
def get_annual_salary(self): __init__ parameters.
return (self.monthly_worked_hours * self.pay_rate * 12)
class Employee:
def __init__(self,na, add, bon, salary):
self.name = na
self.address = add
self.bonus = bon
self.obj_salary = salary
def annual_salary_with_bonus(self):
return "Total: " + str(self.obj_salary.get_annual_salary() + self.bonus)
obj_sal = Salary(40, 10.50) # 40 hours per month, and the pay rate is £10.50
obj_emp = Employee("Thomas", "50 Curzon St.", 2500, obj_sal)
print (obj_emp.annual_salary_with_bonus())
class Employee:
def __init__(self, na, add, bon, hrs, rate):
self.name=na
self.address = add
self.bonus=bon
self.obj_salary = Salary(hrs, rate)
def annual_salary_with_bonus(self):
return "Total: " + str(self.obj_salary.get_annual_salary() + self.bonus)
In the real world, many objects are a specialized version of more general
objects
– Example: grasshoppers and bees are specialized types of insect
- In addition to the general insect characteristics, they have unique
characteristics:
- Grasshoppers can jump
- Bees can sting, make honey, and build hives
We can specify this feature by using a relationship between the objects
which is known as “Is a” relationship.
“Is a” relationship: exists when one object is a specialized version of
another object
– Specialized object has all the characteristics of the general object plus
unique characteristics
– Example: Rectangle is a shape
Daisy is a flower
CMP 4266, School of CS & DT, Birmingham City University.
Inheritance
For example, need to create classes for cars and pickup trucks
In addition:
– Car has a number of doors
– Pickup truck has a drive type
Output
Person Employee
???
???
Class variable
– declared inside a class but outside of any method
– also known as static variable
– can be accessed without creating an instance of the class
– shared by all instances of the class
Instance variable
– declared within a method preceded by the self keyword
– each instance of the class can have unique value for the instance variable