Tuesday 4/2 Week 11

Chapter 9

 

Objectives

Records

 

1.  What is a record?

·        A record is a

·         

·         

·        Example: an entry in an address book

·        One entry might contain:

                  Last Name

         First Name

         Street address

         City

         State

         Zip Code

         Area Code

         Phone number

         Birth date

·         

·         

·         

·         

·        NameSize : CONSTANT := 20;

SUBTYPE NameType IS String(1..NameSize);

SUBTYPE StreetType IS String(1..2*NameSize);

SUBTYPE StateType IS String(1..2);

SUBTYPE ZipType IS Positive RANGE 00000..99999; 

SUBTYPE AreaCodeType IS Positive RANGE 111..999;

SUBTYPE PhoneType IS Positive RANGE 1111111..9999999;

 

TYPE AddressEntry IS RECORD

            LastName : NameType;

            FirstName : NameType;

            SteetAddress : StreetType;

            City : NameType;

            State : StateType;

            ZipCode: ZipType;

            AreaCode : AreaCodeType;

            Phone : PhoneType;

            BirdthDate : Date;

      END RECORD;

 

·        This record is now a new type and we can declare variables of type AddressEntry

                  Julie : AddressEntry;

         Dad : AddressEntry;

 

 

2.  Getting at the information in a RECORD

·        a                                  which is the varname.field

·        Julie.LastName := “Strain              “;

Julie.FirstName := “Julie               “;

Julie.StreetAddress := “15121 Sobey Rd.                         “;

Julie.City := “Yreka               “;

Julie.State := “CA”;

Julie.ZipCode := “95090”;

Julie.AreaCode := “949”;

Julie.Phone := “2345678”;

Julie.Birthday := Dates.MakeDate(12, 31, 1980);

 

·        Once each field has a value, the field selector is used like any other variable

Ada.Text_IO.Put(Item => Julie.City);

 

·         E.g. page 371

 

3.  Reading in records

·         

·         

·         

·         

·        E.g. page274

 

4.  Aggregate Assignment

·         

·         

·         

·        It is also possible to use an aggregate assignment, so that we may assignment all of the parts of a record with one statement.

 

Julie := (LastName => “Strain              “,

      FirstName => “Julie               “,

      StreetAddress => “15121 Sobey Rd.                         “,

      City => “Yreka               “,

      State => “CA”,

      ZipCode => “95090”,

      AreaCode => “949”,

      Phone => “2345678”,

      Birthday => Dates.MakeDate(12, 31, 1980));

 

·        In an aggregate assignment, you must

·         

·         

·        It is also possible to assign the parameters with out the formal names, but

·         

·         

·        In general  we use the formal parameters because it is much more clear and avoids accidentally incorrectly assigning a field.

 

5.   Hierarchical records

·         

·         

·         

·        We can have records whose fields are records. See BirthDate for an example

·        Page 381, type Employee

·        When a field is also a record it is accessed like varname.fieldrec1.field

·        Page 383, reading in a hierarchical record