CSCI 1110 - C++ LectureNotes
Chapter 14 - Records (C++ Structs)
Records
-
A Record
is a structured data type with a fixed number of components that are accessed
by name rather than by index. The components may be heterogeneous (of different
types).
-
A Field
is a component or member of a record.
-
In C++ records are declared using the
struct statement.
enum GradeType {A, B,
C, D, F};
typedef char NameString[20];
struct StudentRec
{
NameString
firstName;
NameString
lastName;
long
studentId;
float
gpa;
float
programAvg;
float
examAvg;
float
labAvg;
int
finalExam;
GradeType
coursegrade;
}
StudentRec aStudent;
StudentRec aPupil;
-
Each member of the struct
is accessed by using the member
selector operator (.).
aStudent.courseGrade
=
aStudent.examAvg * 0.30 +
aStudent.programAvg * 0.25 +
aStudent.labAvg * 0.30 +
aStudent.finalExam * 0.15;
-
Members of a struct
can be intitalizes when the declaration is made:
StudentRec aStudent
=
{
"Ophelia",
"Payne",
12345678,
0.725,
54.3,
62.0,
25.4,
76.0,
D
};
-
The members firstName
and lastName are arrays.
To access their individual components, specify the entire name of the field
followed by the index of the array member enclosed in square brackets.
aStudent.firstName[2]
= 'i';
-
The following table compares aggregate
operations for arrays and structs.
|
Aggregate Operation
|
Arrays
|
Structs
|
| I/O |
Strings only |
No |
| Assignment operation = |
No |
Yes |
| Arithmetic |
No |
No |
| Comparison |
No |
No |
| Parameter passage |
By reference only |
By value or by reference |
| Function return value |
No |
Yes |
-
Unlike arrays, the contents of a struct
can be assigned to another variable of the same type.
aPupil = aStudent;
-
Alternate forms of the struct
statement:
struct StudentRec
{
NameString
firstName;
NameString
lastName;
long
studentId;
float
gpa;
float
programAvg;
float
examAvg;
float
labAvg;
int
finalExam;
GradeType
coursegrade;
} aStudent, aPupil;
The form above combines the definition
of the struct
with the declaration of two variables of that type.
struct
{
int firstMember;
float secondMember;
} someVar;
This form results in what is known
as an anonymous type -
no other variables of the same type are possible since the type does not
have a name.
Arrays of Records
-
Structs can be used as the members of arrays. For example:
const int MAX_STUDENTS
= 200;
enum GradeType {A, B,
C, D, F};
typedef char NameString[20];
struct StudentRec
{
NameString
firstName;
NameString
lastName;
long
studentId;
float
gpa;
float
programAvg;
float
examAvg;
float
labAvg;
int
finalExam;
GradeType
coursegrade;
}
StudentRec gradeBook[MAX_STUDENTS];
-
To access individual members of the
array, you must specify both the array index and the field of the struct.
gradeBook[2].finalExam
= 96.0;
Hierarchical Records
-
Components of structs can be any other
data type - including another struct. Such records are called Hierarchical
Records.
Example:
struct PhoneNumber
{
char areaCode[4];
char exchange[4];
char number[5];
};
typedef char String20[21];
struct AddressRec
{
String20 Street;
String20 Line2;
String20 City;
char State[3];
char Zip[10];
};
struct NameRec
{
String20 Last;
char Initial;
String20 First;
};
struct StudentRec
{
NameRec name;
AddressRec localAddress;
AddressRec permAddress;
PhoneRec homePhone;
PhoneRec workPhone;
};
-
To reference a member of a hierarchical record, you must
fully specify which member you mean.
StudentRec aStudent;
cout << aStudent.homePhone.areaCode;
Updated by Tom Irby, December 3, 1998.
Copyright 1998 by Thomas C. Irby.