string Assignment and Concatenation

Figure 18.1 demonstrates string assignment and concatenation. Line 7 includes header for class string. The strings string1, string2 and string3 are created in lines 1214. Line 16 assigns the value of string1 to string2. After the assignment takes place, string2 is a copy of string1. Line 17 uses member function assign to copy string1 into string3. A separate copy is made (i.e., string1 and string3 are independent objects). Class string also provides an overloaded version of member function assign that copies a specified number of characters, as in

targetString.assign( sourceString, start, numberOfCharacters );

where sourceString is the string to be copied, start is the starting subscript and numberOfCharacters is the number of characters to copy.

Figure 18.1. Demonstrating string assignment and concatenation.

(This item is displayed on pages 886 - 887 in the print version)

 1 // Fig. 18.1: Fig18_01.cpp
 2 // Demonstrating string assignment and concatenation.
 3 #include 
 4 using std::cout;
 5 using std::endl;
 6
 7 #include  
 8 using std::string;
 9
10 int main()
11 {
12 string string1( "cat" );
13 string string2; 
14 string string3; 
15
16 string2 = string1; // assign string1 to string2 
17 string3.assign( string1 ); // assign string1 to string3
18 cout << "string1: " << string1 << "
string2: " << string2
19 << "
string3: " << string3 << "

";
20
21 // modify string2 and string3 
22 string2[ 0 ] = string3[ 2 ] = 'r';
23
24 cout << "After modification of string2 and string3:
" << "string1: "
25 << string1 << "
string2: " << string2 << "
string3: ";
26
27 // demonstrating member function at
28 for ( int i = 0; i < string3.length(); i++ )
29 cout << string3.at( i );
30
31 // declare string4 and string5
32 string string4( string1 + "apult" ); // concatenation
33 string string5;
34
35 // overloaded +=
36 string3 += "pet"; // create "carpet"
37 string1.append( "acomb" ); // create "catacomb"
38
39 // append subscript locations 4 through end of string1 to
40 // create string "comb" (string5 was initially empty) 
41 string5.append( string1, 4, string1.length() - 4 ); 
42
43 cout << "

After concatenation:
string1: " << string1
44 << "
string2: " << string2 << "
string3: " << string3
45 << "
string4: " << string4 << "
string5: " << string5 << endl;
46 return 0;
47 } // end main
 
 string1: cat
 string2: cat
 string3: cat

 After modification of string2 and string3:
 string1: cat
 string2: rat
 string3: car

 After concatenation:
 string1: catacomb
 string2: rat
 string3: carpet
 string4: catapult
 string5: comb
 

Line 22 uses the subscript operator to assign 'r' to string3[ 2 ] (forming "car") and to assign 'r' to string2[ 0 ] (forming "rat"). The strings are then output.


Lines 2829 output the contents of string3 one character at a time using member function at. Member function at provides checked access (or range checking); i.e., going past the end of the string throws an out_of_range exception. (See Chapter 16 for a detailed discussion of exception handling.) Note that the subscript operator, [], does not provide checked access. This is consistent with its use on arrays.

Common Programming Error 18.2

Accessing a string subscript outside the bounds of the string using function at is a logic error that causes an out_of_range exception.

Common Programming Error 18.3

Accessing an element beyond the size of the string using the subscript operator is an unreported logic error.

String string4 is declared (line 32) and initialized to the result of concatenating string1 and "apult" using the overloaded addition operator, +, which for class string denotes concatenation. Line 36 uses the addition assignment operator, +=, to concatenate string3 and "pet". Line 37 uses member function append to concatenate string1 and "acomb".

Line 41 appends the string "comb" to empty string string5. This member function is passed the string (string1) to retrieve characters from, the starting subscript in the string (4) and the number of characters to append (the value returned by string1.length() - 4).

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net