Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning C++ Through Game Programming, Second Edition by Michael Dawson.

Similar presentations

Presentation on theme: "Beginning C++ Through Game Programming, Second Edition by Michael Dawson."— Presentation transcript:

1 Beginning C++ Through Game Programming, Second Edition by Michael Dawson

2 Chapter 3 For Loops, Strings, and Arrays: Word Jumble

3 Objectives Use for loops Understand objects Use string objects Use arrays

4 for Loop Like while loop, can repeat a section of code Suited for counting and moving through a sequence of things

5 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

6 for Loop Example for (int i = 0; i < 10; ++i) cout << i << " "; Displays 0 1 2 3 4 5 6 7 8 9

7 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 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2 4,0 4,1 4,2

8 Objects Combine data and functions Data element is data member Function of an object is member function

9 Spacecraft Class New type called Spacecraft Data member for its energy level Member function to fire its weapons

10 Accessing Object Members Access through dot notation //ship is object of Spacecraft type if ( > 10) ship.fireWeapons() accesses the object’s energy data member ship.fireWeapons() calls the object’s fireWeapons() member function

11 string Objects Perfect way to work with sequences of characters string objects have a set of member functions Can be concatenated with + operator (overloaded)

12 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 "!!!"

13 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.

14 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!

15 Arrays Provide a way to work with elements of any type Similar to string object in terms of indexing Not objects, so no methods

16 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"};

17 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;

18 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!

19 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!!!";

20 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

21 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'}};

22 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 ; }

23 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

24 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

Download ppt "Beginning C++ Through Game Programming, Second Edition by Michael Dawson."

Similar presentations

Ads by Google