For this experiment, you will write code for string variables.
Strings in C++ are represented using the string
class.
Unlike integers, real numbers, and characters, strings are not built
into C++. They're added to the language through a library. A class
defines what strings are. The name of the class is also the data
type we need for our variables.
Since the string
type is defined by a class, we must load a
library to use the string
data type. The library is
conveniently named string
, so we must add
#include <string>to the program with the other
#include
s. Do this now —
either above or below the other #include
s but before
using namespace std;
which informs the compiler where to
find these libraries. There are some libraries it can find without this
line, but it is a good idea to have it there anyway.
A string literal begins and ends with double quotes, with any number
of characters between them (e.g., "a string"
, "another
string"
). Incidentally, it isn't necessary to include the
string library if we're only using string literals — as we've done
already in our program when we've used "value1 is "
and "value2 is "
to label output — but there's no harm
in doing so.
Write declarations for string
variables str1
and
str2
, initializing them to "Samson"
and
"Delilah"
, respectively. Do not get rid of any
code from earlier experiments. Compile and execute your code.
Question #2.5.1: What is the declaration that you added to your program?
Once you can compile and execute the code without problems, add a
statement to output str1
and str2
. The
original program contained the statement
cout << "value1 is " << value1 << "\n" << "value2 is " << value2 << endl;to output
value1
and value2
. Your new statement for
str1
and str2
should be quite similar; you need
only replace the integer variables with the string variables and change the
labels appropriately.
Now compile and execute the program. Make sure it outputs the proper labels and values for the variables.
Question #2.5.2: What is the ouput statement you just added to your program?
Now, as before, you will experiment with these declarations.
In the integer experiment, we tried initializing an integer variable
with various different types. Let's try this again for the string
variables you just added to your program.
Initialize str1
to an integer. Compile your program.
Question #2.5.3: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Initialize str1
to a double. Compile your program.
Question #2.5.4: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Initialize str1
to a character. Compile your program.
Question #2.5.5: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
Initialize str1
to be equal to cin
. Compile your
program.
Question #2.5.6: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it output for the new initialization when you execute it?
It appears that string
doesn't like any of these other types.
You may find that your compiler gives very convoluted error messages
since string
is a class.
Change your program back so that str1
is initialized to a
string.
In the previous experiment, we took a look at escape characters.
These characters can also be used in strings; in fact, we used a string
literal "\n"
in output statements to advance output to a
newline:
cout << "value1 is " << value1 << "\n" << "value2 is " << value2 << endl;
But it, as well as other escape characters such as \t
, can
appear anywhere in a string literal. For example the statement
cout << "\n\tJohn Doe\nt123 ABC St.\n\tSome, Where\n\t\t\12345\n"generates the output:
John Doe 123 ABC St. Some, Where 12345
Everything we learned about escape characters as char
s
also applies to them inside string
s.
We initialized str1
and str2
with strings that
had several characters in them. The preceding output statement has an
even longer string literals.
But how small can we make the strings? One character? Let's
try it. Initialize str1
to "a"
, and add this output
statement to the program:
cout << "The number of characters in str1 is " << str1.size() << "\n";
Question #2.5.7: Make a prediction: will this compile? If so, what will it display? If not, what error message will you get?
Compile and execute the program (if you can).
Question #2.5.8: If it compiled, what did it display? If not, what error message did you get? Compare the result with your prediction.
Keep in mind that the char
type stores exactly one character,
but that's not the only way to store a character---a string
can have just one character in it. The output statements in
the program use the string literal "\n"
; as seen in the
previous experiment, this is an escape character which really counts
as one character. We could replace the double quotes
with single quotes, but we'd have to change them back if we wanted to
add more characters to those literals; for example, we might want to change
the output statement to
cout << "str1 has " << str1.size() << " characters.\n"
What about no characters? Can a string be empty? To find out,
initialize str1
to ""
.
Question #2.5.9: Make a prediction: will this compile? If so, what will it display? If not, what error message will you get?
Compile and execute the program (if you can).
Question #2.5.10: If it compiled, what did it display? If not, what error message did you get? Compare the result with your prediction.
The literal ""
is known as the
empty string and wlthough it seems rather useless now,
we will see later how it can be used when constructing a string
from smaller strings.