Structures (aka Records)

advertisement
Programmer Defined Structures
(Records)
CS362
C++ Structures

A Structure (Record) Contains Data About a Single Entity:
 Each Data Item is Called a Member (or Field)
 Members Can Be Of Different Data Types
 Member Data Types Can Be Basic or Complex
 Basic – int, float, double, string
 Complex – array, other structures
 In The End, Any Valid Data Type Can Used to Define a
Member in a Structure
C++ Structures


Data About a Student May Consist of:
 Last Name
string
 First Name
string
 Middle Initial
char
 Id Number
int
 Sex
char
 GPA
float
Putting This All Together We Can Define A Structure
Defining A Structure

Structures Require a Two Step Definition Process:

First – Define a struct Data Type That Defines the
Members of the Required Structure
 Second – Use the struct to Declare Variables Needed
for the Program
struct Data Type Definition:
struct struct-name

{
type
type
type
};
member1-name;
member2-name;
:
memberN-name;
Defining A Structure

Definition Rules:
 The Structure Definition Must Begin With Reserved
Word – struct
 Each Member Must Have a Type (the Type Can Be
Different Then Other Members)
 Semicolons Separate the Structure Members
 Structure Definition Must End With };
Defining A Structure Variable


The struct Definition Only Defines a Data Type
To Use the Structure, Memory Must be Allocated Via
Variable Declaration:
struct-type variable_identifier;


Where struct-type is the Structure Type Declared
Where variable identifier is a Valid Identifier Name
Initializing A Structure Variable

Like Other Variables Structures Can be Initialized at
Declaration:
struct-type variable_identifier = {member1-value, member2-value,
….memberN-value};
Structure Example
struct studentRec
{
int
string
string
char
char
double
};
idNumber;
lname;
fname;
initial;
sex;
gpa;
Structure Variable
studentRec
stu1;
stu1
idNumber
lname
fname
initial
sex
Gpa
Initialized
studentRec
stu1
stu1 = {123456,”Sanders”,”Mark”,’S’,’M’,3.50};
Accessing Structure Data



A Structure is a Single Data Unit
Members Can Be Accessed Individually
To Access a Member Use The DOT notation:
struct variable_name.member_name
studentRec.lname = “Sanders”;

Members Can Be Used Like Any Other Variable
Accessing Structure Data

(cont.)
More Examples
cout << “Enter student GPA: “;
cin >> stu1.gpa;
stu1.gpa = stu1.gpa + 0.5;
cout << fixed << showpoint << setprecision(2);
cout << stu1.lname << “, “ << stu1.fname << “ “ <<;
cout << stu1.gpa << endl;
Using Structure Data

Once You Have Data in the Structure, it Can Be Used Like
Any Other Variable:
if (stu1.sex == ‘F’)
{
cout << stu1.fname << ‘ ’ << stu1.lname << “ is Female.”;
cout << endl;
}
if (stu1.gpa < 2.5)
{
cout << stu1.lname << “, ” << stu1.fname;
cout << “ is on probation.” << endl;
}
Structures As Arguments


Structures Can Be Passed as Data to Functions
If Data is to be Modified, Pass By Reference
void readStuInfo (studentRec& stu1)
{
cout << “Enter student last name: “;
cin >> stu1.lname;
:
cout << “Enter student GPA: “;
cin >> stu1.gpa;
}
Arrays of Structures



Rather Than Using Parallel Arrays, we can use an Array of
Structures
Does Not Violate Single Data Type Rule
Each Cell In The Array Will Hold One Structure
struct-type array_name[size];
studentRec studentList[MAX];


Declares Array to Store MAX Quantity of Structures
Each Cell Contains One Record With Six Members
Initializing Structure Array

Same As In The Past, Only Thing New Is the Subscript
studentRec studentList[MAX] = {
{123456,”Sanders”, “Mark”, ‘S’, ‘M’, 3.50},
{234789, “Blow”, “Joe”, ‘J’,‘M’, 2.50},
{367890, “Smith”, “Sally”, ‘S’,‘F’, 4.00} };

All Remaining Cells Are Initialized to “blank” or 0
Accessing Structure Array Data

Just Need To Use Subscript:
arrayName[index].member_name
studentList[1].idNumber = 234567;
studentList[2].gpa = studentList[3].gpa;
for (num = 0; num < MAX; num++)
{
cout << studentList[num].lname << “, ” <<;
cout << studentList[num].fname << endl;
}
Access Structure Array Data (cont.)

Swapping Structures is like Swapping Any Variables:
studentRec temp;
temp = studentList[top];
studentList[top] = studentList[minPosition];
studentList[minPosition] = temp;
Nested Structure




A Nested Structure is a Structure that has a Member that
is a Structure
Used to Organize Data, and to Allow for the Reuse of
Variable Names
To Declare a Nested Structure You Must First Declare the
Structure(s) that will be Nested
Once Declared the Structure can be used as Data Type for
Members of Other Structures
Nested Structure (cont.)
struct address
{
string street;
string city;
string state;
string zip;
};
struct student
{
string
string
int
double
address
address
lname;
fname;
age;
gpa;
campus;
home;
};
student studentData;
Nested Structure (cont.)

Accessing Nested Structure

Need to Use Entire Name:
studentData.lname
studentData.fname
studentData.age
studentData.gpa
studentData.campus.street
studentData.campus.city
studentData.campus.state
studentData.campus.zip
studentData.home.street
studentData.home.city
studentData.home.state
studentData.home.zip
Nested Structure (cont.)

Arrays of Nested Structures
student studentList[MAX];
studentList[n].lname
studentList[n].fname
studentList[n].age
studentList[n].gpa
studentList[n].campus.street
“
“
“
“
“
“
studentList[n].campus.zip
studentList[n].home.street
“
“
“
“
“
“
studentList[n].home.zip
Download