|
|
|
|
|
|
|
colon after the right brace of a compound statement. However, the member list in a struct declaration is not considered to be a compound statement; the braces are simply required syntax in the declaration. A struct declaration, like all C++ declaration statements, must end with a semicolon. |
|
|
|
|
|
|
|
|
firstName, lastName, gpa, programGrade, quizGrade, finalExam, and courseGrade are member names within the struct type StudentRec. These member names make up the member list. Note that each member name is given a type. |
|
|
|
|
|
|
|
|
firstName and lastName are of type NameString, which is a char array type. gpa is a float member. programGrade, quizGrade, and finalExam are int members. courseGrade is of an enumeration data type made up of the grades A through D and F. |
|
|
|
|
|
|
|
|
None of these struct members are associated with memory locations until we declare a variable of the StudentRec type. StudentRec is merely a pattern for a struct (see Figure 14-1). The variables firstStudent and student are variables of type StudentRec. |
|
|
|
|
|
|
|
|
The members of a struct variable are accessed by giving the name of the variable, followed by a dot (period), and then the member name. This expression is called a member selector. The syntax template is |
|
|
|
|
|
|
|
|
StructVariable.MemberName |
|
|
|
|
|
|
|
|
|
Just as brackets ([ ]) are used to select individual components of an array, dot notation is used to select individual components of a struct. To access the grade point average of firstStudent, we would write |
|
|
|
|
|
|
|
|
Figure 14-1
Pattern for a Struct |
|
|
|
|
|