Structures and Enumerators
Structures
• Structure: allows multiple variables of
different types to be grouped together
= User Defined Type
• Structure Declaration Format:
struct structure name
{
type1 field1;
type2 field2;
…
typen fieldn;
};
7-2
Example struct Declaration
struct Student
{ structure tag
int studentID;
string name; structure members
short year;
double grades[10];
}; Notice the
required
;
7-3
Defining Structure Variables
• struct declaration does not allocate memory or
create variables
s1
• To define variables, use studentID
structure tag as type name name
Student s1; year
grades
In C it is necessary to use the ‘struct’ keyword when declaring
struct variables:
struct Student s1;
Or use a typedef to avoid this:
typedef struct { … } Student;
7-4
Structure Initialization
Structures can be initialized with a comma separated list
of values, similar to arrays:
Structure Declaration Structure Variable
box
struct Dimensions
{ int length, length 12
width, width 6
height;
height 3
};
Dimensions box = {12,6,3};
7-5
Accessing Structure Members
• Use the dot (.) operator to refer to
members of struct variables
Student student1;
getline(cin, student1.name);
cin >> student1.studentID;
student1.grades[0] = 3.75;
7-7
Displaying struct Members
To display the contents of a struct
variable, you must display each field
separately, using the dot operator
Wrong:
cout << s1; // won’t work!
Correct:
cout << s1.studentID << endl;
cout << s1.name << endl;
cout << s1.year << endl;
cout << s1.grades[0];
7-8
Copying structs
• You can copy a struct using the assignment
operator. This performs a memberwise
assignment (each member gets copied).
Student s1, s2;
s1.name = “Tom”;
s1.age = 42;
s2 = s1;
// now s2 has the same values as s1
7-9
Comparing struct Members
• Similar to displaying a struct, you
cannot compare two struct variables
directly:
if (s1 == s2) // won’t work!
• Instead, compare member variables:
if (s1.age == s2.age) // OK
7-10
Nested Structures
A structure can have another structure as a
member.
struct Date
{ int day,
month,
year;
};
struct Student
{ int studentID;
string name;
Date birthdate;
double grades[10];
}; 7-11
Members of Nested Structures
Use the dot operator multiple times to
access fields of nested structures
Student s5;
s5.name = “John Smith”;
s5.birthdate.year = 1999;
s5.birthdate.month = 12;
s5.birthdate.day = 25;
7-12
Arrays of Structures
Can create arrays of structures, same as simple types:
struct Student {
string name;
int grade;
};
// create an array of 2 students
Student students[2] = {
{“Jim”, 2},
{“Anne”, 1}
};
cout << students[0].name;
students[0].grade += 1;
7-13
Example: Your student homework
Instead of:
const int MAX_STUDENTS = 20;
string studentNames[MAX_STUDENTS];
int studentGrades[MAX_STUDENTS];
You should declare a struct and write your code like this:
struct Student
{
string name;
int grade;
};
Student students[MAX_STUDENTS];
7-14
Example
struct Date struct Student
{ int day, { string name;
month, Date birthdate;
year; double grades[10];
}; };
Student students[100];
print name of second student in students array
cout << students[1].name;
print third grade of first student
cout << students[0].grades[2];
print birth year of third student
cout << students[2].birthdate.year;
7-15
Structures as Function Arguments
• Can pass members of struct variables to functions
double computeAverage(int arr[], int count);
cout << computeAverage(s1.grades, 10);
• Can pass entire struct variables to functions
void showStudentData(Student student);
showStudentData(s1);
• Can use reference parameter if function needs to
modify contents of structure variable (you will learn
about reference parameter in future class) 7-16
Enumerator Values
Enums define a set of integer constants. They create a data
type that can only take on a restricted range of values.
enum Color {RED, BLUE, GREEN, YELLOW};
By default enum values start at 0, so RED=0, BLUE=1, …
Color color1 = BLUE; // OK
Color color2 = 0; // NOT OK
But you can use a cast:
Color color2 = static_cast<Color>(0);
7-23
// same as setting it to RED
Setting Explicit Enumerator Values
You can also set explicit values for enumerators:
enum Color {
RED = 1,
BLUE = 2,
GREEN = 4,
YELLOW = 8
};
7-24