Download presentation
Presentation is loading. Please wait.
Published byAntonia Wilson Modified over 9 years ago
1
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures u Accessing the Array of Structures u Rules When using Structures u Case Study u Exercise/Home Work Dr. Ming Zhang
2
Syntax of a struct Declaration u Structures Structures ae aggregate data types built using elements of other types including other struct. u Syntax of a sturct struct structure_name { …… Declaratioons for the components making up the sturcture …… }; // must end with a semicolon! Dr. Ming Zhang
3
Example of Structure Structure definition Structure name struct fruit_case { //struct likes a record int number; // member, or field float average_weight; // member, or field Char name[21]; // member, or field }; Dr. Ming Zhang
4
Structure Variables u Declare Structure Variables So the structure fruit_case thus created is similar to a type, in that we can declare variables using it: fruit_case orange_case, apple_case; u Structure Variables orange_case and apple_case are structure variables, each one contains the three component number, average_weight, and name. Dr. Ming Zhang
5
orange_case and apple_case orange_case: apple_case: number: average_weight: average_weight: name: Dr. Ming Zhang
6
Accessing Members of Structures u Dot Operator (. ) The dot operator (field selection) accesses a structure or class member via the variable name for the object or via a reference to the object. orange_case.average_weight = 4.8; orange_case.number = 64; orange_case.name = “Oranges”; Dr. Ming Zhang
7
Accessing orange_case orange_case.average_weight = 4.8; orange_case.number = 64; orange_case.name = “Oranges”; orange_case: apple_case: number: 64 number: average_weight: 4.8 average_weight: name: Oranges name: Dr. Ming Zhang
8
A Structure is Single Variable apple_case = orange_case; orange_case: apple_case: number: 64 average_weight: 4.8 average_weight: 4.8 name: Oranges Dr. Ming Zhang
9
Initialize Structure Variables fruitcase banana_case = { 96, 0.2, “Bananas”}, tomato_case = {64, 0.5, “Tomatoes”}; banana_case: tomato_case: number: 96 number: 64 average_weight: 0.2 average_weight: 0.5 name: Bananas name: Tomatoes Dr. Ming Zhang
10
Array of Structures fruitcase cases[4]; cases[1] = banana_case = { 96, 0.2, “Bananas”}, cases[2]=tomato_case = {64, 0.5, “Tomatoes”}; banana_case: tomato_case: number: 96 number: 64 average_weight: 0.2 average_weight: 0.5 name: Bananas name: Tomatoes Dr. Ming Zhang
11
Accessing the Array of Structures fruitcase cases[4];cases[3] = strawberry_case; cases[3].number =24; cases[3].average_weight=0.1; cases[3].name=“Strawberry”; strawberry_case: tomato_case: number: 24 number: 64 average_weight: 0.1 average_weight: 0.5 name: Strawberry name: Tomatoes Dr. Ming Zhang
12
Rules When using Structures u We may not have two fields of the same name in the one structure. u We may have two fields of the same name in different structures. u A structure must have at least one field. u A structure may contain fields which are themselves structures, but it may not contain another copy of itself. u A structure can contain arrays, and arrays can have structures as elements. Dr. Ming Zhang
13
Case Study: Time Structure (Figure 6.1) (1) #include using std::cout; using std::endl; struct Time { // structure definition int hour; // 0-23 int minute; // 0-59 int second; // 0-59 }; void printMilitary( const Time & ); void printStandard( const Time & ); Dr. Ming Zhang
14
Case Study: Time Structure (Figure 6.1) (2) int main() { Time dinnerTime;//variable of new type Time // set members to valid values dinnerTime.hour = 18; dinnerTime.minute = 30; dinnerTime.second = 0; cout << "Dinner will be held at "; printMilitary( dinnerTime ); cout << " military time,\nwhich is "; printStandard( dinnerTime ); cout << " standard time.\n"; Dr. Ming Zhang Dinner will be held at 18:30 military time Which is 6:30 PM standard time.
15
Case Study: Time Structure (Figure 6.1) (3) // set members to invalid values dinnerTime.hour = 29; dinnerTime.minute = 73; cout << "\nTime with invalid values: "; printMilitary( dinnerTime ); cout << endl; return 0; } Dr. Ming Zhang Time with invalid values: 29:73
16
Case Study: Time Structure (Figure 6.1) (4) // Print the time in military format void printMilitary( const Time &t ) { cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":" << ( t.minute < 10 ? "0" : "" ) << t.minute; } // Print the time in standard format void printStandard( const Time &t ) { cout << ( ( t.hour == 0 || t.hour == 12 ) ? 12 : t.hour % 12 ) << ":" << ( t.minute < 10 ? "0" : "" ) << t.minute << ":" << ( t.second < 10 ? "0" : "" ) << t.second << ( t.hour < 12 ? " AM" : " PM" ); } Dr. Ming Zhang
17
Question 1 - Exercise/Home Work (1) u State whether the following are true or false u (a) One structure can not have two fields with the same name. u (b) One structure can not have two fields with the same type of data. u One structure must have al least two fields. u A field in a structure can not be a structure. Dr. Ming Zhang
18
Question 2 - Exercise/Home Work (3) Our task is to sort a file containing a company’s employee data. Each record contains information about a single employee, such as surname, given name, address, title, payroll number, age, salary. All data about a single employee can be collected in a structure. Please create a structure to fill employee data and declare an array to store information of 50 employees. Dr. Ming Zhang
19
Question 3 -Exercise/Home Work (5) Create a Time structure with three integer members: hour, minute and second. Define a single Time structure called lunchtime and use the dot operator to initialize the structure members with the values 12 for hour, 30 for minute and 0 for second. Print the Time in military format and standard format. Note that the print functions receive references to constant Time structures. Dr. Ming Zhang
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.