" at the top of our program. Consider
the following program:
The first three lines of code in main declare and initialize three
string objects called "phrase", "copy" and "part". The next two lines
print phrase and its length using the length() member function.
Copy the code above into a file called "string.cpp" and
add some lines of code to print out "copy" and "part". Then
compile the program using "g++ -o string.exe string.cpp".
Now run the program. Can you deduce what the parameters mean
in the definition of the string "part"?
Copy and paste your program output below.
Using String Methods
There are dozens of methods (member functions) in the string class.
In this section, we will be using the following methods to manipulate
strings. For the full list, see table 10-7 on p582 in your textbook.
- theString.append(str) - Appends the string str to the
end of the string theString.
- theString.assign(str) - Assigns the string str to the
the string theString.
- theString.at(pos) - Returns the character at position pos
from the string theString. This is the same as "theString[pos]".
- theString.c_str() - Converts the contents of the string theString
to an array of characters, and returns the address of this array.
- theString.empty() - Returns true if the string theString
is empty (zero characters long) and false otherwise.
- theString.find(str, pos) - Returns the first position at or beyond
pos where the string str is found inside the string
theString. The value -1 is returned if str is not found.
- theString.erase(pos, cnt) - Erases cnt characters from
the string theString starting at position pos.
- theString.insert(pos, str) - Inserts a copy of the string str
into the string theString starting at position pos.
- theString.substr(pos, cnt) - Returns a copy of a substring that
is cnt characters long and begins at position pos
in the string theString.
Add the following lines of code to your program from above. Then add
some code to print out the variables part and copy again. Before you
run the program, read the descriptions of methods above to see if you
can figure out what the code does. Now run the program. Does it do
what you expected?
Extend your program to use the string methods above to replace the word
"quick" with the word "brave" in the string phrase. Then add code to
print out the phrase variable. Now run the program. Does it work?
Modify your program to use the "append" method to append the copy string
to the end of the phrase string. Then add code to print out the new value
of phrase. When you compile and run the program you will see that there
is no space between the two sentences. Can you figure out how to fix this
using the string methods above? If so, add the code to your program
Finally, add the following lines of code to the bottom of your program.
Before you compile and run the program, read the descriptions of methods
above to see if you can figure out what the code does. Now run the program.
Does it do what you expected?
Copy and paste your final program below.
Copy and paste your program output below.
Defining and Using the Temp Class
In this section, we will be creating and using our own class
called "Temp". To do this, we need to create separate files for
the interface and implementation, and a small main program that
uses this class.
Copy and paste the class interface below into a file called "temp.h".
By convention, all C++ class interface files are stored in files with
names ending with a ".h". Notice that this class has four public methods
(getCelsius, getFahrenheit, setCelsius, setFahrenheit) and one
private attribute (CelsiusTemperature).
Copy and paste the class implementation below into a file called "temp.cpp".
By convention, all C++ class implementation files are stored in files
with names ending with a ".cpp". Notice that each of these functions have
a "Temp::" before the method name. This is to tell the C++ compiler
which class they are part of.
Copy and paste the code below into a file called "main.cpp".
Notice that this file is including "temp.h". This means that
we can use the public methods of the temperature class Temp
in our main program. We are NOT allowed to access the private
attribute CelsiusTemperature, only the methods of the Temp
class are allowed to change this variable.
To compile this program, type in "g++ temp.cpp main.cpp -o main.exe".
This will combine the implementation of the temperature class with
your main program to create main.exe. Now run the program to see
what it does.
Now extend the main.cpp above to define three new objects called
"hot", "cold" and "just_right" and set these to appropriate values
using the "setFahrenheit" method, and then print out both their
Celsius and Fahrenheit values using the corresponding get methods.
Hopefully you will find that using the temperature class is almost as
easy as using the string class.
Copy and paste your new main.cpp program below.
Extending the Temp Class
In general, it is helpful to have a print method in every
class so users can more easily output the values stored in the object.
To do this we must make changes to both the interface and
implementation of the class.
Edit the temp.h file to add a new public method called "print" with
no parameters and no return value.
Edit the temp.cpp file to add the implementation of the "print" method.
It would be nice for your method to print out the temperature in both
Celsius and Fahrenheit. To get these values, you can call the
getCelsius and getFahrenheit methods. Since you are inside the class
definition, you do not need to use "value = object.method_name()", you
can simply use "value = method_name()".
Your final step is to call the new "print" method from within your
main.cpp program. Use your new method to print out the values of "one",
"two", "hot", "cold" and "just_right". Now compile your program
using "g++ temp.cpp main.cpp" and run your program again. With any
luck, you should see the F and C values for all five of these objects.
Copy and paste your modified temp.cpp below.
Copy and paste your modified main.cpp program below.
Finally, copy and paste your program output below.
This lab assignment will be submitted electronically
to the TAs once you fill in the fields below and
click on the "submit" button. You do NOT need to print
a copy of this document to hand in.