Chapter 11 Structured Data - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Chapter 11 Structured Data

Description:

Anonymous structure prohibit using the structure after the initial declarations have been made ... Anonymous Unions. The members of an anonymous union have ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 12
Provided by: cathe67
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11 Structured Data


1
Chapter 11 Structured Data
2
Arrays 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.
3
Declare 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
4
struct 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
6
When 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
7
When 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!
8
struct 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
9
Anonymous 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
11
UNIONS 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
Write a Comment
User Comments (0)
About PowerShow.com