CSE 505 Lecture Notes:

Object-Oriented Programming and Simula


Simula: the first object-oriented langauge

Developed at the Norwegian Computer Center by Ole-Johan Dahl, Kristen Nygaard, and others circa 1965 as an Algol-60 extension

key ideas:

simple example: begin class Stack(size); integer size; begin real array stack_array(1:size); integer ptr; procedure push(x); real x; begin ptr := ptr+1; stack_array[ptr] := x; end; real procedure pop; begin pop := stack_array[ptr]; ptr := ptr-1; end; comment stack initialization; ptr := 0; end Stack; ref(Stack) s1,s2; real x; integer k; s1 :- new Stack(10); s2 :- new Stack(10); comment push and pop something on the stack; s1.push(3.14159); x := s1.pop; s2.push(2.718); comment note there is no protection for the internal data; k := s1.ptr; end;
More complicated example illustrating class hierarchies, virtual procedures, and SIMULA type-checking begin class vehicle(destination); value destination; text destination; virtual: procedure start; begin procedure start; begin outtext("vehicle going to "); outtext(destination); outimage; end; end vehicle; vehicle class car; begin procedure start; begin outtext("fasten seat belts, please"); outimage; end; end car; vehicle class bus(max_passengers); integer max_passengers; begin procedure start; begin outtext("85 cents, please"); outimage; end; end bus; ref(vehicle) v; ref(car) c; ref(bus) b; comment construct a new car with destination = downtown; v :- new car("downtown"); c :- new car("airport"); b :- new bus("ballard",60); comment invoke associated procedures to see effects; v.start; c.start; b.start; comment and now some examples of legal assignments and accesses; v :- c; v :- b; comment the following assignment would be illegal without the 'qua'; b :- v qua bus; comment these assignments and accesses would cause compile-time errors: c :- v v.max_passengers ; comment finally, an assignment that is ok at compile time but that will create a run-time error; v :- new vehicle("tukwila"); c :- v qua car; end;