1321
Download
1 / 13

1321 - PowerPoint PPT Presentation


  • 264 Views
  • Updated On :

1321 CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 26 April 11, 2002 Today’s Menu Summary of Important OO Ideas Inheritance revisited Polymorphism Building the traffic simulation (finally!) Important OO Ideas

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '1321' - ostinmannual


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Slide1 l.jpg

1321

CS


Cs1321 introduction to programming l.jpg

CS1321:Introduction to Programming

Georgia Institute of Technology

College of Computing

Lecture 26

April 11, 2002


Today s menu l.jpg
Today’s Menu

  • Summary of Important OO Ideas

  • Inheritance revisited

  • Polymorphism

  • Building the traffic simulation (finally!)


Important oo ideas l.jpg
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


An important distinction l.jpg
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


Inheritance example l.jpg
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


Polymorphism example l.jpg
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 …


Polymorphism example8 l.jpg
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 .


Example inheritance l.jpg
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


A derived queue class l.jpg
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


Slide11 l.jpg

queue.scm

trav-queue.scm

inheritance.scm

traffic-model.scm