home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / OOF20.ZIP / DEMO.PAS next >
Encoding:
Pascal/Delphi Source File  |  1987-09-19  |  5.9 KB  |  219 lines

  1. program Demo;
  2. {                          Version 2.0                        87/09/19
  3.  
  4.   Example of Object Oriented Programming in TURBO Pascal.
  5.  
  6.   Author:   Mike Babulic
  7.             3827 Charleswood Drive N.W.
  8.             Calgary, Alberta
  9.             CANADA
  10.             T2L 2C7
  11.  
  12.             Compuserve Id.:  72307,314
  13.  
  14.  
  15. This program demonstrates how to use OOF.INC to extend (or "Fudge")
  16. Turbo Pascal to include Object Oriented Programming concets.
  17.  }
  18.  
  19. {------------------------- Debugging Tools ----------------------------------}
  20.  
  21. type str255 = string[255];
  22.  
  23. procedure WAIT;
  24.   var c : Char;
  25.   begin
  26.     writeln; writeln('Press any key ...');
  27.     repeat until keypressed;
  28.     gotoXY(1,whereY-1);
  29. {    ClrScr;}
  30.   end;
  31.  
  32. procedure w(s:str255);
  33.   begin
  34.     writeln('DEBUG -- ',s);
  35.     wait;
  36.   end;
  37.  
  38. procedure stack(n:integer);
  39.   var s : array [-1..-1] of integer absolute n;
  40.       i : integer;
  41.   begin
  42.     i := 2-ofs(n);
  43.     write('SP = ',-i,' => ');
  44.     if i < 0 then i := 0;
  45.     if i<n+n then n := i shr 1 + 1;
  46.     for i := 1 to n do write(s[i],' /');
  47.     writeln;
  48.   end;
  49.  
  50. procedure PC(n:integer); {call to pc is 13 bytes}
  51.   var s : array [0..0] of integer absolute n;
  52.       i : integer;
  53.   begin
  54.      i := -1;
  55.      writeln('PC = ',s[i]+n);
  56.   end;
  57.  
  58. {----------------------------------------------------------------------------}
  59.  
  60.  
  61. {$I OOF.INC}      {Import Object Programming routines}
  62.  
  63. {---------------------- This Class counts by 1 ------------------------}
  64.  
  65. TYPE    TOnesees = record
  66.           parent    : TObject;
  67.           value     : Integer;
  68.           end;
  69.  
  70. {MESSAGES}
  71.  
  72.     procedure Assign(x:integer; var self);      begin Message(0); end;
  73.  
  74.     function Equals(var self):integer;          begin Message(3); end;
  75.  
  76.     procedure Up(var self);                     begin Message(6); end;
  77.  
  78. {METHODS}
  79.  
  80.     procedure TOnesees_Assign(x:integer; var self:TOnesees);  forward;
  81.  
  82.     function TOnesees_Equals(var self:TOnesees):integer;  forward;
  83.  
  84.     procedure TOnesees_Up(var self:TOnesees);  forward;
  85.  
  86.     function TOnesees_GetParent(var self:Class):Class;  forward; {CLASS METHOD}
  87.  
  88. {DISPATCHER}
  89.  
  90.     procedure COnesees(message,no:integer);
  91.       begin
  92.         if (message>=ofs(assign)) and (message<=ofs(Up)) then
  93.           DoMethod(ofs(TOnesees_assign)+no)
  94.         else if message=ofs(GetParent) then
  95.           DoMethod(ofs(TOnesees_GetParent))
  96.         else
  97.           DoParent(ofs(cObject)); {Faster Compile}
  98.         {should never get here}
  99.           stack(10);
  100.           writeln(' eek! ',ofs(assign),', ',message);
  101.           halt
  102.       end;
  103.  
  104. {IMPLEMENTATION}
  105.  
  106.     procedure TOnesees_Assign{x:integer; var self:TOnesees};
  107.       begin with self do begin
  108.         value := x;
  109.       end  end;
  110.  
  111.     function TOnesees_Equals{var self:TOnesees):integer};
  112.       begin with self do begin
  113.         TOnesees_Equals := value;
  114.       end  end;
  115.  
  116.     procedure TOnesees_Up{var self:TOnesees};
  117.       begin with self do begin
  118.         value := value + 1;
  119.       end  end;
  120.  
  121.     function TOnesees_GetParent{var self:Class):Class;  (CLASS METHOD};
  122.       begin
  123.         TOnesees_GetParent := ofs(CObject);
  124.       end;
  125.  
  126.  
  127. {---------------------- This Class counts by 2 ----------------------------}
  128.  
  129. TYPE    TTwosees = TOnesees;
  130.  
  131. {METHODS}  {ok, ok ... so I cheated a little, this bit is small enough!!!}
  132.  
  133.     procedure TTwosees_Up(var self: TTwosees);  {OVERRIDE METHOD so no message}
  134.       begin with self do begin
  135.         value := value + 2;
  136.       end  end;
  137.  
  138.     function TTwosees_GetParent(var self:Class):Class; {CLASS METHOD}
  139.       begin
  140.         TTwosees_GetParent := ofs(COnesees);
  141.       end;
  142.  
  143. {DISPATCHER}
  144.  
  145.     procedure CTwosees(message,no:integer);
  146.       begin
  147.         if message=ofs(Up) then  {OVERRIDE METHOD}
  148.           DoMethod(ofs(TTwosees_Up))
  149.         else if message=ofs(GetParent) then
  150.           DoMethod(ofs(TTwosees_GetParent))
  151.         else
  152.           DoParent(ofs(COnesees));
  153.       end;
  154.  
  155.  
  156. {----------------------------------------------------------------------------}
  157.  
  158.  
  159. VAR
  160.     a: TOnesees;
  161.     b: TTwosees;
  162.  
  163. begin
  164.  
  165.   ClrScr;
  166.   writeln('----------------------- Initialization ------------------------');
  167.  
  168.   writeln;
  169.   writeln('Notice that OOF uses the STACK and NOT THE HEAP to store objects.');
  170.   writeln('  This is unlike most object oriented programming systems. They');
  171.   writeln('  are almost always heap based.');
  172.   writeln;
  173.   writeln('Why is OOF stack based? Well, I''m not just being ornery ...');
  174.   writeln;
  175.   writeln('      1) Garbage collection - this is trivial (and extremely fast)');
  176.   writeln('              with stack-based objects.');
  177.   writeln;
  178.   writeln('      2) Safety - a programmer using Object Pascal must dispose of ');
  179.   writeln('              (and C++ programmes must free) objects when they are finished');
  180.   writeln('              with them. This creates a danger of DANGLING POINTERS.');
  181.   writeln;
  182.   writeln('      3) Appropriate Model - the vast majority of objects are created by the');
  183.   writeln('              method that uses them. Why complicate things with a handle in');
  184.   writeln('              the stack AND an object in the heap?');
  185.   writeln;
  186.  
  187.   SetClass(a,ofs(COnesees));
  188.   SetClass(b,ofs(CTwosees));
  189.  
  190.   WAIT;
  191.   writeln('-- Count by 1 --------- ONEsees Class -------------------------');
  192.   writeln;
  193.  
  194.   Assign(0,a);
  195.  
  196.   Up(a); write(Equals(a),', ');
  197.   Up(a); write(Equals(a),', ');
  198.   Up(a); write(Equals(a));
  199.   writeln;
  200.  
  201.   writeln;
  202.   writeln('GetParent(a)=ofs(CObject) is ',GetParent(a)=ofs(CObject));
  203.   writeln;
  204.  
  205.   WAIT;
  206.   writeln('-- Count by 2 --------- TWOsees Class ------------------------');
  207.   writeln;
  208.  
  209.   Assign(0,b);
  210.  
  211.   Up(b); write(Equals(b),', ');
  212.   Up(b); write(Equals(b),', ');
  213.   Up(b); write(Equals(b),', ');
  214.   writeln;
  215.  
  216.   writeln;
  217.   writeln('GetParent(b)=ofs(COnesees) is ',GetParent(b)=ofs(COnesees));
  218.  
  219. end.