Nested Records and Composite Objects


Composite Objects and Class Line


Type Line: A Composite Object in Ada

           type Pair is record        
              x: Integer;            
              y: Integer;
           end record;

           type Line is record
              start: Pair;
              end: Pair;
           end record;

           myLine: Line;

           ...

        begin

           myLine.start.x := 1;
           myLine.start.y := 2;

           put(myLine.start);

           myLine.end := (3, 4);

           put(myLine.end);

Composite Objects: Reference and Value Semantics


An Array of Lines


Another Example Array of Records

with Ada.Integer_Text_IO; use  Ada.Integer_Text_IO;
with Ada.Text_IO;         use  Ada.Text_IO;

procedure tryArrayOfRecords is

   type Triple is record        
      x: Integer;
      y: Integer;
      z: Integer;
   end record;

   procedure putTriple(p: Triple) is
   begin
      put(p.x); put(p.y); put(p.z); new_line;
   end putTriple;

   type TripleArray is array (1 .. 3) of Triple;

   theTriples1: TripleArray;
   theTriples2: TripleArray;

   t1: Triple;                  

begin
   theTriples1 := (3 => (3, 3, 3), 
                   others => (y => 2, others => 1));

   for i in theTriples1'Range loop
      putTriple( theTriples1(i) );
   end loop;
   new_line;

   t1 := theTriples1(2);
   t1.x := 9;
   putTriple(t1);
   putTriple(theTriples1(2));
   new_line;

   theTriples1(2).x := 99;
   putTriple(theTriples1(2));
   new_line;

   theTriples2 := theTriples1;
   for i in theTriples2'Range loop
      putTriple( theTriples2(i) );
   end loop;

end tryArrayOfRecords;
-- OUTPUT:
          1          2          1
          1          2          1
          3          3          3

          9          2          1
          1          2          1

         99          2          1

          1          2          1
         99          2          1
          3          3          3


A Type for Words


An Array of Words