Download presentation
Presentation is loading. Please wait.
Published byBryce Jackson Modified over 9 years ago
1
Chapter 7 Continued Arrays & Strings
2
Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this, “partaray.cpp”.
3
Arrays as Class Member Data Arrays can be used as data items in classes. We will look at an example that models a common computer data structure: the stack.
4
Arrays as Class Member Data A stack works like the spring loaded devices that hold trays in cafeterias. When you put the tray on the top, the stack sinks down a little; when you take a tray off, it pops up. The last tray placed on the stack is always the first tray removed.
5
Arrays as Class Member Data Hardware and Software stacks offer a useful storage device in certain programming situations. First in last out (FILO) data structure. Queue is a first in first out (FIFO) data structure. Our next example, “stakaray.cpp” creates a simple stack class.
6
Arrays of Objects We can also create an array of objects. Let’s go back to our example of distances, this time an array of such objects, “englaray.cpp”.
7
Array Bounds In our last example, the program uses a do loop to get input from the user. This way the user can input data for as many structures of type part, up to MAX, the size of the array. Even though it is hard to imagine anyone entering over 100 distances, what would happen if they did?
8
Array Bounds If the user was to enter more than 100, something unpredictable and bad will happen. There is no bounds checking. If the user enters too much data for an array to hold, neither the compiler or runtime system will complain.
9
Array Bounds This extra data makes the array “too full” and in turn will probably write over the top of other data or the program code itself. This may cause the program to do strange things and even crash the system completely.
10
Array Bounds In the end it is up to the programmer to deal with array bounds. If it seems that the user will input too much data, the programmer should warn the user that the array is full. This can be accomplished with a simple if statement.
11
Accessing Objects in an Array We define an array of objects like this: Distance dist[MAX]; Here the data type of the dist array is Distance, and it has MAX elements. We access an array element (an object in the array) like this: dist[j].showdist();
12
Array of Cards Our next example shows an array of objects. This program will simulate a deck of cards. Let’s look at “cardaray.cpp”.
13
Graphics Characters There are several graphics characters in the range below ASCII code 32. In the display() member function of card we used codes 5, 4, 3, and 6 to access the characters for a club, diamond, a heart, and a spade.
14
Graphics Characters Casting these numbers to type char causes the << operator to print them as characters rather than as numbers. static_cast (5)
15
The Card Deck The array of structures that make up the deck of cards is defined in the statement: card deck[52]; This statement creates an array called deck, consisting of 52 objects of type card. To display the jth card in the deck, we call the display() member function: deck[j].display();
16
C-Strings There are 2 kinds of strings commonly used in C++. These 2 types are C-strings and strings that are objects of the string class. First we will look at C-strings, these are arrays of type char. We call these C-strings because they were the only kind of strings available in the C language.
17
C-Strings Even though strings created with the string class is the best way to represent a string in C++, we will study them because there are many C library functions in use today, and because the are more primitive and easier to understand. For years to come C style strings will show up in old code.
18
C-String Variables Just like other data types, strings can be variables or constants. First let’s look at an example that defines a single string variable, asks the user to enter a string, and places this string in the string variable. Let’s look at “stringin.cpp”.
19
C-String Variables Each character occupies 1 byte of memory. An important part of C-strings is that they must terminate with a byte containing 0. This is represented by the character constant ‘\0’, which is the ASCII value of 0. This terminating zero is called the null character. When the << operator displays a string, it continues until it finds a null character.
20
Avoiding Buffer Overflow In our last example the user inputted a string. What happens if the user enters a string that is longer than the array used to hold it? There is no built in mechanism in C++ to keep a over active typist to keep typing “off the array”, which would end up crashing the system.
21
Avoiding Buffer Overflow Thankfully there is a way to tell the >> operator to limit the number of characters it places in an array. Let’s look at “safetyin.cpp” for a way to do this.
22
String Constants You can initialize a string to a constant value when you define it. Let’s look at an example that does this, “strinit.cpp”.
23
Reading Embedded Blanks If you tried our last program with strings that contained more than one word, you would have printed only one word of your sentence. It turns out that the >> operator considers a space to be a terminating character. So it will read strings consisting of a single word, but anything typed after a space is thrown away.
24
Reading Embedded Blanks To read text containing blanks we use another function, cin.get(). This syntax means a member function get() of the stream class of which cin is an object. We will look at an example of how it’s used, “blanksin.cpp”.
25
Reading Multiple Lines Now we have solved the problem of reading strings with embedded blanks, but what about strings with multiple lines? There is a third argument in the cin::get() function that can help us out in this. This 3 rd argument specifies the character that tells the function to stop reading.
26
Reading Multiple Lines The default value for this argument is the newline (‘\n’) character, but if you call the function with some other character for this argument, the default will be overridden by the specified character. Let’s look at an example of this, “linesin.cpp”.
27
Copying a String the Hard Way The best way to understand the true nature of strings is to deal with them character by character. The next program does this, “strcopy1.cpp”.
28
Copying a String the Easy Way Of course you don’t have to copy strings that hard way, there is a built in easy way to do it. Let’s look at this easy way, “strcopy2.cpp”.
29
Arrays of Strings If there are arrays of arrays then of course there are arrays of strings. In our next example we will see this, “straray.cpp”.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.