Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11: Structured Data. Slide 11- 2 Introduction An array makes it possible to access a list or table of data of the same data type by using a single.

Similar presentations


Presentation on theme: "Chapter 11: Structured Data. Slide 11- 2 Introduction An array makes it possible to access a list or table of data of the same data type by using a single."— Presentation transcript:

1 Chapter 11: Structured Data

2 Slide 11- 2 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:

3 Slide 11- 3 Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together General Format: struct { type1 field1; type2 field2;... };

4 Slide 11- 4 Example struct Declaration struct Student { int studentID; string name; short yearInSchool; double gpa; }; structure tag structure members

5 Slide 11- 5 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;

6 Slide 11- 6 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; studentID name yearInSchool gpa bill

7 Slide 11- 7 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

8 Slide 11- 8

9 Slide 11- 9 Program11-1 (Continued)

10 Slide 11- 10

11 Slide 11- 11 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;

12 Slide 11- 12 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)...

13 Slide 11- 13 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;

14 Slide 11- 14 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

15 Slide 11- 15 Excerpts From Program 11-4

16 Slide 11- 16 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; };

17 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; }

18 Slide 11- 18 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;

19 Slide 11- 19

20 Slide 11- 20

21 Slide 11- 21

22 Slide 11- 22 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

23 struct Employee // declare a global type { int id_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( ).

24 struct Employee // declare a global type { int id_number; double pay_rate; double hours; }; double calcNet(Employee); // function prototype int main ( void ) { Employee emp = {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

25 Slide 11- 25 Returning a Structure from a Function Function can return a struct : Student getStudentData(); // prototype stu1 = getStudentData(); // call Function must define a local structure – for internal use – for use with return statement

26 Slide 11- 26 Returning a Structure from a Function - Example Student getStudentData() { Student tempStu; cin >> tempStu.studentID; getline(cin, tempStu.pData.name); getline(cin, tempStu.pData.address); getline(cin, tempStu.pData.city); cin >> tempStu.yearInSchool; cin >> tempStu.gpa; return tempStu; }

27 Slide 11- 27

28 Slide 11- 28

29 Slide 11- 29 Program 11-8 (Continued)


Download ppt "Chapter 11: Structured Data. Slide 11- 2 Introduction An array makes it possible to access a list or table of data of the same data type by using a single."

Similar presentations


Ads by Google