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

  1.                           -- Chapter 13 - Programming Exercise 2
  2.                                        -- Chapter 13 - Program 2
  3. with Text_IO, Unchecked_Deallocation;
  4. use Text_IO;
  5.  
  6. procedure Ch13_2b is
  7.  
  8.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  9.    use Int_IO;
  10.    package Flt_IO is new Text_IO.Float_IO(FLOAT);
  11.    use Flt_IO;
  12.  
  13.    type POINT_TO_INT is access INTEGER;
  14.    Index,Arrow : POINT_TO_INT;
  15.  
  16.    type POINT_TO_FLOAT is access FLOAT;
  17.    X,Y,Z : POINT_TO_FLOAT;
  18.  
  19.    procedure Free is new
  20.                    Unchecked_Deallocation(INTEGER, POINT_TO_INT);
  21.    procedure Free is new
  22.                     Unchecked_Deallocation(FLOAT,POINT_TO_FLOAT);
  23.  
  24. begin
  25.    Index := new INTEGER'(173);
  26.    Arrow := new INTEGER'(57);
  27.    Put("The values are");
  28.    Put(Index.all);
  29.    Put(Arrow.all);
  30.    New_Line;
  31.    Index.all := 13;
  32.    Arrow.all := Index.all;
  33.    Index := Arrow;
  34.  
  35.    X := new FLOAT'(3.14159);
  36.    Y := X;
  37.    Z := X;
  38.    Put("The float values are");
  39.    Put(X.all,6,6,0);
  40.    Put(Y.all,6,6,0);
  41.    Put(Z.all,6,6,0);
  42.    New_Line;
  43.  
  44.    X.all := 2.0 * Y.all;
  45.    Put("The float values are");
  46.    Put(X.all,6,6,0);
  47.    Put(Y.all,6,6,0);
  48.    Put(Z.all,6,6,0);
  49.    New_Line;
  50.  
  51.    Free(Index);
  52.    Free(X);
  53.  
  54. end Ch13_2b;
  55.  
  56.  
  57.  
  58.  
  59. -- Result of execution
  60.  
  61. -- The values are   173    57
  62. -- The float values are     3.141590     3.141590     3.141590
  63. -- The float values are     6.283180     6.283180     6.283180
  64.  
  65. -- Note that the deallocations could be done with any of the
  66. --  variable names since all variables of the same type are point-
  67. --  ing to the same places.
  68.  
  69. -- Note also that the two procedures could be named something else
  70. --  rather than overloading the name Free.  It would be better
  71. --  practice to use different names for the variables.
  72.  
  73.