|
|
|
|
|
|
|
The reserved word struct is an abbreviation for structure, the C++ term for a record. Because the word structure has many other meanings in computer science, we'll use struct or record to avoid any possible confusion about what we are referring to. |
|
|
|
|
|
|
|
|
You probably recognize the syntax of a member list as being nearly identical to a series of variable declarations. Be careful: a struct declaration is a type declaration, and we still must declare variables of this type for any memory locations to be associated with the member names. As an example, let's use a struct to describe a student in a class. We want to store the first and last names, the overall grade point average prior to this class, the grade on programming assignments, the grade on quizzes, the final exam grade, and the final course grade. |
|
|
|
|
|
|
|
|
// Type declarations
enum GradeType {A, B, C, D, F};
typedef char NameString[16]; // Max. 15 characters plus \0
struct StudentRec
{
NameString firstName;
NameString lastName;
float gpa; // Grade point average
int programGrade; // Assume 0..400
int quizGrade; // Assume 0..300
int finalExam; // Assume 0..300
GradeType courseGrade;
};
// Variable declarations
StudentRec firstStudent;
StudentRec student;
int index;
int grade; |
|
|
|
|
|
|
|
|
Notice, both in this example and in the syntax template, that a struct declaration ends with a semicolon. By now, you have learned not to put a semi- |
|
|
|
|
|