Programming Languages

G22.2110 Summer 1998

 

Class #6

OO Imperative Programming Languages - Part II

 

Homework #6: Sethi Exercises 6.4, 6.12, 7.2, 7.9, 7.13

Read Sethi’s Chapters 7, 15.3, 15.4, handouts and references

 

Contents

 

C6.0. Midterm, Current Assignments, PL Project

C6.1. Introduction to Object-Oriented Programming Languages

C6.2. The Smalltalk Programming Language

C6.3. Support for Object-Oriented Programming in C++ (next session)

C6.4. Support for Object-Oriented Programming in Ada95 (next session)

C6.5. Comments on Sethi's Chapters 7, 15.3, 15.4

 

C6.0. Current Assignments, PL Project

 

Homework #6 due 7/9/98

 

Midterm, 1 hour in class – June 25th (Sethi’s chapters 1-6 and class notes)

 

PL Project – Part I due today, Part II to be posted on the web on July 3 (due July 16)

(Part II focuses on C++ and Java, Part III focuses on Ada, and Part IV on Scheme/LISP)

 

C6.1. Introduction to Object-Oriented Programming Languages

 

Pure OO languages:

 

Hybrid languages:

 

Object-Oriented Programming:

 

ADT:

 

Reuse:

 

Inheritance:

subtype SMALL_INT is INTEGER range -100 .. 100;

 

Derived Types:

 

Subtype:

 

Classes:

 

Complex Type Inheritance:

 

Polymorphism:

 

Inheritance and dynamic type binding distinguish OO programming from programming with ADTs.

 

C6.2. The Smalltalk Programming Language

 

Message Passing Model

 

Everything is an object (integer constant or complex file-handling system)

 

Objects:

 

Messages:

 

Method:

 

Classes:

 

Smalltalk Environment

 

Smalltalk system integrates:

 

System interface:

 

System is written in Smalltalk and can be customized to fit user needs (source available to user).

 

Smalltalk is a programming language + programming methodology + programming environment

 

Smalltalk Language

 

Expressions:

 

Literals:

 

Variables:

 

Message Expressions:

 

2 parts:

 

3 categories:

e.g., firstAngle sin

e.g., 21 + 2

sum / count

e.g., firstArray at: 1 put: 5

method is "at:put:" and is called a selector

 

Message expressions:

e.g, (total - 3) * divisor

firstArray at: index - 1 put: 77 (1 sent to the - method of object index, the result and 77

are sent to the at:put: method of the object firstArray

e.g., ourPen home; up; goto: 500@500; down; home

same as:

ourPen home.

ourPen up.

ourPen goto: 500@500.

ourPen: down.

ourPen: home.

 

Methods:

 

General syntactic form:

Message_pattern [ | temporary variables| ] statements

 

Temporary variables:

 

Message pattern:

 

Examples:

e.g., unary method

currentTotal

^(oldTotal + newValue)

currentTotal method returns the value of the expression in parenthesis

e.g., keyword method

x: xCoord y: yCoord

ourPen up; goto xCoord@yCoord; down

x:y: message selector is such that object ourPen is sent the messages up, goto, and down.

Example message is: ourPen x: 300 y: 400

 

Assignment Statements:

 

e.g., total <- 22

index <- index + 1

salesTax <- deducts grossPay: 350.0 dependents: 4

 

Block and Control Structures:

 

Control structures are not provided by statements in the language, but they are formed with the fundamental object-oriented paradigm: message passing.

 

Blocks

 

Provide a way to collect expressions into groups into an unamed literal object

 

Blocks can be used to build execution control constructs.

 

Blocks are instances of the class Block.

 

Blocks can be thought of as procedure declarations that may appear anywhere

 

e.g., block is specified in brackets, with its expression components separated by periods

[index <- index + 1. sum <- sum + index]

 

e.g., expressions in a bloc are deferred actions (only executed when encountered)

[sum <- sum + index] value

 

e.g., blocks can be assigned to variables, and executed by sending the value message to the variable

addIndex <- [sum <- sum + index]

addIndex value

or

 

addIndex <- [sum + index]

sum <- addIndex value

 

e.g., block parameter separated from remainded of the block by (|), formal parameter specifications require a colon to be attached to the left end of each parameter

[:x :y | sum <- x + 10. total <- sum * y]

 

Iterations

 

Use the keyword method whileTrue provided by the class Block

e.g.,

count <- 1.

sum <- 0.

[count <= 20]

whileTrue: [sum <- sum + count.

count < count + 1]

 

Use integer method timesRepeat:

e.g.,

xCube <- 1.

3 timesRepeat: [xCube <- xCube * x]

 

Use to:do: integer method:

e.g.,

1 to: 5 do: [sum <- sum + x], object 1 produces and returns 1, 2, 3, 4, 5 as internal values

 

Use to:by:do: integer method:

e.g.,

2 to: 10 by: 2 do: [:even | sum <- sum + even], block parameter takes on internal values

2, 4, 6, 8, and 10

 

Selection

 

Use ifTrue:ifFalse: method of the true and false objects

e.g.,

total = 0

ifTrue: [average <- 0]

ifFalse: [average <- sum // total]

 

Classes

 

Four parts:

 

Instance variables:

 

Dynamic Binding

 

Messages to an object normally causes the class to which the object belongs to be searched for a corresponding method.

If search fails, it is continued in the superclass of that class up to the system class Object which has no superclass (root).

 

More About Methods

 

e.g., use of temporary variables

first: x second: y | temp |

temp <- x + y.

temp > 1000

ifTrue: [y <- 1000].

^y

 

e.g., use of pseudovariable self (object name that refers to the object in which it appears)

total = 0

ifTrue: [self error: 'Error - cannot compute average']

ifFalse: [^sum // total]

 

Type Checking and Generics

 

Smalltalk variables are not typed (any name can be bound to any object)

 

Type checking occurs dynamically when a message is sent to an object.

 

Inheritance

 

Can use "super" pseudovariable as a message prefix to bypass method hiding and start the method search in the superclass rather than locally

 

Smalltalk support single inheritance only.

 

Evaluation

 

Small language

Language syntax is simple and very regular

Programs are slower

Type errors go undetected until run time.

Appealing user interface and methodology.

 

C6.3. Support for Object-Oriented Programming in C++

 

Inheritance

 

Dynamic Binding

 

Evaluation

 

C6.4. Support for Object-Oriented Programming in Ada95

 

Classes and Objects

 

Inheritance

 

Dynamic Binding

 

Evaluation

 

C6.5. Comments on Sethi's Chapters 7, 15.3, 15.4