C - Programming - C - Program - Chapter 11.pdf

(593 KB) Pobierz
CHAPTER
11
R ECORDS ( struct s)
IN THIS CHAPTER, YOU WILL:
n Learn about records ( struct s)
n Examine various operations on a struct
n Explore ways to manipulate data using a struct
n Learn about the relationship between a struct and functions
n Discover how arrays are used in a struct
n Learn how to create an array of struct items
1247350327.050.png 1247350327.058.png 1247350327.059.png 1247350327.060.png 1247350327.001.png 1247350327.002.png 1247350327.003.png 1247350327.004.png 1247350327.005.png 1247350327.006.png 1247350327.007.png 1247350327.008.png 1247350327.009.png 1247350327.010.png 1247350327.011.png 1247350327.012.png 1247350327.013.png 1247350327.014.png 1247350327.015.png 1247350327.016.png 1247350327.017.png 1247350327.018.png 1247350327.019.png 1247350327.020.png 1247350327.021.png 1247350327.022.png 1247350327.023.png 1247350327.024.png 1247350327.025.png 1247350327.026.png 1247350327.027.png 1247350327.028.png 1247350327.029.png
604
|
Chapter 11: Records ( struct s)
In Chapter 9, you learned how to group values of the same type by using arrays. You also
learned how to process data stored in an array and how to perform list operations, such as
searching and sorting.
This chapter may be skipped without experiencing any discontinuation.
In this chapter, you will learn how to group related values that are of different types. C++
provides another structured data type, called a struct (some languages use the term
‘‘record’’), to group related items of different types. An array is a homogeneous data
structure; a struct is typically a heterogeneous data structure. The treatment of a
struct in this chapter is similar to the treatment of a struct in C. A struct in this
chapter, therefore, is a C-like struct . Chapter 12 introduces and discusses another
structured data type, called a class.
Records ( struct s)
Suppose that you want to write a program to process student data. A student record
consists of, among other things, the student’s name, student ID, GPA, courses taken,
and course grades. Thus, various components are associated with a student. However,
these components are all of different types. For example, the student’s name is a
string and the GPA is a floating-point number. Because these components are of
different types, you cannot use an array to group all of the items associated with a
student. C++ provides a structured data type called struct to group items of
different types. Grouping components that are related, but of different types, offers
several advantages. For example, a single variable can pass all the components as
parameters to a function.
struct : A collection of a fixed number of components in which the components are
accessed by name. The components may be of different types.
The components of a struct are called the members of the struct . The general syntax
of a struct in C++ is:
struct structName
{
dataType1 identifier1;
dataType2 identifier2;
.
.
.
dataTypen identifiern;
};
1247350327.030.png 1247350327.031.png 1247350327.032.png 1247350327.033.png 1247350327.034.png 1247350327.035.png 1247350327.036.png 1247350327.037.png 1247350327.038.png
 
Records ( struct s)
|
605
In C++, struct is a reserved word. The members of a struct , even though they
are enclosed in braces (that is, they form a block), are not considered to form a
compound statement. Thus, a semicolon (after the right brace) is essential to end the
struct statement. A semicolon at the end of the struct definition is, therefore, a
part of the syntax.
The statement:
struct employeeType
{
string firstName;
string lastName;
string address1;
string address2;
double salary;
string deptID;
};
defines a struct employeeType with six members. The members firstName ,
lastName , address1 , address2 , and deptID are of type string , and the member
salary is of type double .
Like any type definition, a struct is a definition, not a declaration. That is, it defines
only a data type; no memory is allocated.
Once a data type is defined, you can declare variables of that type. Let us first define a
struct type, studentType , and then declare variables of that type:
struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
1
1
};
//variable declaration
studentType newStudent;
studentType student;
These statements declare two struct variables, newStudent and student , of type
studentType . The memory allocated is large enough to store firstName , lastName ,
courseGrade , testScore , programmingScore , and GPA (see Figure 11-1).
1247350327.039.png
606
|
Chapter 11: Records ( struct s)
newStudent
student
firstName
firstName
lastName
lastName
courseGrade
courseGrade
testScore
testScore
programmingScore
programmingScore
GPA
GPA
FIGURE 11-1 struct newStudent and student
You can also declare struct variables when you define the struct . For example,
consider the following statements:
struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
} tempStudent;
These statements define the struct studentType and also declare tempStudent
to be a variable of type studentType .
Typically, in a program, a struct is defined before the definitions of all the functions in
the program, so that the struct can be used throughout the program. Therefore, if you
define a struct and also simultaneously declare a struct variable (as in the pre-
ceding statements), then that struct variable becomes a global variable and thus can
be accessed anywhere in the program. Keeping in mind the side effects of global
variables, you first should only define a struct and then declare the struct variables.
Accessing struct Members
In arrays, you access a component by using the array name together with the relative
position (index) of the component. The array name and index are separated using square
brackets. To access a structure member (component), you use the struct variable name
together with the member name; these names are separated by a dot (period). The syntax
for accessing a struct member is:
structVariableName.memberName
1247350327.040.png 1247350327.041.png 1247350327.042.png 1247350327.043.png 1247350327.044.png 1247350327.045.png 1247350327.046.png 1247350327.047.png 1247350327.048.png 1247350327.049.png 1247350327.051.png 1247350327.052.png 1247350327.053.png 1247350327.054.png 1247350327.055.png
 
Records ( struct s)
|
607
The structVariableName.memberName is just like any other variable. For example,
newStudent.courseGrade is a variable of type char , newStudent.firstName is a
string variable, and so on. As a result, you can do just about anything with struct
members that you normally do with variables. You can, for example, use them in assign-
ment statements or input/output (where permitted) statements.
In C++, the dot, (.), is an operator, called the member access operator.
Suppose you want to initialize the member GPA of newStudent to 0.0 . The following
statement accomplishes this task:
newStudent.GPA = 0.0;
Similarly, the statements:
newStudent.firstName = "John";
newStudent.lastName = "Brown";
store "John" in the member firstName and "Brown" in the member lastName of
newStudent .
After the preceding three assignment statements execute, newStudent is as shown in
Figure 11-2.
newStudent
firstName
John
lastName
Brown
courseGrade
testScore
1
1
programmingScore
GPA
0.0
FIGURE 11-2 struct newStudent
The statement:
cin >> newStudent.firstName;
reads the next string from the standard input device and stores it in:
newStudent.firstName
The statement:
cin >> newStudent.testScore >> newStudent.programmingScore;
1247350327.056.png 1247350327.057.png
Zgłoś jeśli naruszono regulamin