Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.

Similar presentations


Presentation on theme: "More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands."— Presentation transcript:

1 More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands for different things even though the type is the same, we may use a struct. Example: struct Time { int hour, minute, second; };

2 Nested Structs Since a struct is a new type, it can be used where ever types are used, included as the type of a field in another struct. For example, consider the following struct: enum Month { Jan, Feb, Mar, ... }; string monthName[] = { “January”, ... }; struct Date { Month month; int day; int year; };

3 Internal v. External Representation
The last slide hinted at an important concept: the difference between the internal representation and the external representation of data. The internal representation is what the program uses. It should be easy to manipulate, e.g., compare values. The external representation is for the human user – it should be easy to read and understand.

4 Internal v. External (cont'd)
The program converts from the external to the internal representation when reading in. The program converts from the internal to external representation when printing. To read in months, we would convert from strings to the enumerated type, Month. This would need a multi-way if-else statement. To print the months, an array of strings works well.

5 Internal v. External - Example
Reading in: string m; Date date; cin >> m; if(m == “January”) date.month = Jan; else if(month == “February”) date.month = Feb; ... Printing out: cout << monthName[date.month] << endl;

6 Nested Struct - Example
This could be used as part of the StudentRecord struct: struct StudentRecord { string firstName; string lastName; int id; Date dob; int year; double qpa; };

7 Example (cont'd) To access this field, we must first access the date of birth: StudentRecord sr; sr.dob.month = Jan; sr.dob.day = 14; sr.dob.year = 1995; cout << MonthName[sr.dob.month] << “ “ << sr.dob.day << “ “ << sr.dob.year << endl;

8 Boxes in Boxes Note that the dob field is itself a data object. Think of the dob as a box inside of another box, the StudentRecord, sr. As a data object, it can be passed to a function: void printDate(const Date& date) { cout << MonthName[date.month] << “ “ << date.day << “ “ << date.year << endl; } In main: printDate(sr.dob);

9 Arrays of Structs We can have an array of structs. This is common when processing records. The statement: StudentRecord sr[10]; declares (and defines) an array of ten (10) StudentRecords. As with any other array, sr[0] is the first element, a StudentRecord, sr[1], the second, and sr[9], the last.

10 Sorting an Array of structs
for(int i=1; i<10; i++) { StudentRecord x = sr[i]; int j = i-1; while(j >= 0 && sr[j].id > x.id) { sr[j+1] = sr[j]; j--; } sr[j+1] = x; Insertion sort is particularly useful for sorting arrays of structs, since each element is copied just once.

11 Structs with Arrays A struct can also have a field which is an array. For example, the StudentRecord struct could have an array of grades: struct StudentRecord { ... double grades[40]; }; To access a single grade, you must use the field name and then an index: cout << sr.grades[i] << endl;

12 Exercise Write a boolean function, dateGreater, that takes two Dates and returns true if the first date is greater than the second date (comes after), and false otherwise. Then change the sort program to sort the student records by date of birth, from youngest student to oldest.


Download ppt "More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands."

Similar presentations


Ads by Google