Date post: | 17-Jan-2016 |
Category: |
Documents |
Upload: | antonia-wilson |
View: | 218 times |
Download: | 0 times |
If you can't read please download the document
Structures (L30)Syntax of a struct DeclarationStructure VariablesAccessing Members of StructuresInitialize Structure VariablesArray of StructuresAccessing the Array of StructuresRules When using StructuresCase StudyExercise/Home Work Dr. Ming Zhang
Syntax of a struct DeclarationStructuresStructures ae aggregate data types built using elements of other types including other struct.Syntax of a sturctstruct structure_name {Declaratioons for the components making up the sturcture}; // must end with a semicolon! Dr. Ming Zhang
Example of StructureStructure definition Structure name
struct fruit_case { //struct likes a recordint number; // member, or fieldfloat average_weight; // member, or field Char name[21]; // member, or field
}; Dr. Ming Zhang
Structure VariablesDeclare Structure VariablesSo 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;
Structure Variablesorange_case and apple_case are structure variables, each one contains the three component number, average_weight, and name. Dr. Ming Zhang
orange_case and apple_case
orange_case: apple_case:
number: number:
average_weight: average_weight:
name: name: Dr. Ming Zhang
Accessing Members of StructuresDot 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
Accessing orange_caseorange_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
A Structure is Single Variableapple_case = orange_case;
orange_case: apple_case:
number: 64 number: 64
average_weight: 4.8 average_weight: 4.8
name: Oranges name: Oranges Dr. Ming Zhang
Initialize Structure Variablesfruitcase 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
Array of Structuresfruitcase 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
Accessing the Array of Structuresfruitcase 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
Rules When using StructuresWe may not have two fields of the same name in the one structure.We may have two fields of the same name in different structures.A structure must have at least one field.A structure may contain fields which are themselves structures, but it may not contain another copy of itself.A structure can contain arrays, and arrays can have structures as elements. Dr. Ming Zhang
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
Question 1 - Exercise/Home Work (1)State whether the following are true or false
(a) One structure can not have two fields with the same name.
(b) One structure can not have two fields with the same type of data.
One structure must have al least two fields.
A field in a structure can not be a structure.
Dr. Ming Zhang
Question 2 - Exercise/Home Work (3)Our task is to sort a file containing a companys 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
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