home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / maj / 666 / h8.hlp < prev    next >
Text File  |  1994-02-06  |  7KB  |  234 lines

  1.                             CLASSES
  2.  
  3. class iden ( op-param-list op-decl)    class definition
  4.   {                                    where op-param-list is an
  5.     iden ( op-param-list ) block       optional parameter list, op-decl
  6.   } ;                                  is an optional list of local
  7.                                        variables with the form ; iden-list
  8.                                        and block consists of one or more
  9.                                        statements enclosed by braces
  10.  
  11.                                        Example
  12.  
  13.                                        class queue (;rep) {
  14.                                         queue() {rep = [];}
  15.                                         enqueue(x) {rep = rep conc [x];}
  16.                                         dequeue(;y) {y = hd rep;
  17.                                                      rep = tl rep;
  18.                                                      return y;}};
  19.  
  20.                              INHERITANCE
  21.  
  22. class iden1 ( ... ) : iden2             where iden2 is the superclass name
  23.   { ...
  24.                                         Example
  25.  
  26.                                         class deque (;rep) : queue {
  27.                                          ...
  28.  
  29.  
  30. A class is defined by giving the class name, followed by an
  31. enumeration of instance variables in the form of a
  32. parameter list, followed by a collection of function definitions
  33. (also called methods). Classes, contrary to C++, may only contain
  34. functions.
  35.  
  36. In order to motivate the definition and usage of a class, let us first
  37. define a simple queue. It does not matter what type of item the queue
  38. contains; we will assume a queue of integers. In the specification
  39. below, the state component q (a global variable) represents the queue
  40. as a sequence, which is initialized as the empty sequence.
  41.  
  42. q=[];
  43. enqueue(x) {q=q conc [x];};
  44. dequeue(;x) {x=hd q;q=tl q;return x;};
  45. empty() {return q==[];};
  46. length() {return len q;};
  47.  
  48. A typical sequence of operations is:
  49.  
  50. ? enqueue(3);
  51.  
  52. ? enqueue(5);
  53.  
  54. ? length();
  55.  2
  56.  
  57. ? dequeue();
  58.  3
  59.  
  60. ? empty();
  61.  false
  62.  
  63. ? dequeue();
  64.  5
  65.  
  66. ? empty();
  67.  true
  68.  
  69. There are drawbacks to this approach. Only one queue is available, and
  70. its representation is completely exposed. The problem can be solved by
  71. defining a queue class.
  72.  
  73. class queue(rep) {
  74.  
  75. enqueue(x) {rep=rep conc [x];}
  76. dequeue(;y) {y=hd rep;rep=tl rep; return y;}
  77. empty() {return rep==[];}
  78. length() {return len rep;}};
  79.  
  80. The coding is very similar except now the state component (rep) is local
  81. and is hidden inside the class definition. The representation is
  82. initialized from the outside and may only be changed indirectly by
  83. invoking either the enqueue or dequeue operation. The analogous sequence
  84. of operations is:
  85.  
  86. ? q=new queue([]);
  87.  
  88. ? q.enqueue(3);
  89.  
  90. ? q.enqueue(5);
  91.  
  92. ? q.length;                    or q.length();
  93.  2
  94.  
  95. ? q.dequeue;                   or q.dequeue();
  96.  3
  97.  
  98. ? q.empty;                     or q.empty();
  99.  false
  100.  
  101. ? q.dequeue;                   or q.dequeue();
  102.  5
  103.  
  104. ? q.empty;                     or q.empty();
  105.  true
  106.  
  107. ? q;
  108.  object              this shows the printed representation of an object
  109.  
  110. A second queue may be instantiated by using the new operation again.
  111.  
  112. p=new queue([]);
  113.  
  114. and an arbitrary number of queues may be created in this way.
  115.  
  116. An example of a priority queue follows.
  117.  
  118. class pqueue(rep) {
  119.  
  120.  insrt(x,y) {if(y == []) {rep = [x]; return rep;} else
  121.                      if(x < hd y) {rep = [x] conc y;return rep;} else
  122.                      {rep = [hd y] conc insrt(x,tl y);
  123.                      return rep;}}
  124.  
  125.  insert(x) {rep = insrt(x,rep); return rep;}
  126.  remove(;x) { if(rep == []) return "queue empty";
  127.                   x = hd rep; rep = tl rep; return x;} };
  128.  
  129.  
  130. The last statement in the insert function returns the value of rep
  131. so that the user can see that the items are inserted in sorted
  132. order.Alternatively, the string "ok" could be returned. Note the
  133. declaration of the local variable x in the header of the remove
  134. function. The following sequence of operations show calls to
  135. the insert and remove functions:
  136.  
  137. ? q= new pqueue([]);
  138.  
  139. ? q.insert(3);
  140.  [3]
  141. ? q.insert(2);
  142.  [2,3]
  143. ? q.insert(9);
  144.  [2,3,9]
  145. ? q.insert(7);
  146.  [2,3,7,9]
  147. ? q.remove;
  148.  2
  149. ? q.remove;
  150.  3
  151. ? q.remove;
  152.  7
  153. ? q.remove;
  154.  9
  155. ? q.remove;
  156.  queue empty
  157.  
  158. CLASS CONSTRUCTORS
  159.  
  160. When a class constructor is defined by the user, by declaring a method
  161. with the same name as the class, the corresponding class object will
  162. be initialized automatically when it is instantiated. Taking the queue
  163. example again:
  164.  
  165. class queue(;rep) {
  166.  
  167. queue() {rep=[];}
  168. enqueue(x) {rep = rep conc [x];}
  169. dequeue(;y) {y = hd rep; rep = tl rep; return y;}};
  170.  
  171. An instantiation for this class would have the syntax
  172.  
  173. iden = new queue();
  174.  
  175. where iden represents the name of the instantiated object.
  176.  
  177. Note that rep is defined as a local variable of the class, and that
  178. the first method, named queue, initializes rep to the empty sequence.
  179. When an object is instantiated, i.e. q = new queue(), the rep of that
  180. object will be initialized.
  181.  
  182.  
  183. INHERITANCE
  184.  
  185. A class can inherit instance variables and operations from another
  186. class, called the superclass (or base class). The inheriting class
  187. is called the subclass (or derived class). Suppose that we first
  188. define a queue class:
  189.  
  190. class queue(;rep) {
  191.  queue() {rep=[];}
  192.  insert(x) {rep=rep conc [x];}
  193.  remove(;y) {y=hd rep;rep=tl rep; return y;}
  194.  empty() {return rep==[];}};
  195.  
  196. The characteristics of the queue is that items are inserted at one
  197. end and removed from the other end. The queue is represented by a
  198. sequence which is initialized by the empty sequence. Suppose we wish
  199. to define a deque by specializing the operations which are already
  200. provided by the queue. A deque is characterized by allowing insertions
  201. and removals to be performed at either end. We can obtain a deque class
  202. by defining it to be a subclass of queue:
  203.  
  204. class deque (;rep) : queue {
  205.  deque() {rep=[];}
  206.  insert_front(x) {rep=[x] conc rep;}
  207.  remove_rear(;y) {y=hd (last rep);rep=butlast rep;return y;}
  208.  empty(;x) {if(rep==[]) return "yes";}};
  209.  
  210. Since insertions to and removals from the queue were made from the
  211. rear and front respectively, we define two new operations insert_front
  212. and remove_rear. So the new class deque inherits the operations insert
  213. and remove from queue, its superclass and defines the two new operations.
  214. The syntax ": queue" which appears in the header defines queue as the
  215. superclass. Since the operation empty has the same name as an operation
  216. in the super class, the new definition of empty supercedes the previous
  217. one.
  218.  
  219. Consider the following class definitions.
  220.  
  221. class rec (;value) {
  222.  rec() {value=0;}
  223.  get() {return value;}};
  224.  
  225. class newrec () : rec {
  226.  newrec() {value=0;}
  227.  get() {return value;}
  228.  setval(x) {value=x;}};
  229.  
  230. Class rec has the instance variable value which is set to 0 by the
  231. class constructor. The class newrec defines no instance variables itself
  232. but inherits value from its superclass. Value is also initialized by
  233. the class constructor of rec.
  234.