8.1. C-Strings And The string Class

Time: 00:03:48 | Download: Large, Large (CC), Small | Streaming Streaming (CC) | Slides: PDF, PPTX

Students transitioning from Java may find it odd that C++ has two different representations for string data. Historically, strings were a primitive or fundamental data type based on character arrays and pointers. C++ inherits these fundamental strings, now called C-strings, from the C programming language. Furthermore, the first releases of C++ did not include a native string class (and creating a string class based on character arrays was, and still is, a typical example used to introduce many object-oriented programming concepts).

Today, C++ does include a full-featured string class similar to Java's String class (but take notice that in C++ the class name begins with a lower-case letter). Nevertheless, C-strings remain important for many kinds of C++ programs, so the chapter covers both C-strings and the string class. The natural progression of topics begins with arrays (covered in the previous chapter), then C-strings (a special use of character arrays), and ends with the string class.

A depiction of a string as an array or series of boxes. The string contains characters marked as used or filled array elements. Some array elements are empty or unused, indicated as empty boxes. The string's capacity is the sum of the filled and empty array elements.
Fundamental string characteristics. In general, strings (C-strings and string objects) have three fundamental characteristics: