Topics:
C++ provides a data type (the class) that allows the programmer to group a set of data and the operations allowed on that data into a single unit called the class. When we declare varibles that have type class, the variables are called objects. We have already used some of the built in classes - (<iostream> and <fstream>) and have seen how to use (call) their member functions - cout.setf(ios::showpoint), mystream.close(), etc .
We will continue our study of classes by examining the C++ string class. The C++ string class is very similar to the cstrings we have already studied. With the string class you can perform the same type of operations as you can with cstrings (copy, compare, concatenate, find the length...) and more - over 100 member or helper functions for the string class. In addition the string class avoids some of the problem with cstrings (for example - writing past the end of an array with strcpy(srt1, str2) if str1 is not big enough to hold str2). The string class is also designed to adjust the length of the string to be just "big enough".
Another problem avoided by the string class is the initial value problem. When objects of type string are declared a special member function called a constructor is used to give the string an initial value. The constructor, like any other function, can be overloaded to provide a variety of ways to create string objects.
For example:
1.) string str1; //default constructor - empty string "" 2.) string str2("Hello"); //construct a string with a literal value 3.) string str3(str2); //construct a string from another string object 4.) char cstr1[] = "CS150"; //declare a cstring 5.) string str4(cstr1); //construct a string from a cstring 6.) string str5("May you write interesting programs"); //string with spaces 7.) string str6 = str2 + " " + cstr1 + " " + str5; //string from concatenation
A class can also override operators such as the extraction >>, insertion <<, and the "+" operator (as in the above example).
For example:
cout << str1 + str2 + " " + str3 + " " + str4 << "\n" << str5 << endl;;
Here the "+" operator has been overloaded to concatenate two string objects (srt1 + str2), one string object and a cstring (str3 + " "), etc... String concatenation has the normal precedence and order of operation (left to right) for the + operator. Also note that the << operator has been overloaded to output a string to the stream.
cin >> srt1;
Here the >> operator has been overridden to input keyboard data to a string. The extraction operators performs as it does for all other data types - will read the next sequence of character - up to the next white space. The problem with the >> operator, is that it will only read in a word at a time - if I want to read in a string with spaces, we can use the function getline(cin, str1); getline takes an input stream as the first argument and a srting variable as the second argument. getline() will read input until it encounters the end of the line character. The following code sample illustrates how to use getline in a program:
//.....main code
string buf; //declare an empty string;
cout << "Enter a string: ";
getline(cin,buf);
cout << "You entered:\n" + buf << endl;
Write a program (yourLastNameLab11Part1.cpp) that incorporates the string declarations 1 - 7. Add code to output the value of each string. Run and compile the code. Once it is running enable the debugger and step through the above code. Note: make sure to expand the boxes with the + as each object is declared. Example (str4 -> alloc_ -> m_ ) - note the size and data members .
The standard C++ string
class includes member functions for initializing (constructing) a string,
extracting a substring, computing the length of a string, replacing characters in an existing string, and appending
a string to an existing one. In addition each element of the string may be accessed by using its array index. For
example to print the string in reverse or change an individual element of the string.
Begin by reviewing section 10.3 of your book and see the table of string functions on page 650. Note that the
length()
function was inadvertently omitted from the list (example use: int strLength = someString.length();).
Your task is to write a program (yourLastNameLab11Part2.cpp) that will
#include <iostream> #include <string> using namespace std; int main() { string firstString; string secondString;
This is a sample dialogue: (user input in bold)
*****Sample input of program***** Please enter a string: great Please enter a second string: algorithm *****Sample output of program***** The first string great is 5 characters long Its first character is: g last character is: t The first string in reverse is: taerg
The second string algorithm is 9 characters long Its first character is: a last character is: m The first string in reverse is: mhtirogla
After inserting the second string into the middle of the first string New string is: gralgorithmeat length: 14
After appending the second string to the end of the first string New string is: greatalgorithm length: 14
After appending the second string to the end of the first string plus the word FIFTH, New string is: greatalgorithmFIFTH length: 19
Write a C++ program (yourLastNameLab11Part3.cpp) that reads in a list of doubles into an array (use a file or get the input from the keyboard - your choice) and sorts the list in descending order (biggest to smallest). Use functions to initialize the array to all zeros, read in the data, print the array, and sort the array. Use the following function prototypes:
void initialize(double list[], int size); //gets the array and it size - set all values to zero
int getData(double list[], int size); //get data and store in array - return the number of values read
void printArray(double list[], int size); //gets the array and the actual number of values in the array
void sort(double list[], int size); //sort an array of size elements
Your main should look something like
int main() { double myArray[ArraySize]; int numVals = 0; initialize(myArray, ArraySize); numVals = getData(myArray, ArraySize); cout << "Array before sort: \n"; printArray(myArray, numVals);
sort(myArray, numVals);
cout << "Array after sort: \n"; printArray(myArray, numVals); }
Use the codeSamples on the web and your lecture notes for examples of all the above functions.