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

The Structured Data Object

 

 

 

Arrays:

An array is a data structure containing a fixed number of contiguous storage elements all of the same data type. Arrays can be very useful in storing and accessing many integers, characters, or any other data type.  They are also very useful in working with strings.  For example: This is a representation of an array that stores several numbers. 

 

Text Box:  

 

Text Box:  

 

Text Box:  

 

Text Box:  

 

Text Box:  

 

Text Box:  

 

Text Box:  

 

Text Box:  
 


 

Text Box: 2    5    1    5    7    4    8    6    9    1    2    8    8    4   

Text Box:  

 

 

 

 

Structured Data Objects:

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.

 

A structure is a mechanism that 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.

       
         ____________________  FirstName|_|_|_|_|_|_|_|_|_|_|
         ___________________               LastName|_|_|_|_|_|_|_|_|_|_|
       ____________________    Address|_|_|_|_|_|_|_|_|_|_|
    ___ 
Age|_|_|                      
                ___________                   
TelephoneNumber |_|_|_|_|_|         
       
         _____________________ 
FirstName|_|_|_|_|_|_|_|_|_|_|
         ___________________               LastName|_|_|_|_|_|_|_|_|_|_|
       ____________________    Address|_|_|_|_|_|_|_|_|_|_|
    ___ 
Age|_|_|                      
                ___________                   
TelephoneNumber |_|_|_|_|_|         
         ____________________
FirstName|_|_|_|_|_|_|_|_|_|_|
         ___________________               LastName|_|_|_|_|_|_|_|_|_|_|
       ____________________    Address|_|_|_|_|_|_|_|_|_|_|
    ___ 
Age|_|_|                      
                ___________                   
TelephoneNumber |_|_|_|_|_|         
         ____________________
FirstName|_|_|_|_|_|_|_|_|_|_|
         ___________________               LastName|_|_|_|_|_|_|_|_|_|_|
       ____________________    Address|_|_|_|_|_|_|_|_|_|_|
    ___ 
Age|_|_|                      
                ___________                   
TelephoneNumber |_|_|_|_|_|         
 

 

 

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;

 

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

 

Here is another example of a program using a structured data object:

 

struct database
{
  int id_number;
  int age;
  float salary;
};
int main()
{
  database employee;  //There is now an employee variable that has modifiable 
                                                  //variables inside it.
  employee.age=22;
  employee.id_number=1;
  employee.salary=12000.21;
  return 0;
}

 

This is a reference to help you further understand Structured Data Object:

 

http://www.fredosaurus.com/notes-cpp/structs/struct-ops.html

 

 

Questions:

 

 

1.    Which of the following is a properly defined struct?
            A. struct {int a;}
            B. struct a_struct {int a;}                               
Answer:  D
            C. struct a_struct int a;
            D. struct a_struct {int a;};

 

 2.    Which of these types of data types can a struct have?

 

 A. char

 B. int                                                              Answer:  D

 C. float

 D. or all of the above

3.  How many bytes of storage are needed for a Struct?

           

            A. It depends on the Struct

            B. It depends on the number of fields                 answer:  A

            C. It depends on the number chars

            D. All of the above

 

4.  A Struct is essentially a data type.

 

            A. True

            B. False                                                               answer: A

 

5.  What is the advantage of using a struct array?

 

            Answer: When using a struct you can have data elements of different types.