Vous êtes sur la page 1sur 7

CS 242

Simula 67
• First object‐oriented language
• Designed for simulation
Simula and Smalltalk – Later recognized as general‐purpose prog language
• Extension of Algol 60
g
John Mitchell • Standardized as Simula (no “67”) in 1977
• Inspiration to many later designers
– Smalltalk
– C++
– ...

Brief history Comparison to Algol 60
• Norwegian Computing Center • Added features
– Designers:  Dahl, Myhrhaug, Nygaard – class concept
– Simula‐1 in 1966    (strictly  a simulation language) – reference variables (pointers to objects)
– General language ideas – pass‐by‐reference
• Influenced by Hoare
Influenced by Hoare’ss ideas on data types
ideas on data types – char text I/O
char, text, I/O
• Added classes and prefixing (subtyping) to Algol 60 – coroutines
– Nygaard
• Operations Research specialist and political activist
• Removed
• Wanted language to describe social and industrial systems – Changed default par passing from pass‐by‐name
• Allow “ordinary people” to understand political (?) changes – some var initialization requirements
– Dahl and Myhrhaug – own (=C static) variables
• Maintained concern for general programming – string type (in favor of text type)

Objects in Simula Example: Circles and lines
• Class • Problem
– A procedure that returns a pointer to its activation record – Find the center and radius of the circle  q
• Object passing through three distinct points, p, q, 
p
and r
– Activation record produced by call to a class r
• Object access
Obj t • Solution
– Access any local variable or procedures using dot  – Draw intersecting circles Cp, Cq around p,q 
notation: object. and circles Cq’, Cr around q, r (Picture 
assumes Cq = Cq’)
• Memory management – Draw lines through circle intersections
– Objects are garbage collected – The intersection of the lines is the center 
• user destructors considered undesirable of the desired circle. 
– Error if the points are colinear. 

1
Approach in Simula Simula Point Class
• Methodology  class Point(x,y); real x,y; formal p is pointer to Point
– Represent points, lines, and circles as objects. begin
– Equip objects with necessary operations. boolean procedure equals(p); ref(Point) p;
if p =/= none then
• Operations  equals := abs(x ‐ p.x) + abs(y ‐ p.y) < 0.00001
– Point  real procedure distance(p); ref(Point) p;
real procedure distance(p); ref(Point) p;
equality(anotherPoint) : boolean 
if p == none then error else
distance(anotherPoint) : real          (needed to construct  circles) 
distance := sqrt(( x ‐ p.x )**2 + (y ‐ p.y) ** 2);
– Line  end ***Point***
parallelto(anotherLine) : boolean           (to see if lines intersect) 
meets(anotherLine) : REF(Point) 
p :‐ new Point(1.0, 2.5); uninitialized ptr has
– Circle 
intersects(anotherCircle) : REF(Line)
q :‐ new Point(2.0,3.5); value none
if p.distance(q) > 2 then ...

pointer assignment

Representation of objects Simula line class
class Line(a,b,c); real a,b,c;
Local variables
begin
p access link
boolean procedure parallelto(l); ref(Line) l; line determined by
real x 1.0 code for if l =/= none then  parallelto := ... ax+by+c=0
real y 2.5 equals ref(Point) procedure meets(l); ref(Line) l;
proc equals
p q
begin real t;  Procedures
if l /
if l =/= none and ~parallelto(l) then ...
d~ ll lt (l) th
proc distance code for end; 
distance real d;   d := sqrt(a**2 + b**2);
if d = 0.0 then error else
begin
d := 1/d;
a := a*d;  b := b*d;  c := c*d; Initialization:
end;
Object is represented by activation record with access link to 
end *** Line***
“normalize” a,b,c
find global variables according to static scoping

Derived classes in Simula Subtyping
• A class decl may be prefixed by a class name • The type of an object is its class
class A • The type associated with a subclass is treated as a 
subtype of the type assoc with superclass
A class B • Example:
A class C
A class C class A(…);  ...
l A( )
B class D A class B(…); ...
ref (A) a :‐ new A(…)
• An object of a “prefixed class” is the  ref (B) b :‐ new B(…)
concatenation of objects of each class in prefix
A part a := b          /* legal since B is subclass of A */
– d :‐ new D(…) B part ...
d D part b := a         /* also legal, but run‐time test  */

