home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch28_2.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  1.4 KB  |  68 lines

  1.                        -- Chapter 28 - Programming exercise 2
  2.    generic
  3.       type ITEM is range <>;   -- integer class only
  4.    procedure Exchange_Data(X,Y : in out ITEM);
  5.  
  6.    procedure Exchange_Data(X,Y : in out ITEM) is
  7.    Temp : ITEM;
  8.    begin
  9.       Temp := X;
  10.       X := Y;
  11.       Y := Temp;
  12.    end Exchange_Data;
  13.  
  14. -- This is the beginning of the main program which uses the generic
  15. -- procedure defined above.
  16. with Exchange_Data;
  17. with Text_IO;
  18. use Text_IO;
  19.  
  20. procedure CH28_2 is
  21.  
  22.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  23.    use Int_IO;
  24.  
  25.    type MY_INT is new INTEGER range 1..128;
  26.  
  27.    procedure SwapInt is new Exchange_Data(INTEGER);
  28.    procedure SwapNew is new Exchange_Data(MY_INT);
  29.    procedure SwapFlt is new Exchange_Data(FLOAT);
  30.  
  31.    Index1 : INTEGER := 17;
  32.    Index2 : INTEGER := 33;
  33.    Limit1 : MY_INT  := 3;
  34.    Limit2 : MY_INT  := 7;
  35.  
  36. begin
  37.  
  38.    SwapInt(Index1,Index2);
  39.    SwapNew(Limit1,Limit2);
  40.  
  41.    Put(Index1);
  42.    Put(Index2);
  43.    New_Line;
  44.    Put(INTEGER(Limit1));
  45.    Put(INTEGER(Limit2));
  46.    New_Line(2);
  47.  
  48.    SwapInt(Index1,INTEGER(Limit1));
  49.    SwapNew(MY_INT(Index2),Limit2);
  50.  
  51.    Put(Index1);
  52.    Put(Index2);
  53.    New_Line;
  54.    Put(INTEGER(Limit1));
  55.    Put(INTEGER(Limit2));
  56.    New_Line(2);
  57.  
  58. end CH28_2;
  59.  
  60.  
  61.  
  62.  
  63. -- Result of Execution
  64.  
  65. -- Compile error,
  66. -- Line 29 - must instantiate with integer type for "ITEM".
  67.  
  68.