Transcript Chapter 3

Beginning C++ Through
Game Programming,
Second Edition
by Michael Dawson
Chapter 3
For Loops, Strings, and Arrays:
Word Jumble
Objectives
•
•
•
•
Use for loops
Understand objects
Use string objects
Use arrays
for Loop
• Like while loop, can repeat a
section of code
• Suited for counting and moving
through a sequence of things
for Loop Structure
for (initialization; test; action)
statement;
• initialization sets up initial condition
• If test is false, the program moves on to
the statement after the loop. If test is
true, the program executes statement.
• action is executed (which often involves
incrementing a counter variable)
• Cycle repeats until test is false
for Loop Example
for (int i = 0; i < 10; ++i)
cout << i << " ";
Displays 0 1 2 3 4 5 6 7 8 9
Nesting for Loops
const int ROWS = 5;
const int COLUMNS = 3;
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
cout << i << "," << j << " ";
cout << endl;
}
Displays:
0,0 0,1
1,0 1,1
2,0 2,1
3,0 3,1
4,0 4,1
0,2
1,2
2,2
3,2
4,2
Objects
• Combine data and functions
• Data element is data member
• Function of an object is member
function
Spacecraft Class
• New type called Spacecraft
• Data member for its energy level
• Member function to fire its weapons
Accessing Object Members
• Access through dot notation
//ship is object of Spacecraft type
if (ship.energy > 10)
ship.fireWeapons()
• ship.energy accesses the object’s
energy data member
• ship.fireWeapons() calls the object’s
fireWeapons() member function
string Objects
• Perfect way to work with sequences
of characters
• string objects have a set of
member functions
• Can be concatenated with + operator
(overloaded)
Creating string Objects
string word1 = "Game";
string word2("Over");
string word3(3, '!');
• word1 is equal to "Game"
• word2 is equal to "Over"
• word3 is equal to "!!!"
string Member Functions
• size()—Returns number of characters in
string object
• length()—Returns number of characters
in string object
• find()—Searches the calling string
object for the string supplied as an
argument
• erase()—Removes a specified substring
• empty()—Returns true if calling string
is the empty string; otherwise, returns
false.
Indexing a string Object
• Access individual char values with subscripting
operator ([])
• Indexing begins at position 0
string word = "Game";
cout << "Char at pos 0 is: " << word[0]";
• Can reassign individual char values
word[0] = 'L';
• First character of word becomes char 'L' which
means word becomes "Lame Over!!!"
• C++ compilers do not perform bounds checking!
Arrays
• Provide a way to work with elements
of any type
• Similar to string object in terms of
indexing
• Not objects, so no methods
Creating Arrays
const int MAX_ITEMS = 10;
string inventory1[MAX_ITEMS];
• Declares an array inventory1 of 10 string
objects
• Like strings, indexing begins at position 0
• Initializer list
string inventory2[MAX_ITEMS] = {"sword",
"armor", "shield"};
string inventory3[] = {"sword", "armor",
"shield"};
Indexing Arrays
• Index like string objects
• Access individual element with index
number and the subscripting operator ([])
inventory1[0] = "sword";
inventory1[1] = "armor";
inventory1[2] = "shield";
cout << "Your items:\n";
for (int i = 0; i < 3; ++i)
cout << inventory1[i] << endl;
Array Bounds
• Array indexing begins at 0, just as with string
objects
int highScores[5];
• Defines a five-element array
• Valid position numbers are 0 through 4, inclusive
• There is no element highScores[5]
• C++ compilers do not perform array bounds
checking!
C-Style Strings
• Before string objects came along
• Are arrays of char values
• Automatically terminate with null character,
'\0'.
• No member functions
• string objects work with C-style strings
• Same shortcomings as arrays
• Use string objects whenever possible
char phrase[] = "Game Over!!!";
Multidimensional Arrays
• Represent multidimensional entities
(like a chess board)
• Can be many dimensions, but two or
three is usually enough
• Dimensions don't need to be of same
length
Declaring Multidimensional
Arrays
• A two-dimensional array with 64 elements
char chessBoard[8][8];
• With intializer lists
int ROWS = 3;
int COLS = 3;
char board[ROWS][COLS] = {{'O','X','O'},
{' ','X','X'},
{'X','O','O'}};
Indexing Multidimensional
Arrays
• Index an individual element of a multidimensional
with values for each dimension
• Assign the character to the element at [1][0]
board[1][0] = 'X';
• Display the whole board
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLS; ++j)
cout << board[i][j];
cout << endl;
}
Summary
• The for loop lets you repeat a section of
code
• for loops are often used for counting or
looping through a sequence
• Objects are encapsulated entities that
combine data and functions
• string objects allow you to store a
sequence of characters
• string objects have member functions
Summary (cont.)
• Arrays provide a way to store and access
sequences of any type
• A limitation of arrays is that they have a
fixed length
• You can access individual elements of
string objects and arrays through the
subscripting operator
• Bounds checking on string and array
elements is up to the programmer
• C-style strings are character arrays
terminated with the null character
• Multidimensional arrays allow for access to
array elements using multiple subscripts