2
Main object‐oriented features Features absent from Simula 67
• Classes • Encapsulation
• Objects – All data and functions accessible; no private, protected
• Inheritance  (“class prefixing”) • Self/Super mechanism of Smalltalk
• Subtyping – But has an expression this〈class〉 to refer to object 
itself regarded as object of type 〈class〉. Not clear how 
itself, regarded as object of type 〈class〉 Not clear how
• Virtual methods  powerful this is…
– A function can be redefined in subclass • Class variables
• Inner  – But can have global variables
– Combines code of superclass with code of subclass • Exceptions
• Inspect/Qua  – Not fundamentally an OO feature ...
– run‐time class/type tests

Simula Summary Smalltalk
• Class • Major language that popularized objects
– ”procedure" that returns ptr to activation record • Developed at Xerox PARC
– initialization code always run as procedure body – Smalltalk‐76, Smalltalk‐80 were important versions
• Objects: closure created by a class • Object metaphor extended and refined
– Used some ideas from Simula, but very different lang
U d id f Si l b diff l
• Encapsulation 
– Everything is an object, even a class
– protected and private not recognized in 1967 – All operations are “messages to objects”
– added later and used as basis for C++  – Very flexible and powerful language
• Subtyping: determined by class hierarchy • Similar to “everything is a list” in Lisp, but more so 
• Example: object can detect that it has received a message it 
• Inheritance: provided by class prefixing does not understand, can try to figure out how to respond. 

Motivating application: Dynabook Smalltalk language terminology
• Concept developed by Alan Kay • Object      Instance of some class
• Small portable computer • Class        Defines behavior of its objects
– Revolutionary idea in early 1970’s
• At the time, a minicomputer was shared by 10 people,  • Selector    Name of a message
stored in a machine room.
– What would you compute on an airplane?
• Message Selector together with parameter values
Message    S l t t th ith t l

• Influence on Smalltalk • Method     Code used by a class to respond to message
– Language intended to be programming language and  • Instance variable   Data stored in object 
operating system interface
– Intended for “non‐programmer” • Subclass    Class defined by giving incremental  
– Syntax presented by language‐specific editor modifications to some superclass

3
Example: Point class Class messages and methods
Three class methods Explanation
• Class definition written in tabular form newX:xvalue Y:yvalue  | | 
- selector is mix-fix newX:Y:
^ self new x: xvalue
class name Point e.g, Point newX:3 Y:2
y: yvalue            
super class Object - symbol ^ marks return value
class
l var pii newOrigin | | - new isi method
th d ini allll classes,
l
instance var x y ^ self new x: 0 inherited from Object
class messages and methods y: 0 - | | marks scope for local decl
〈…names and code for methods...〉
initialize | |
instance messages and methods - initialize method sets pi, called
pi <‐ 3.14159
〈…names and code for methods...〉
automatically
- <- is syntax for assignment

Instance messages and methods Run‐time representation of point
Five instance methods Explanation to superclass Object
x: xcoord y: ycoord | | set x,y coordinates,
x <‐ xcoord Point class
e.g, pt x:5 y:3
y <‐ ycoord Template
Point object
moveDx: dx Dy: dy | |
y y|| x
move point
i t by
b given
i amountt class
x <‐ dx + x y
x 3
y <‐ dy + y
y 2
x | | ^x
Method dictionary code
y | | ^y return hidden inst var x
draw | |   return hidden inst var y newX:Y:
...
〈...code to draw point...〉 draw point on screen Detail: class method shown in
dictionary, but lookup procedure
...
distinguishes class and instance move
code
methods

Inheritance Run‐time representation
Point object Point class Template
• Define colored points from points Method dictionary
x
class name ColorPoint y newX:Y:
2 ...
super class Point draw
3
move
class var
new instance
instance var color variable ColorPoint class
ColorPoint object Template
class messages and methods Method dictionary
x
newX:xv Y:yv C:cv 〈 … code … 〉 new method
y newX:Y:C:
4
instance messages and methods color color
5
color | | ^color draw
override Point red
draw 〈 … code … 〉 method
This is a schematic diagram meant to illustrate the main idea. Actual implementations may differ.

4
Encapsulation in Smalltalk Object types 
• Methods are public • Each object has interface
• Instance variables are hidden  – Set of instance methods declared in class
– Not visible to other objects – Example:
• pt x   is not allowed unless x is a method
t i t ll d l i th d Point          { x:y:, moveDx:Dy:, x, y, draw} 
Point { x:y: moveDx:Dy: x y draw}
– But may be manipulated by subclass methods ColorPoint   { x:y:, moveDx:Dy:, x, y, color, draw}
• This limits ability to establish invariants – This is a form of type
• Example:  Names of methods, does not include type/protocol of 
– Superclass maintains sorted list of messages with some  arguments 
selector, say insert 
– Subclass may access this list directly, rearrange order
• Object expression and type
– Send message to object
p draw                          p  x:3 y:4

