The still growing fascination of object-oriented programming dates to 1989 when several software companies offered compilers of object-oriented Pascal's, of C (C++ and Objective C) etc. We welcome this recognition of merits of object-oriented programming with satisfaction. Let us recall that classes and objects have more than 24 years of tradition. They appeared in Simula-67. Along the line of R&D concerning classes and their objects one can find the results achieved at the Institute of Informatics, University of Warsaw. We shall present many of them during these lectures.
Notion of object has its roots in the well known structure of an Algol and Pascal-like program.
Let us begin with the notion of module. In a program one can indicate several modules. The whole program is a module, every procedure and function declared in a program is a module too.
In the language Loglan we have more kinds of modules:
Let us look at the most external module of a program
_______________________ |program name (...) | | | This a module. | <declarations> e.g. | (It can contain other modules) | var x,y:real,a:bool;| | | |begin | | | | <instructions> e.g. | | x:=x+y; | | a:=x*x<y; | | if not a then ...fi;| | while ... | | do | | ... | | od | | | |end | |_____________________|
During an execution of a program the so called activation record of the above module is allocated in a memory. This is a prototype of the notion of object.
_________________________ |memory of data | | | | x real 0.5 | R | y real -1.17 | This is an activation record, A | a bool true | sometimes called a dynamic ins- M | ..................... | tance of the (program) module. |memory of instructions | m | x:=x+y; | e | a:=x*x<y; | m | if not a then ... fi; | o | while ... | r | do | y | ... | | od | |_______________________|
Object-oriented programming develops the above remarks and enriches the image the reader could build from them. In particular, object-oriented programming requires more frames and assumes a wider spectrum of the types of frames. Objects are just one type of frames appearing in programming.
More frames! Where they are coming from? Can we recognize them in our Pascal practice? Yes, they arise during execution of procedure statements. How? They are known under the name of activation records or dynamic instances of procedures.
An example, a snapshot of program's execution may look like:
The above picture is a snapshot of an execution of the main program taken in the moment when in the main program we executed a call g procedure instruction, which caused the creation of an activation record for g and its execution, which in turn executed a call f procedure instruction, which caused the creation of (1st) instance of an activation record for f procedure and ...
__________________________________________________ | ___________ | __|______________ _____________|____ | | | activ.rec f | | activ.rec. g | | | | ÆÍÍÍÍÍ>͵ | | | | (1st instance)| | (1st instance) | | | | | | | ____________ | | | ______________ ====> | Main | | ____________ | | | | | | ... | | call f | |f: proc | | call f | | | | | | | | | |g: proc | | | | | | | ----------------- ------------------ |_________ | ^ | | | _________________________________>___|call g | ________________ __________________ | | | activ.rec. f | | activ.rec. f ÃÄÄÄÄ>Ä´ | | ÆÍ<ÍÍ͵ | ÀÄÄÄÄÄÄÄÄÄÄÙ | (2nd instance) | | (3rd instance) | | | | | | _____________ | | | | | | | | call f | | ... | | | | | |________________| |_________________|
The instance of main program is the static father of all remaining activation records. This may be denoted by single lines (static links) leading to the leftmost object. A static father of an object is to be searched when the processor finds an identifier e.g. a variable which is non-local for a currently executed object. This in turn may lead to further searches along the path of static links. In every snapshot the set of objects together with the set of all static links creates a tree. Every edge of this graph corresponds to the tree structure of module nesting in an obvious way.
Another kind of arrows (double lines) leads from an object to its dynamic father i.e. to the object in which processor subsumes the execution of instructions when all the instructions of a current object are executed to the end. The graph structure of objects and dynamic links differs from the previous one and shows "who called who". Making use of our Pascal experience we would like to assert it is a straight line structure. Later we shall see that objects and coroutines enrich and complicate the picture.
Let us think of a scenario for an activation record of a procedure.
About Simula-67
The origins of object oriented programming go back to 1967
when O.-J.Dahl, K.Nygaard and B.Myhrhaug defined the language
Simula-67. [books: SIMULAbegin by G.Birtwistle, O.J.Dahl,
Auerbach Publ. and Simula 67 by W.M.Bartol, H.Oktaba, PWN Publ.
are worth reading].
about class
General structure of a class module is as follows:
unit <name_of_class >: class (<formal parameters>); <local declarations of variables, procedures, functions and classes! > begin <instructions> end <name_of_class >
Example
unit circle : class (center: point, radius: real); unit intersects: function (c: circle): line; {the function returns the line which passes through the intersection points of this circle object and the c circle object, NOTE! it might return none if the two circles have no common points, see how it is solved below }... end intersects; begin if r=0 then raise SignalNoCircle fi end circle
Let us remark that the syntactic difference between a procedure declaration and class declaration is little: the keyword procedure is replaced by another keyword class. However, the semantics is entirely different as it will be seen from the diagram-scenario below.
declaration of variables of type class
One can assume that every declaration of a class introduces
a new type, for it enables declarations of variables like:
var x : circle
objects
Objects of classes can be generated by means of expressions (object
generator) of the form
new KLASA(actual_parameters>
and stored in variables
k:= new KLASA(actual_paramts)
One module of a class can serve as a pattern for many objects
x:= new circle(point1, 88);
y:= new circle(new point(45,159), 644)
they can have different names. The names help to acces objects
z:= x
and their internal structure
x.center ... y.radius
The values of the latter expressions will be correspondingly:
point1 and 644.
scenario of an object looks as follows
IMPORTANT consequences
1. one can use initialization phase to execute an algorithm or
to initialize objects.
EXERCISE. write factorial algorithm using objects and no
recursion.
2. objects can be used as records,
EXERCISE. write a piece of program which realizes a tree.
3. one can send commands to objects, an object is able to execute
the call commands referring to its local procedure declarations
(and local functions too)
e.g. call x.aProc(a)
z:= x.aFun(b)
if aProc and aFun were declared in a class C and if x denotes
an object of the class C.
Examples complex numbers unit complex: class (re,im: real); var module: real; unit add: function(z: complex): complex; begin result:= new complex(re+z.re, im+z.im) end add; unit mult: function(z: complex): complex; begin result:= new complex(re*z.re-im*z.im, re*z.im+z.re*im) end mult; begin module:= sqrt(re*re+im*im) end complex; Having such class one can create several objects of type complex and manipulate on them e.g. program UsingComplex; unit complex ... end complex; var c1,c2,z1,z2: complex; begin c1:= new complex(1,9); {creation of complex number 1+9i} c2:= new complex(-3,-21); {second object of class complex} z1:= c1.add(c2.mult(c1)); {now z1=c1+(c2*c1))} z2:= z1.mult(new complex(z1.re,-z1.im)) {Note an object without a name, once used, it becomes a garbage}end UsingComplex
EXERCISE. Write a similar program in Pascal and compare : how many parameters transmitted in an expression?, are the concepts of complex numbers encapsulated?
EXAMPLE which follows introduces concepts of planar geometry and
uses them in a prefixed block. The reader can run it and modify
it as well. {This is the first Loglan program I wrote in December
1981 to test the compiler.}
program CircumscribedCircle; unit GEOPLAN : class; (* This class is in fact a problem oriented language, it offers various facilities for problem solving in the field of analitical planar geometry. The class has the following structure: remark the correspondence between software notions of class, procedure, function and the notions of general algebra: algebraic structure, sorts and operations GEOPLAN <----- class algebraic structure / | \ / | \ / | \ POINT CIRCLE LINE <----- classes sorts / | \ | / | \ / | \ | / | \ operations / | | | | | \ <--| / | | | | | \ | opera- EQUALS DIST | | MEETS | \ | ERROR | | ERROR \ tions | | / INTERSECTS | | | | PARALLELTO <--| *) unit POINT : class(X,Y : REAL); unit EQUALS : function (Q : POINT) : BOOLEAN; begin RESULT:= Q.X=X and Q.Y=Y ; end EQUALS; unit DIST : function (P : POINT) : REAL; (* DISTANCE BETWEEN THIS POINT AND POINT P *) begin if P = none then call ERROR else result:= SQRT((X-P.X)*(X-P.X)+(Y-P.Y)*(Y-P.Y)) fi end DIST; unit virtual ERROR : procedure; begin WRITELN(" THERE IS NO POINT") end ERROR; end POINT; unit CIRCLE : class (P : POINT, R : REAL); { THE CIRCLE IS REPRESENTED BY ITS CENTER P AND THE RADIUS R } unit INTERSECTS : function (C : CIRCLE) : LINE; (* IF BOTH CIRCLES INTERSECT AT 2 POINTS, THE LINE JOINING THEM IS RETURNED. IF CIRCLES INTERSECT AT ONE POINT, IT IS TANGENT TO BOTH OF THEM. OTHERWISE PERPENDICULAR BISECTION OF THEIR CENTRES IS RETURNED *) var R1,R2 : REAL; begin if C=/= none then R1:= R*R-P.X*P.X-P.Y*P.Y; R2:= C.R*C.R-C.P.X*C.P.X-C.P.Y*C.P.Y; result := new LINE (P.X-C.P.X,P.Y-C.P.Y,(R1-R2)/2); fi end INTERSECTS; begin if P=none then WRITELN (" WRONG CENTRE") fi end CIRCLE; unit LINE : class (A,B,C : REAL); {LINE IS REPRESENTED BY COEFFICIENTS OF EQUATION AX+BY+C=0 } unit MEETS : function (L : LINE) : POINT; (* IF TWO LINES INTERSECT function MEETS RETURNS THE POINT OF INTERSECTION, OTHERWISE RETURNS NONE *) VAR T : REAL; begin if L =/= none and not PARALLELTO (L) then T := 1/(L.A*B-L.B*A); result := new POINT (-T*(B*L.C-C*L.B), T*(A*L.C-C*L.A)); else call ERROR fi end MEETS; unit PARALLELTO : function (L : LINE) : BOOLEAN; begin if L=/= none then if A*L.B-B*L.A = 0.0 then result:=TRUE; WRITELN(" zle"); else result:=FALSE; WRITELN(" dobrze"); fi else call ERROR fi end PARALLELTO; unit virtual ERROR : procedure; begin WRITELN(" THERE IS NO LINE") end ERROR; var D : REAL; begin (* NORMALIZATION OF COEFFICIENTS *) D := SQRT(A*A+B*B); if D= 0.0 then WRITELN( " ZLE, ZERO"); call ERROR else A := A/D; B := B/D; C := C/D; fi end LINE; end GEOPLAN; begin pref GEOPLAN block (* THE LANGUAGE GEOPLAN IS USED FOR FINDING THE CIRCLE CIRCUMSCRIBED ON A GIVEN TRIANGLE: P / \ / \ / .<-\------- CENTRE / \ Q---------R *) taken POINT,LINE,CIRCLE; var P,Q,R,CENTRE : POINT, L1,L2 : LINE, C1,C2,C4 : CIRCLE, RADIUS, X1,Y1 : REAL; begin do WRITELN("THIS PROGRAM FINDS THE CENTRE AND RADIUS OF "); WRITELN(" THE CIRCLE CIRCUMSCRIBED ON A GIVEN TRIANGLE "); WRITELN(" GIVE THE VERTICES COEFFICIENTS OF A TRIANGLE"); WRITELN(" X1,Y1= ?? ??"); READ (X1,Y1); P := new POINT(X1,Y1); WRITELN(" ",X1," ",Y1); WRITELN(" X2,Y2= ?? ??"); READ (X1,Y1); Q := new POINT(X1,Y1); WRITELN(" ",X1," ",Y1); WRITELN(" X3,Y3= ?? ??"); READ (X1,Y1); R := new POINT (X1,Y1); WRITELN(" ",X1," ",Y1); RADIUS := P.DIST(Q) + Q.DIST(R); C1 := new CIRCLE (P,RADIUS); C2 := new CIRCLE (Q,RADIUS); C4 := new CIRCLE (R,RADIUS); L1 := C2.INTERSECTS(C1); (*THE PERPENDICULAR BISECTOR OF THE SIDE PQ*) L2 := C2.INTERSECTS(C4); (*THE PERPENDICULAR BISECTOR OF THE SIDE QR *) CENTRE := L1.MEETS(L2); if CENTRE = none then WRITELN(" ALL POINTS LAY ON THE SAME LINE"); else WRITELN(" THE CIRCUMSCRIBED CIRCLE IS AS FOLOWS:"); WRITELN(" CENTRE = (",CENTRE.X,',',CENTRE.Y,')'); WRITELN(" RADIUS = ",CENTRE.DIST(P)); fi od end end The static structure of modules in the above program is the tree PROGRAM / \ / \ / \ / \ GEOPLAN pref GEOPLAN block / | \ / | \ / | \ POINT CIRCLE LINE / | \ | / | \ / | \ | / | \ / | | | | | \ / | | | | | \ EQUALS DIST | | MEETS | \ ERROR | | ERROR | | | | INTERSECTS | | PARALLELTO The edges lead from a module to its static father (up). The module GEOPLAN and the block prefixed with the name GEOPLAN are in another relation: namely of prefixing, or inheritance. We shall develop this remark later. What is worth noting here is that the structure of GEOPLAN remains intact. This is due to the fact that the class GEOPLAN encapsulates the structure of internal classes and modules.
Let us view a few snapshots.
ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ The initial snapshot shows just one |program | dynamic instance of the main prog- | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | ram. | |GEOPLAN | | | | | | The only instruction to be execu- | | | | ted is the instruction of prefixed | | | | block. | | | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | | | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | | |block | | ---- block prefixed with GEOPLAN | | | | | | | | | | | | | | | | | | | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Just before the first writeln ... instruction ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿SL ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ |program ÃÄÄ<ÄÄÄÄÄÄÄÄÄ´GEOPLAN block | | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ |DL | | | |GEOPLAN | ÆÍÍ<ÍÍÍÍÍÍÍÍ͵ all features of | | | | | | GEOPLAN inhtd. | | | | | | | | | | | | P point none | | | | | | Q point none | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | R point none | | | | C1 circle none | | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | | C2 circle none | | |block | | | C4 circle none | | | | | | L1 line none | | | | | | L2 line none | | | | | | RADIUS real 0 | | | | | | CENTRE | | | | | | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | | | | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This may be a snapshot just before the instruction putting radius to be equal the sum of distances PQ and QR. We omitted all SL links and DL links in order to simplify the picture. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄ¿ |program | |GEOPLAN block | ÚÄÄÄÄÄ´ X real 4 | | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | | | | | Y real 6 | | |GEOPLAN | | | all features of | | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | GEOPLAN inhtd. | | ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | | | | | ÚÄÄÄ´ X real -4| | | | | | P point ÄÄÄÄÄÄÄÅÄÙ | | Y real 88| | | | | | Q point ÄÄÄÄÄÄÄÅÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÙ | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | R point ÄÄÄÄÄÄÄÅÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | C1 circle none | | | X real -9| | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | | C2 circle none | ÀÄÄÄ´ Y real 23| | |block | | | C4 circle none | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | L1 line none | | | | | | L2 line none | | | | | | RADIUS real 0 | | | | | | | | | | | | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | => radius:= ... | | | | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This may be a snapshot just after the three circles were created. ___________________ ___________________ ÚÄÄÄÄÄÄÄÄÄÄ¿ |program | |GEOPLAN block | ÚÄÄÄÄÄ´ X real 4 | | _______________ | | | | | Y real 6 | | |GEOPLAN | | | all features of | | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | GEOPLAN inhtd. | | ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | | | | | ÚÄÄÄ´ X real -4| | | | | | P point ÄÄÄÄÄÄÄÅÄÙ | | Y real 88| | | | | | Q point ÄÄÄÄÄÄÄÅÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÙ | |_____________| | | R point ÄÄÄÄÄÄÄÅÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | C1 circle ÄÄÄÄÄÄÅÄÄ¿| | X real -9| | _______________ | | C2 circle ÄÄÄÄÄÄÅÄ¿|ÀÄÄÄ´ Y real 23| | |block | | | C4 circle ÄÄÄÄÄÄÅ¿|ÀÄÄ¿ ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | L1 line none ||ÀÄÄ¿| ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | | | L2 line none |ÀÄ¿ |ÀÄ´CENTER P | | | | | | RADIUS real 0 | | | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | | | ÀÄÄÂÄÄÄÄÄÄÄÄÄÄ¿ | | | | | | | |CENTER Q | | |_____________| | | => L1:= ... | | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | ÚÄÄÄÄÄÄÄÄÄÄ¿ |_________________| |_________________| ÀÄÄÄÄ´CENTER R | ÀÄÄÄÄÄÄÄÄÄÄÙ
This may be a snapshot of situation in which two lines were created and their intersection point was found.
ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄ¿ |program | |GEOPLAN block | ÚÄÄÄÄÄ´ X real 4 | | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | | | | | Y real 6 | | |GEOPLAN | | | all features of | | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | GEOPLAN inhtd. | | ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | | | | | ÚÄÄÄ´ X real -4| | | | | | P point ÄÄÄÄÄÄÅÄÙ | | Y real 88| | | | | | Q point ÄÄÄÄÄÄÅÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÙ | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | R point ÄÄÄÄÄÄÅÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | C1 circle ÄÄÄÄÄÄÅÄÄ¿| | X real -9| | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | | C2 circle ÄÄÄÄÄÄÅÄ¿|ÀÄÄÄ´ Y real 23| | |block | | | C4 circle ÄÄÄÄÄÄÅ¿|ÀÄÄ¿ ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | L1 line ÄÄÄÄÄÄ¿||ÀÄÄ¿| ÚÄÄÄÄÄÄÄÄÄÄ¿ | | | | | L2 line ÄÄÄÄÄ¿||ÀÄ¿ |ÀÄ´CENTER P | | | | | | RADIUS real 0 |ÀÅÄ¿| | ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | CENTRE Ä¿ | | || ÀÄÄÂÄÄÄÄÄÄÄÄÄÄ¿ | | | | | | | | || |CENTER Q | | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | => if CE|NTRE | | || ÀÄÄÄÄÄÄÄÄÄÄÙ | | | | | | || ÚÄÄÄÄÄÄÄÄÄÄ¿ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÅÄÙ |ÀÄÄÄÄ´CENTER R | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ | | ÀÄÄÄÄÄÄÄÄÄÄÙ ÚÄÄÄÄÄÄÄÄÁÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ | ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ | X coeff. of | |A real ... | ÀÄ´A real ... | | Y solution | |B real ... | |B real ... | | | |C real ... | |C real ... | ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Exercises. Add drawing functions as attributes of classes point, line, circle. Write the algorithm inverting a point w.r.t. a given circle.Andrzej Salwicki