wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

The Structure Data Type

 

Structured data objects and Arrays

We have seen in previous sections that objects of simple data type can only store data of one type. That is why they are called simple objects. Whilst an array can provide storage for many data elements, each of these elements must be of the same type within any one array.

There are many situations where we would like to store data of different types within the same data structure. If we were developing a data base program to store details of club membership, for instance, we would need to keep records of lots of individual members each of which has a name, address, age and telephone number. It would be convenient if we could refer to each members record as a single data object. On their own, simple data objects and arrays will not let us do this.


   // Example of a membership database using simple data types.

   char FirstName[10],  // an array of 10 characters
   LastName[10],        // an array of 10 characters
   Address[10];         // an array of 10 characters

   int Age,             // the member's age

   TelephoneNumber;     // the members telephone number

The declarations in the above example will only give us sufficient data objects to store information for one club member. To store information about 100 members we would need 100 copies of the above data objects, all with different name tags. We cannot incorporate the data objects into a 100 element array because there are objects of different data type involved.

By combining simple data types and encapsulating them within a data type called a structure this limitation can be overcome.

The Structure

A structure is a mechanism which allows data objects of different types to be grouped together as a single compound type. The structure is a way of allowing programmers to declare and define their own abstract data types. A structure data type can be used within a program in the same way that a simple data type can be used to instantiate data objects. In particular the elements of an array can each store a complete structure object.

The following glossary of terms may be useful:

 

TERM MEANING
DATA OBJECT An object capable of storing data. A variable or a constant. (A function is allocated memory within the computer and is therefore an object; but it is not a data object because it cannot store data).
ENCAPSULATION The grouping together of a number of items into a structure which allows the items to be treated as a single entity.
BASIC TYPE The object types from which a structure is constructed. Basic types can themselves be simple or compound types.
ABSTRACT DATA TYPE A data type constructed by encapsulating basic types into a structure.
MEMBER An object encapsulated within a structure (this object may or may not be a data object).
FIELD A data object encapsulated within a structure.
DATA MEMBER The same as a field.

Declaring a Structure

A simplified version of the syntax used to declare a structure is:


     struct <TypeTag> { member declarations};

where:
  • struct is a C++ reserved keyword used to proceed the declaration of a structure,
  • <TypeTag> is an identifier name tag for the structure type, and
  • the member declarations are enclosed within braces and terminated by a semi-colon.
For instance, the data declarations in the above club membership database can be encapsulated in a structure with the following declaration:


     // Example of a membership database using a structure.

     struct Member{

        char FirstName[10],  // an array of 10 characters
             LastName[10],   // an array of 10 characters
             Address[10];    // an array of 10 characters

        int Age,             // the member's age
            TelephoneNumber; // the members telephone number
     };

The new abstract data type can be conceptualized as a black box containing its data members.


     Member.
        ______________________________
       |          ___________________ |
       |FirstName|_|_|_|_|_|_|_|_|_|_||
       |          ___________________ |
       | LastName|_|_|_|_|_|_|_|_|_|_||
       |          ___________________ |
       |  Address|_|_|_|_|_|_|_|_|_|_||
       |       _                      |
       |  Age |_|                     |
       |                   _          |
       |  TelephoneNumber |_|         |
       |______________________________|

Once the above structure has been declared we can now use it to declare an array of members like so:


     Member MemberList[100];

Here MemberList is declared as an array with 100 elements, each of which can store an object of type Member.

Dot Notation

Once the data elements have been encapsulated within a structure their individuality is hidden. We can no longer refer to the variable Age without first stating which particular Member object we are referring to. In the MemberList array, for instance, there are 100 elements each containing identical data structures each containing instances of the variable Age. So we need a mechanism which will allow us to access the data fields within the structure. This mechanism is referred to as the dot notation for reasons that will become obvious.

Suppose that we declare a single object of type Member as follows:



     Member aMember;

aMember is now a direct reference to a single instance of an object of type Member.

The data field Age can be accessed by using the dot notation like so:



     Member aMember;
     aMember.Age

For instance, we can assign a value to Age by making the assignment


     aMember.Age = 21;

We can extract the value from Age by writing


     int TempAge = aMember.Age;

Arrays of Structures

Consider the declaration:


     Member MemberList[100];

Here we have declared MemberList to be an array with 100 elements each of which can store a structure of type Member.

We can use the subscript notation to access any particular element like so:



     MemberList[46]     // Access the 47th element

but we need to be very clear about what it is we have actually accessed.

Each element in the MemberList array is a structure of the form:



        ______________________________

       |          ___________________ |

       |FirstName|_|_|_|_|_|_|_|_|_|_||

       |          ___________________ |

       | LastName|_|_|_|_|_|_|_|_|_|_||

       |          ___________________ |

       |  Address|_|_|_|_|_|_|_|_|_|_||

       |       _                      |

       |  Age |_|                     |

       |                   _          |

       |  TelephoneNumber |_|         |

       |______________________________|




We have not yet accessed any individual data members within this structure.

To do that we need to use the dot notation.

We could access the Age field within the 47th element by writing:



     MemberList[46].Age

Similarly we can gain access to the Firstname array like this:


     MemberList[46].FirstName

but this will only give us access to the whole FirstName array, not to individual characters within the array. If we want to access the 5th character within the FirstName in the 34th element of MemberList we would need to write:


     MemberList[33].FirstName[4] = 'H'; // Assign a character