Subtyping  Subtyping and Inheritance
• Relation between interfaces • Subtyping is implicit
– Suppose expression makes sense – Not a part of the programming language
p msg:pars    ‐‐ OK if msg is in interface of p – Important aspect of how systems are built
– Replace p
Replace p by q
by q if interface of q
if interface of q contains interface of 
contains interface of • Inheritance is explicit
I h it i li it
p
– Used to implement systems
– No forced relationship to subtyping
• Subtyping
– If interface is superset, then a subtype
– Example: ColorPoint subtype of Point
– Sometimes called “conformance”

Collection Hierarchy Smalltalk Flexibility
isEmpty, size, includes: , … • Measure of PL expressiveness:
Collection
at: – Can constructs of the language be defined in the 
Indexed language itself?
add: – Examples:
Set remove:
Updatable at:Put:
• Lisp cond:  Lisp allows user‐defined special forms
• ML datatype: sufficient to define polymorphic lists, 
Dictionary sortBlock: equivalent to built‐in list type
Sorted collection …
Array associationAt: • ML overloading: limitation, since not available to 
programmer
replaceFrom:to:with:
Subtyping • C/C++: ???
Inheritance • Smalltalk is expressive in this sense
M h ld b “ i i i ” h

5
Smalltalk booleans and blocks Self and Super
• Boolean value is object with ifTrue:ifFalse:
Integer
– Class boolean with subclasses True and False
– True ifTrue:B1 ifFalse:B2 executes B1
SmallInt LargeInt
g
– False ifTrue:B1 ifFalse:B2
False ifTrue:B1 ifFalse:B2 executes B2 Factorial |   |
i l| |
• Example expression self <= 1
i < j  ifTrue: [i add 1] ifFalse: [j subtract 1] ifTrue: [^1]
– i < j is boolean expression, produces boolean  ifFalse: [^(self‐1) factorial * self ]
object
– arg’s are blocks, objects with execute methods This method can be implemented in Integer, and works 
even if SmallInt and LargeInt are represented differently.
• Since booleans and blocks are very common C++ and Java type systems can’t really cope with this.

Ingalls’ test Smalltalk integer operations
• Dan Ingalls: principal designer Smalltalk system • Integer expression
– Grace Murray Hopper award for Smalltalk and  – x plus: 1 times: 3 plus: (y plus: 1)  print
Bitmap graphics work at Xerox PARC     • Properties
– 1987 ACM Software Systems Award with Kay, 
1987 ACM Software Systems Award with Kay – All operations are executed by sending messages 
All i db di
Goldberg
– If x is from some “new” kind of integer, expression 
• Proposed test for “object oriented” makes sense as long as x has plus, times, print 
– Can you define a new kind of integer, put your new  methods.
integers into rectangles (which are already part of 
the window system), ask the system to blacken a  Actually, compiler does some optimization. 
rectangle, and have everything work? But will revert to this if x is not built‐in integer.
– Smalltalk passes, C++ fails this test

Costs and benefits of “true OO” Smalltalk Summary
• Why is property of Ingalls test useful? • Class
– Everything is an object – creates objects that share methods
– All objects are accessed only through interface – pointers to template, dictionary, parent class
– Makes programs extensible
Makes programs extensible • Objects: 
Obj t created by a class, contains instance variables
db l bl
• What is implementation cost? • Encapsulation 
– Every integer operation involves method call – methods public, instance variables hidden
• Unless optimizing compiler can recognize many cases
• Subtyping: implicit, no static type system
– Is this worth it?
• One application where it seems useful ?
• Inheritance: subclasses, self, super 
Single inheritance in Smalltalk‐76, Smalltalk‐80
• One application where it seems too costly?
• Are there other issues? Security? (wait for Java final

6
Ruby               www.ruby‐lang.org Example Ruby class
• Ruby is a “complete, full, pure object oriented language” class Song 
– All data in Ruby are objects, in the sense of Smalltalk @@plays = 0 
– Example: the number 1 is an instance of class Fixnum def initialize(name, artist, duration) 
• Very flexible @name = name 
– Can add methods to a class, or even to instance during runtime @artist = artist 
– Closures @duration = duration 
duration
– Automatic small integer (Fixnum) large integer (Bignum) conversion @plays = 0 
end 
• Single inheritance only
def play 
– Ruby features single inheritance only, *on purpose*
@plays += 1 
– Modules are collections of methods
@@plays += 1 
• A class can import a module and gets all its methods
"This song: #@plays plays. Total #@@plays plays." 
end 
end 
see http://www.rubycentral.com/book/

Vous aimerez peut-être aussi