Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart.

Similar presentations


Presentation on theme: "Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart."— Presentation transcript:

1 Chapter 7 Continued Arrays & Strings

2 Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart program from chapter 6, uses a C-string to hold the name of the widget part. Let’s look at “strpart.cpp”.

3 A User-Defined String Type There are some problems with C-Strings as they are normally used in C++. One problem is that you cannot use the following expression: strDest = strScr; Let’s try to create our own string data type.

4 A User-Defined String Type If we define our own string type, using a C++ class, we can use this assignment statement. In our next example, “strobj.cpp”, we will create a class called String (not to be confused with the “real” string class built into C++).

5 The Standard C++ String Class Standard C++ includes a new class called string. This class improves on the traditional C-string in many ways. One way it improves is that you no longer need to worry about creating an array of the right size to hold string variables. The string class assumes all the responsibility for memory management.

6 The Standard C++ string Class Also, the string class allows the use of overloaded operators, so you can concatenate string objects with the + operator: s3 = s1 + s2; This new class is more efficient and safer to use than C-strings were. In most cases this is the preferred way to use strings in C++.

7 Defining and Assigning string Objects You can define a string object in several ways. You can use a constructor with no arguments, creating an empty string, you can also use a one argument constructor, where the argument is a C-string constant. Our next example shows this, “sstrass.cpp”.

8 Input/Output with string Objects Input and output are handled in a similar way to that of C-strings. The > operators are overloaded to handle string objects; and a function getline() handles input that contains embedded blanks or multiple lines. The next example, “sstrio.cpp” shows this.

9 Finding string Objects The string class includes a variety of member functions for finding strings and substrings in string objects. The next example, “sstrfind.cpp” shows this.

10 Modifying string Objects There are various ways to modify string objects. The next example shows the member functions erase(), replace(), and insert() at work. Let’s look at “sstrching.cpp”

11 Comparing string Objects You can overload operators or the compare() function to compare string objects. These will show whether strings are the same, or whether they precede or follow one another alphabetically. The program “sstrcom” show this.

12 Accessing Characters in string Objects You can access individual characters within a string object in several ways. In our next example we’ll show access using the at() member function. You can also use the overloaded [ ] operator, which makes the string object look like an array.

13 Accessing Characters in string Objects But the [ ] operator doesn’t warn you if you attempt to access a character that’s out of bounds. It is safer to use the at() function which causes the program to stop if you use an out-of-bounds index. Let’s look at “sstrchar.cpp”.


Download ppt "Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart."

Similar presentations


Ads by Google