1321 - PowerPoint PPT Presentation

ostinmannual
slide1 l.
Skip this Video
Loading SlideShow in 5 Seconds..
1321 PowerPoint Presentation
play fullscreen
1 / 13
Download Presentation
1321
297 Views
Download Presentation

1321

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 1321 CS

  2. CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Lecture 26 April 11, 2002

  3. Today’s Menu • Summary of Important OO Ideas • Inheritance revisited • Polymorphism • Building the traffic simulation (finally!)

  4. Important OO Ideas Translation: know this for the final exam • Encapsulate Behavior • Attributes (local data) • Methods (functions to manipulate the data) • Public methods (in Scheme, publicized by the service manager) • Private methods (internal use only) • Inheritance • Derive more specific classes from a general class • By extension – adding attributes and methods • By redefinition – changing methods • In Scheme, super is the reference to all the parent class behavior • Need for Protected access to the parent’s data • Polymorphism • Literally, having multiple forms • Process any collection of classes without caring about specifics • Objects in the collection need only inherit from a base class • Any method of the base class may be applied to the whole collection

  5. An Important Distinction • There are two different kinds of relationship between objects: • Inheritance (IS-A) relationship where a child object inherits characteristics of the parent object • Parent class: Vehicle • Attributes: location, speed, length, people • Child class: Van • Attributes: all above plus cargo capacity • Containing (HAS-A) relationship where the data part of an object is a collection of other objects • Container: Queue • Internal structure: don’t care • Items contained: anything you put in there • Special classes derived from queues might have methods that restrict the nature of the items in the queue

  6. Inheritance Example Vehicle • Location • Speed • Length • Wheels move draw HAS-A IS-A Car • Trunk • Body style draw Truck • Cargo draw Motorcycle Trailer draw • Cargo draw Semi • Trailer draw HAS-A Transporter • Vehicles move draw move

  7. Polymorphism Example • I have a collection (say, a list) of vehicles • Cars, trucks, semis, motorcycles, transporters • I want to traverse that collection and draw each vehicle • Without polymorphism, the code would look like: (cond [(symbol=? (((first lst) ‘class)) ‘Car) (draw-car (first lst)] [(symbol=? (((first lst) ‘class)) ‘Truck) (draw-truck (first lst)] [(symbol=? (((first lst) ‘class)) ‘Transporter) (draw-transporter (first lst)] [(symbol=? (((first lst) ‘class)) ‘Semi) (draw-semi (first lst)] … etc …

  8. Polymorphism Example • With polymorphism, the code would look like: (((first lst) ‘draw))) • Why? • All of the items on the list inherit from the Vehicle class • They can therefore all respond to the ‘draw method • It is left to the implementation of each object to do the right thing when commanded to ‘draw. • The behavior of the collection is therefore polymorphic because the items in the collection have multiple forms. • the code processing the collection doesn’t need to “know” the form of each object .

  9. Example: Inheritance • For our traffic simulation, we need a special kind of queue. • We need to be able to move and draw all the vehicles • To do this, we need to traverse all of the vehicles, applying a method to each Intersection controls which queues empty Vehicles “decide” which way to turn A collection of vehicles moving “at the speed limit” A collection of vehicles not moving

  10. A Derived Queue Class Whoa, Nellie! The children can’t reach the list data Queue • list isFull? enqueue isEmpty? dequeue data A protected method – available only to children Traverse the data calling a method with no parameters Trav-queue • list • (super ‘data) traverse Traverse the data calling a method with one parameter traverse1 Make the (super …) call each time you need the data, not in the constructor

  11. queue.scm trav-queue.scm inheritance.scm traffic-model.scm

  12. Questions?