Loglan 82, A micro-manual of the programming language - Basic constructs and facilities

5) Classes


Class is a facility which covers such programming constructs as structured type, package, access type, data structure etc. To begin with the presentation of this construct, let us consider a structured type assembled from primitive ones:

The above class declaration has the attributes : dollars (real), not_paid (boolean), and year,month,day (integer). Wherever class bill is visibile one can declare variables of type bill:

var x,y,z: bill

The values of variables x, y, z can be the addresses of objects of class bill. These variables are called reference variables. With reference variable one can create and operate the objects of reference variable type.

An object of a class is created by the class generation statement (new), and thereafter, its attributes are accessed through dot notation.

If an object of class bill has been created (new bill) and its address has been assigned to variable x (x:=new bill), then the attributes of that object are accessible through dot notation (remote access). The expression x.dollars gives , for instance, the remote access to attribute dollars of the object referenced by x. All attributes of class objects are initialized as usual. For the above example the object referenced by x, after the execution of the specified sequence of statements, has the following structure:

The object referenced by y and z has the following structure:

The value none is the default initial value of any reference variable and denotes no object. A remote access to an attribute of none raises a run time error.

Class may have also formal parameters (as procedures and functions). Kinds and transmission modes of formal parameters are the same as in the case of procedures.

Example:

Let, for instance, variables z1, z2, z3 be of type node. Then the sequence of statements:

creates the structure:

where arrows denote the values of the reference variables.

Class may also have a sequence of statements (as any other unit). That sequence can initialize the attributes of the class objects.

Example:

Attribute module is evaluated for any object generation of class complex:

For the execution of a class generator, first a class object is created, then the input parameters are transmitted , and finally, the sequence of statements (if any) is performed. Return is made with the execution of return statement or the final end of a unit. Upon return the output parameters are transmitted.

Procedure object is automatically deallocated when return is made to the caller. Class object is not deallocated , its address can be assigned to a reference variable, and its attributes can be thereafter accessed via this variable.

The classes presented so far had only variable attributes. In general, class attributes may be also other syntactic entities, such as constants, procedures, functions, classes etc. Classes with procedure and function attributes provide a good facility to define data structures.

Example:

A push_down memory of integers may be implemented in the following way:

Assume that somewhere in a program reference variables of type push_down are declared (of course, in place where push_down is visibile):

Three different push_down memories may be now generated:

One can use these push_down memories as follows:

etc.



Last update 02/07/95
Comments, suggestions and critiques are welcome to : linfo062@crisv2.univ-pau.fr