Title: Chapter 11 Structured Data
1Chapter 11 Structured Data
2Arrays are ADTs all components or "members"
must be the same data type example int ids5
each element is an int C allows you to group
several variables together even though they are
different data types into one data
structure struct students int
grades5 double gpa struct
stu_rec long socsecnum char
lastname10 char firstname10 students
pupil No storage has been allocated yet. The
structures have only been defined.
3Declare an array of structures stu_rec
class24135 // an array of 35 stu_rec
structures stu_rec temp // one struct of
stu_rec stu_rec a, b, c // 3 structs of
stu_rec
4struct stu_rec struct students long
socsecnum int grades5 char
lastname10 double gpa char
firstname10 students pupil stu_rec
student student.socsecnum 123456789 strcpy(st
udent.lastname, "Jones" ) strcpy(student.firstnam
e, "Indiana" ) student.pupil.grades0
100 student.pupil.grades1 99 student.pupil.g
pa 99.5 cin gtgt student.pupil.grades2 cout
ltlt student.pupil.gpa cout ltlt student.lastname
5// Anonymous structure prohibit using the
structure after the initial declarations have
been made include ltiostreamgt using namespace
std struct // notice! No tagname char
Name25 int grades5 student, sptr
student int main( ) cout ltlt "Enter the
students name " cin.get(student.Name,
25) cin.ignore(100, '\n') for( int i 0 i
lt 5 i) // I controls the loop, sptr the
grades cout ltlt endl ltlt "Enter grade " ltlt i
1 cin gtgt sptr-gtgradesi return 0
6When to use . When to use -gt When to use
struct GradeInfo char Name25 int
TestScores float Average GradeInfo
StPtr, Student StPtr Student Equivalent
Statements StPtr-gtTestScores (StPtr).TestScore
s // compare addresses StPtr-gtTestScores
(StPtr).TestScores // compare values
7When to use . When to use -gt When to use
struct GradeInfo char Name25 int
TestScores float Average GradeInfo
Student1 int scores 82, 93, 74
Student1.TestScores scores // TestScores
ptr has been init cout ltlt Student1.TestScores
// print the pointer GradeInfo StPtr cout ltlt
StPtr-gtTestScores // points to
scores Sptr-gtTestscores //
incrementTestScores pointer, not data!
8struct Student // Arrays of Structures char
Name25 int grades5 int main( )
Student student35, sptr student // declare
an array of 35 stu structs for(int j
0 j lt 35 j, sptr) cout ltlt "Enter
the students name " cin.get(studentj.Name,
25) cin.ignore(100, '\n') for( int i 0 i
lt 5 i) cout ltlt endl ltlt "Enter grade " ltlt
i 1 cin gtgt sptr-gtgradesi // end
grades // end student return 0
9Anonymous Unions
- The members of an anonymous union have names, but
the union itself has no name. - union union
-
- member declarations long yard
- . . . short feet
-
10// Demonstrates how members can be overwritten in
a union include ltiostreamgt using namespace
std int main( ) union long lvar short
svar lvar 1000000 svar 111 //
first 2 bytes of lvar about to be
overwritten cout ltlt lvar ltlt endl cout ltlt
svar ltlt endl return 0 Output 65647 111
11UNIONS OF STRUCTURES struct MANAGER struct
LABORER EMPNAME emane EMPNAME
ename int dept int dept float
salary float hourlywage float
hoursworked union EMPLOYEE MANAGER
mgr LABORER laborer EMPLOYEE emp //
declare one var of type EMPLOYEE cin.get(emp.labor
er.ename.lname, 20) cin gtgt emp.laborer.dept cout
ltlt emp.laborer.hoursworked
emp.laborer.hourlywage cin gtgt emp.mgr.salary //
oops! Overwrite laborer info