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
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;
};
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).
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
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;
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin