Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.

Similar presentations


Presentation on theme: "Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type."— Presentation transcript:

1 Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type (homogeneous type). If we want to store data of different types (heterogeneous type) together in one object, we can use an array. Why would we want to do that?

2 There are several reasons: Conceptually, the pieces of data might be related, and we want to store them together. It's easier to move around a bunch of items if we put them in a box first. In this case, moving around means passing to and from functions. It simplifies the programming task by having less things to think about.

3 Struct A struct (for structure) is a way to associate different types of data with a single data object. Unlike an array, which uses numbers (ints starting at 0) to identify each data item, a struct names the data items. So, there will be two names – one for the data object itself (with multiple items) and one name for each particular item.

4 Struct - Example Suppose we want to represent information about a student (a student record). We might have the following items: First name Last name Student id number Birthday Year (Freshman, Sophomore,...) QPA

5 Example (cont'd) We represent a student record with a struct as follows: struct StudentRecord { string firstName; string lastName; int id; int year; double qpa; }; (We'll pretend for the moment we know what strings are.)

6 Example (cont'd) This creates a new type, a StudentRecord, which is a struct with five fields (or components, or member variables), that is, a description of a data object which can hold five pieces of disparate, but related, values. To actually create a data object, we use a declaration statement: StudentRecord sr;

7 Struct Data Object sr data object

8 Accessing the Fields In an array, the different values are indicated by using an index, an int. In a struct, the different fields are accessed by referring to them by the name of the field. The syntax is:. For example, sr.firstName, sr.lastName, sr.qpa This identifier can be used for both setting and retrieving a value.

9 More on Field Names The names of fields are local to their structs, i.e., different structs may use the same field names without confusion. The field names are identifiers and following the usual rules. Field names do have do be distinct within a struct.

10 Structs and Functions Structs may be passed into functions as arguments, just like other data objects. A struct parameter can be call-by-value or call-by- reference. The return type of a function may be a struct.


Download ppt "Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type."

Similar presentations


Ads by Google