Previous topic | Next topic | Ada Home Page | Index

Hierarchical records

The components of a record can be any type, including another record

For example, a name can be a record with three components. A record of information about a person can thus contain a record for the name.

   max_title_size : constant := 4;
   max_fname_size : constant := 15;
   max_sname_size : constant := 20;

   subtype titles is STRING( 1 .. max_title_size);
   subtype fnames  is STRING( 1 .. max_fname_size);
   subtype snames  is STRING( 1 .. max_sname_size);

   -- other constants & types as before

   type names is record
       title : titles := ( others => space); -- title
       fname : fnames := ( others => space); -- first name
       sname : snames := ( others => space); -- surname
   end record;

   type complex_persons is record
       name  : names;
       phone : phones := ( others => space); -- phone no
       sex   : sexes;         		-- sex of person
       age   : ages;				-- age of person
       weight: weights;			-- weight of person
   end record;

   a_person : complex_persons;		-- a person

Refer to inner components using two dot operators

You can have as many records within records as needed. Good style suggests limiting the number of levels.


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96