object oriented design and classes. exercises 1.design a phone book program. your program should...

12
Object Oriented Design and Classes

Upload: alicia-snow

Post on 18-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Object Oriented Design and Classes

Page 2: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Exercises

1.Design a phone book program. Your

program should keep track of the name and

phone number of your contacts. You

should be able to add new contacts to your

phone book and view your phone book.

Page 3: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

What is a class?

• Programming mechanism to support OOD

• Data and the methods that operate on that

data – collectively called members

– Example: bank account class

• Provide structure for organizing programs

Page 4: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Methods

• Methods operate on data

– accessors – read data, provide access to data but

do not change it

– mutators – change data

• examples from bank account, zoo

– constructor – builds a new object

• An object in an instance of a class

– class is a blueprint for objects

Page 5: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

BankAccount Class

class BankAccount:

def __init__(self, initial_balance):

self.balance = initial_balance

def deposit(self, amount):

self.balance += amount

def withdraw(self, amount):

self.balance -= amount

def checkBalance(self):

print "Balance: ", self.balance

Page 6: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

BankAccount Class

def __init__(self, initial_balance):

self.balance = initial_balance

• __init__ method is the constructor

• Used to initialize members of the object

• First parameter is self – the object on which the

method/constructor is called

• Use dot notation (.) to reference members of the object

Page 7: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

BankAccount Class

def deposit(self, amount):

self.balance += amount

def withdraw(self, amount):

self.balance -= amount

• Deposit and withdraw are mutators

– they modify the data member balance

• First parameter is self

• Like functions, methods can take any number of parameters

Page 8: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

BankAccount Class

def checkBalance(self):

print "Balance: ", self.balance

• checkBalance does not modify data members

Page 9: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Creating and Using Objects

b = BankAccount(500)

//Name = Type(constructor parameters)

Page 10: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Creating and Using Objects

b = BankAccount(500)

//Name = Type(constructor parameters)

//how would you withdraw funds?

b.withdraw(500)

//object_name.method_name(params)

Page 11: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Composing Objects• An object can have a reference to another object

class Name:

def __init__(self, first, last):

self.first = first

self.last = last

def printName(self):

print "Name: ", self.first, " ", self.last

class Employee:

def __init__(self, first, last, pay):

self.name = Name(first, last)

self.pay = pay

def printEmployee(self):

self.name.printName()

print "\tPay rate: ", self.pay

Page 12: Object Oriented Design and Classes. Exercises 1.Design a phone book program. Your program should keep track of the name and phone number of your contacts

Exercises

• Implement your phone book program.