E N D
Chapter 11: Structured Data
Introduction • An array makes it possible to access a list or table of data of the same data type by using a single variable name. At times, however, we may want to store information of varying types, such as a string name, an integer part number, and a real price, together in one structure. A data structure that stores different types of data under a single variable name is called a record • For example, consider preparing an employee record. Each of the individual data items listed below is an entity by itself that is referred to as a data field. Taken together, all the data fields form a single unit that is referred to as a record. In C++, a record is referred to as a structure. Name: Identification Number: Regular Pay Rate: Overtime Pay Rate:
Combining Data into Structures • Structure: C++ construct that allows multiple variables to be grouped together • General Format: struct <structName> { type1 field1; type2 field2; . . . };
Example struct Declaration structStudent { intstudentID; string name; short yearInSchool; double gpa; }; structure tag structure members
struct Declaration Notes • Must have ; after closing } • struct names commonly begin with uppercase letter • Multiple fields of same type can be in comma-separated list: string name, address;
Defining Variables • struct declaration does not allocate memory or create variables • It creates a new data type • To define variables, use structure tag as type name: Student bill; bill studentID name yearInSchool gpa
Accessing Structure Members • Use the dot (.) operator to refer to members of struct variables: cin >> bill.studentID; getline(cin, bill.name); bill.gpa = 3.75; • Member variables can be used in any manner appropriate for their data type
Displaying a struct Variable • To display the contents of a struct variable, must display each field separately, using the dot operator: cout << bill; // won’t work cout << bill.studentID << endl; cout << bill.name << endl; cout << bill.yearInSchool; cout << " " << bill.gpa;
Comparing struct Variables • Cannot compare struct variables directly: if (bill == william) // won’t work • Instead, must compare on a field basis: if (bill.studentID == william.studentID) ...
Initializing a Structure • struct variable can be initialized when defined: Student s = {11465, "Joan", 2, 3.75}; • Can also be initialized member-by-member after definition: s.name = "Joan"; s.gpa = 3.75;
More on Initializing a Structure • May initialize only some members: Student bill = {14579}; • If you leave a structure member uninitialized, you must leave all members that follow it uninitialized as well!! • Cannot skip over members: Student s = {1234, "John", , 2.83}; // illegal • Cannot initialize in the structure declaration, since this does not allocate memory
Nested Structures • A structure can contain another structure as a member: • To include a structure within a structure (Nested Structures, p. 642), we follow the same rules for including any data type in a structure. For example, assume that a structure is to consist of a name and a date of birth, where a Date structure, has been declared as struct Date { int month; int day; int year; }; • A suitable definition of a structure that includes a name and a Date structure is struct Person { string name; Date birth; };
struct Date { int month; int day; int year; }; struct Person { string name; Date birth; }; int main ( void ) { Person employee; employee.name = "Mary Hill"; employee.birth.month = 12; employee.birth.day = 25; employee.birth.year = 2002; cout << employee.name << " birthdate is: “<< employee.birth.month << "/" << employee.birth.day << "/“<< employee.birth.year; return 0; }
Arrays of Structures • Structures can be defined in arrays • Can be used in place of parallel arrays const int NUM_STUDENTS = 20;Student stuList[NUM_STUDENTS]; • Individual structures accessible using subscript notation • Fields within structures accessible using dot notation: cout << stuList[5].studentID;
Structures as Function Arguments • Individual structure members may be passed to a function in the same manner as any variable. • May pass members of struct variables to functions: computeGPA(stu.gpa); • May pass entire struct variables to functions: showData(stu); • Can use reference parameter if function needs to modify contents of structure variable
structEmployee // declare a global type { intid_number; double pay_rate; double hours; }; double calcNet(double, double); // function prototype int main ( void ) { Employee emp = {6782, 8.93, 40.5}; double net_pay; net_pay = calcNet(emp.pay_rate, emp.hours); //PASS BY VALUE cout << setw(10) << fixed << showpoint << setprecision(2); cout << "The net pay for employee " << emp.id_number << " is $" << net_pay << endl; return 0; } double calcNet(double pay_rate, double hours) { return (pay_rate * hours); } //PASS BY VALUE //Output: The net pay for employee 6782 is $361.66 • This example passes copies of the values stored in structure members emp.pay_rate and emp.hours to the function named calcNet( ).
structEmployee // declare a global type { intid_number; double pay_rate; double hours; }; double calcNet(Employee); // function prototype int main ( void ) { Employeeemp = {6782, 8.93, 40.5}; double net_pay; net_pay = calcNet(emp); cout << setw(10) << fixed <<showpoint << setprecision(2); cout << "The net pay for employee " << emp.id_number << " is $" << net_pay << endl; return 0; } double calcNet(Employee temp) { return (temp.pay_rate * temp.hours); } • Output The net pay for employee 6782 is $361.66