Introduction to Computer Programming Chapter 9: Object Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Computer Programming Chapter 9: Object Oriented Programming

Description:

ICP: Chapter 9: Object Oriented Programming. 2. Contents ... Blackjack Game. Example: Blackjack Game. Imports: Cards, Games. November 28, 2005 ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 25
Provided by: csK4
Learn more at: https://www.cs.kent.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computer Programming Chapter 9: Object Oriented Programming


1
Introduction to Computer Programming Chapter 9
Object Oriented Programming
  • Michael Scherger
  • Department of Computer Science
  • Kent State University

2
Contents
  • Creating objects of different classes in the same
    program
  • Allow objects to communicate with each other
  • Combining classes
  • Inheritance
  • Overriding method definitions

3
Blackjack Game
  • Example Blackjack Game
  • Imports Cards, Games

4
Sending and Receiving Messages
  • Sending a Message
  • When one object tells another object to perform
    some action or function
  • hero Player()
  • invader Alien()
  • def hero.blast(self, enemy)
  • enemy.die()

5
Sending and Receiving Messages
  • Receiving a Message
  • The action of executing the receiving the message
    from the calling object and performing the
    appropriate method

6
Sending and Receiving Messages
  • Example Alien Blaster Program

7
Combining Objects
  • Objects can be combined to form other objects
    or collections of objects
  • A Car Class consists of individual Tires, Engine,
    Seats,individual parts that make up the whole
  • A Zoo Class has a collection of other animals
    Lions, Tigers, Bears, etc
  • A FreeCheckingAccount is a type of
    CheckingAccount is a type of Account

8
Combining Objects
  • Creating the Card Class
  • A playing card has a rank and a suit
  • Constants
  • Constructor initializes a single Card
  • __str__ is used to print out the rank and suit as
    a two character string

9
Combining Objects
  • Creating the Hand Class
  • The Hand class is a collection of Card objects
  • Uses a list
  • __str__ prints out the contents of each hand
  • clear() clears the hand of cards
  • add() adds a card to the list
  • give() gives a card to another hand

10
Combining Objects
  • Using Card Objects
  • Combining Card Objects Using a Hand Object
  • Example Playing Cards Program

11
Using Inheritance to Create New Classes
  • Create new classes from old
  • is_a relationship
  • Single inheritance
  • Multiple inheritance

12
Extending a Class Through Inheritance
  • Creating a Base Class
  • Same base classes as before
  • Inheriting from a Base Class
  • A deck is a special type of hand
  • It has 52 cards (populate method)
  • You can deal from a deck
  • You can shuffle a deck

13
Extending a Class Through Inheritance
  • Example Playing Cards 2.0 Program

14
Altering the Behavior of Inherited Methods
  • Creating a Base Class

15
Altering the Behavior of Inherited Methods
  • Overriding Base Class Methods

16
Altering the Behavior of Inherited Methods
  • Invoking Base Class Methods

17
Altering the Behavior of Inherited Methods
  • Using the Derived Classes

18
Altering the Behavior of Inherited Methods
  • Example Playing Cards 3.0 Program

19
Understanding Polymorphism
20
Creating Modules
  • Writing Modules

21
Creating Modules
  • Importing Modules

22
Creating Modules
  • Using Imported Functions and Classes

23
Creating Modules
  • Example Simple Game Program
  • Imports Games

24
Blackjack Game (Again)
  • Example Blackjack Game
  • Imports Cards, Games
Write a Comment
User Comments (0)
About PowerShow.com