Docsity

Object-Oriented Programming Exercise by Leonard Bruns at KTH, Exams of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming Exercise by Leonard Bruns at KTH and more Exams Object Oriented Programming in PDF only on Docsity!

Exercise 6

Object-Oriented Programming

Leonard Bruns, [email protected] Robotics, Perception and Learning, KTH

Overview

 (^) Object oriented programming  (^) Classes, objects and instances  (^) Inheritance  (^) Polymorphism

 (^) Exercise / Example

Object-oriented Programming

Inheritance

 (^) Inheritance allows to model parent → child hierarchies  (^) Always ask, whether such a hierarchy is actually required or useful  (^) Inherent hierarchical structure  (^) Additional variables and/or more specific functions required for children  (^) Differences cannot easily be handled with a variable

Person

Customer

MaleEmployee FemaleEmployee

Employee

Object-oriented Programming

Inheritance

class Person () : def init ( self , name ) : self. name = name class Employee ( Person ) : def init ( self , name , salary ) : Person. init ( self , name ) self. salary = salary class Customer ( Person ) : def init ( self , name , purchases ) : Person. init ( self , name ) self. purchases = purchases

Person

Customer Employee

Object-oriented Programming

Polymorphism

 (^) Base class / parent often describes interface  (^) On a list of parent/child instances, we can use parent functions for all of them

persons = [] persons. append ( Person ( ' Peter ') ) persons. append ( Employee ( ' Ludvig ' , salary =30000) ) persons. append ( Customer ( ' Daniel ' , purchases =[]) )

for person in persons : person. print ()

Person

Customer Employee

Exercise

 (^) Design a zoo managing program