Presentation is loading. Please wait.

Presentation is loading. Please wait.

November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger.

Similar presentations


Presentation on theme: "November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger."— Presentation transcript:

1 November 28, 2005ICP: 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 3 Blackjack Game Example: Blackjack GameBlackjack Game –Imports: Cards, GamesCardsGames

4 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 6 Sending and Receiving Messages Example: Alien Blaster ProgramAlien Blaster Program

7 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 10 Combining Objects Using Card Objects Combining Card Objects Using a Hand Object Example: Playing Cards ProgramPlaying Cards Program

11 November 28, 2005ICP: Chapter 9: Object Oriented Programming 11 Using Inheritance to Create New Classes Create new classes from old –“is_a” relationship –Single inheritance –Multiple inheritance

12 November 28, 2005ICP: Chapter 9: Object Oriented Programming 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 November 28, 2005ICP: Chapter 9: Object Oriented Programming 13 Extending a Class Through Inheritance Example: Playing Cards 2.0 ProgramPlaying Cards 2.0 Program

14 November 28, 2005ICP: Chapter 9: Object Oriented Programming 14 Altering the Behavior of Inherited Methods Creating a Base Class

15 November 28, 2005ICP: Chapter 9: Object Oriented Programming 15 Altering the Behavior of Inherited Methods Overriding Base Class Methods

16 November 28, 2005ICP: Chapter 9: Object Oriented Programming 16 Altering the Behavior of Inherited Methods Invoking Base Class Methods

17 November 28, 2005ICP: Chapter 9: Object Oriented Programming 17 Altering the Behavior of Inherited Methods Using the Derived Classes

18 November 28, 2005ICP: Chapter 9: Object Oriented Programming 18 Altering the Behavior of Inherited Methods Example: Playing Cards 3.0 ProgramPlaying Cards 3.0 Program

19 November 28, 2005ICP: Chapter 9: Object Oriented Programming 19 Understanding Polymorphism

20 November 28, 2005ICP: Chapter 9: Object Oriented Programming 20 Creating Modules Writing Modules

21 November 28, 2005ICP: Chapter 9: Object Oriented Programming 21 Creating Modules Importing Modules

22 November 28, 2005ICP: Chapter 9: Object Oriented Programming 22 Creating Modules Using Imported Functions and Classes

23 November 28, 2005ICP: Chapter 9: Object Oriented Programming 23 Creating Modules Example: Simple Game ProgramSimple Game Program –Imports: GamesGames

24 November 28, 2005ICP: Chapter 9: Object Oriented Programming 24 Blackjack Game (Again) Example: Blackjack GameBlackjack Game –Imports: Cards, GamesCardsGames


Download ppt "November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger."

Similar presentations


Ads by Google