C++ string is an object of the class string, which is defined in the header file
Examples
string s1; // Default constructor – creates an empty or null C++ string of length 0, equal to “”
string s2(“hello”); // Explicit constructor call to initialize new object with C string
string s3 = “hello”; // Implicit constructor call to initialize new object with C string
string s4(s2); // Explicit constructor call to initialize new object with C++ string
string s5 = s2; // Implicit constructor call to initialize new object with C++ string
Representation in Memory
Here is another example of declaring a C++ string:
string name = “Karen”;
name is a string object with several data members. The data member p is a pointer to (contains the address of) the first character in a dynamically-allocated array of characters. The data member length contains the length of the string. The data member capacity contains the number of valid characters that may currently be stored in the array.
A “null string” is a string with a length of 0:
The length of a null string is 0.
#mastery25 #TC1017 by Mitzi Hernandez is licensed under a Creative Commons Attribution 4.0 International License.