home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / OOPTUT34.ZIP / EXAMPLES.TXT < prev    next >
Text File  |  1993-06-12  |  7KB  |  169 lines

  1.               EXAMPLES of OBJECT-ORIENTED PROGRAMS.
  2.               ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4.  The linked list, described in the section 'Dynamic Memory Management and
  5.  Pointers' and shown as an example POINTERS.PAS on the companion tutorial
  6.  diskette PASCALTUTOR, is suitable for implementation as a Turbo Pascal
  7.  object type.  Applications with a strong visual element, involving either
  8.  graphics or windows, are also good subjects for object-oriented design.
  9.  
  10.  Because of its obvious visual display, the graphical example will be
  11.  discussed first. Then the linked list example will be considered.
  12.  
  13.  The windows example is separately described in the various notes on
  14.  Turbo Vision, which is used by this suite of programs to provide the
  15.  Menu Bar, with its pull-down sub-menus, the Status Line and the DeskTop.
  16.  
  17.        ----------------------------------------------------------
  18.  
  19.  The original Borland graphics program FIGDEMO.PAS, which involved points,
  20.  circles and arcs, has been extended so that full lines and rectangles can be
  21.  drawn and moved on the screen. The reader is invited to add elliptic arcs.
  22.  The unit Graph is used to make available all the required graphics methods,
  23.  whilst the unit Figures, which is specific to this example, provides the
  24.  ancestral object types 'Location', 'Point' and 'Circle'.
  25.  
  26.  The object type 'Arc' is seen to be a descendant of the object type
  27.  'Circle', whilst the object type FullLine is a descendant of the object
  28.  type 'Point', defined in the note VMT.TXT. Thus FullLine is declared as:
  29.  
  30.   FullLine = object(Point)
  31.     XLength, YLength  : integer;                 {define the length of line}
  32.     constructor Init(InitX, InitY  : integer;
  33.                      InitXLength, InitYLength : integer);
  34.     procedure Show; virtual;
  35.     procedure Hide; virtual;
  36.   end;
  37.  
  38.  The methods for a FullLine include the initialization of the length of the
  39.  line and then a call to the Line procedure in the Graph unit in order to
  40.  show the line in the foreground color or hide the line by drawing in the
  41.  background color.
  42.  
  43.  {-------------------------------------------------------}
  44.  {  FullLine's method implementation                     }
  45.  {-------------------------------------------------------}
  46.  
  47.    constructor FullLine.Init(InitX, InitY : Integer;
  48.                              InitXLength, InitYLength : Integer);
  49.  
  50.    begin
  51.       Point.Init(InitX, InitY);     {Use the inherited Init method.  }
  52.       XLength := InitXLength;       {Then initialize the new fields, }
  53.       YLength := InitYLength;       {defining the length of the line.}
  54.    end;
  55.  
  56.    procedure FullLine.Show;
  57.    begin                                  {Color is set to foreground color}
  58.       Visible := True;                    {before this procedure is called.}
  59.       Graph.Line(X,Y,X + XLength,Y + YLength);
  60.    end;
  61.  
  62.    procedure FullLine.Hide;
  63.    var
  64.       HoldColor : word;
  65.    begin
  66.       HoldColor := Graph.GetColor;        {Foreground color is stored and}
  67.       Graph.SetColor(GetBkColor);         {then set to background color. }
  68.       Visible := false;
  69.       Graph.Line(X,Y,X + XLength,Y + YLength);  {Line drawn in background  }
  70.       Graph.SetColor(HoldColor);                {color and then foreground}
  71.    end;                                         {color restored.           }
  72.  
  73.        ----------------------------------------------------------
  74.  
  75.  To add an elliptic arc, readers are advised that the procedure in the Graph
  76.  unit is
  77.  
  78.      Ellipse(X, Y:integer; StAngle, EndAngle: word; Xradius, YRadius: word);
  79.  
  80.  This means that the elliptic arc can be a descendant of a circular arc if
  81.  one additional radius field is added, say MinorRadius for YRadius, and the
  82.  original arc Radius is used for the XRadius.
  83.  
  84.  The virtual methods Show and Hide can be created by imitation of the methods
  85.  for FullLine, but changing Graph.Line to Graph.Ellipse with its particular
  86.  parameters.
  87.  
  88.        ----------------------------------------------------------
  89.  
  90.  
  91.  The linked list application is illustrated by the program LISTDEMO.PAS
  92.  (page 117 of the User's Guide - version 6.0). Whereas most of the object
  93.  examples so far have had static instances of object types, named in a 'var'
  94.  declaration and allocated in the data segment, the program LISTDEMO.PAS
  95.  makes use of dynamic objects, which are allocated on the heap and are
  96.  manipulated with pointers.
  97.  
  98.  Objects can be allocated as pointer referents with the New procedure,
  99.  which has been extended to allow initialization within the one operation.
  100.  New can now be invoked with two parameters, the pointer name as the first
  101.  parameter and the constructor invocation as the second parameter, as for
  102.  example:
  103.            New(PCircle,Init(600, 100, 30));
  104.  
  105.  'New' has been further extended to allow it to act as a function returning
  106.  a pointer value. The parameter passed to New is then the type of pointer to
  107.  the object rather than the pointer variable itself, as for example:
  108.  
  109.     type
  110.        ArcPtr = ^Arc;
  111.  
  112.     var
  113.        PArc : ArcPtr;
  114.  
  115.     begin
  116.        ....
  117.        PArc := New(ArcPtr,Init(600, 100, 25, 0, 90);
  118.  
  119.  Turbo Pascal also provides a special type of method called a 'destructor'
  120.  for cleaning up and disposing of dynamically allocated objects. With
  121.  polymorphic objects the destructor has to consult the Virtual Method Table
  122.  for the object instance to determine the size of memory used by that object,
  123.  so that the normal Dispose procedure can deallocate the right amount of
  124.  space on the heap.
  125.  
  126.  In the program LISTDEMO.PAS, the List object is provided with the method:
  127.  
  128.           destructor Done; virtual;
  129.  
  130.  and in the List's method implementations, the code for the destructor,
  131.  List.Done, is provided.  This consists of the 'hand-over-hand' call to
  132.  Dispose, which first removes the content of the list item and then removes
  133.  the pointer to the next item.
  134.  
  135.   .....
  136.   NodePtr = ^Node;
  137.   Node = record            {This is a record on the heap with two fields} <─┐
  138.     Item: PointPtr;        {This points to the object stored on the heap}   │
  139.     Next: NodePtr;         {This points to the next Node on the heap    }   │
  140.   end;                                                                      │
  141.                                                                             │
  142.   ListPtr = ^List;                                                          │
  143.   List = object            {This is an object in static memory which has a} │
  144.     Nodes: NodePtr;        {pointer which points to the top-of-heap Node. } ┘
  145.     constructor Init;
  146.     destructor Done; virtual;
  147.     procedure Add(Item: PointPtr);
  148.     procedure Report;
  149.   end;
  150.  ......
  151.  ......
  152.  ......
  153.  destructor List.Done;
  154.  var
  155.    N: NodePtr;
  156.  begin
  157.     while Nodes <> nil do
  158.    begin
  159.      N := Nodes;             {Assign N to the current top-of-heap Node   }
  160.      Nodes := N^.Next;       {Now assign List.Nodes to point to next Node}
  161.      Dispose(N^.Item, Done); {Dispose of the item pointed to by Nodes    }
  162.      Dispose(N);             {Now dispose of the Node itself.                   }
  163.    end;
  164.  end;
  165.  
  166.  
  167.  EXAMPLES.TXT
  168.  1.6.93
  169.