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

  1.                           -- Chapter 13 - Programming Exercise 2
  2.                                        -- Chapter 13 - Program 5
  3. with Text_IO, Unchecked_Deallocation;
  4. use Text_IO;
  5.  
  6. procedure Ch13_2c is
  7.  
  8.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  9.    use Int_IO;
  10.  
  11.    type MY_RECORD is
  12.       record
  13.          Age     : INTEGER;
  14.          Initial : CHARACTER;
  15.          Sex     : CHARACTER;
  16.       end record;
  17.  
  18.    type ACCESS_MY_DATA is access MY_RECORD;
  19.  
  20.    procedure Free is new
  21.                Unchecked_Deallocation(MY_RECORD,ACCESS_MY_DATA);
  22.  
  23.    Myself  : ACCESS_MY_DATA;
  24.    Class   : array(1..10) of ACCESS_MY_DATA;
  25.  
  26. begin
  27.    Myself := new MY_RECORD;
  28.  
  29.    Myself.Age := 34;
  30.    Myself.Initial := 'D';
  31.    Myself.Sex := 'M';
  32.  
  33.    for Index in 1..10 loop
  34.       Class(Index) := new MY_RECORD;
  35.       Class(Index).all := Myself.all;
  36.    end loop;
  37.  
  38.    Class(3).Age := 30;
  39.    Class(3).Initial := 'A';
  40.    Class(5).Initial := 'Z';
  41.    Class(8).Initial := 'R';
  42.    Class(6).Sex := 'F';
  43.    Class(7).Sex := 'F';
  44.    Class(2).Sex := 'F';
  45.  
  46.    for Index in 1..10 loop
  47.       Put("The class members age is");
  48.       Put(Class(Index).Age,3);
  49.       if Class(Index).Sex = 'M' then
  50.          Put(" and his initial is ");
  51.       else
  52.          Put(" and her initial is ");
  53.       end if;
  54.       Put(Class(Index).Initial);
  55.       New_Line;
  56.    end loop;
  57.  
  58.    for Index in 1..10 loop
  59.       Free(Class(Index));
  60.    end loop;
  61.    Free(Myself);
  62.  
  63. end Ch13_2c;
  64.  
  65.  
  66.  
  67.  
  68. -- Result of execution
  69.  
  70. -- The class members age is 34 and his initial is D
  71. -- The class members age is 34 and her initial is D
  72. -- The class members age is 30 and his initial is A
  73. -- The class members age is 34 and his initial is D
  74. -- The class members age is 34 and his initial is Z
  75. -- The class members age is 34 and her initial is D
  76. -- The class members age is 34 and her initial is D
  77. -- The class members age is 34 and his initial is R
  78. -- The class members age is 34 and his initial is D
  79. -- The class members age is 34 and his initial is D
  80.  
  81.