The purpose of this quiz is to give you a chance to focus your knowledge of using our (CSC122-designed) String class.
In the String class, how can the programmer distinguish empty Strings from Strings which failed to allocate. Does it require any special care (or timing) on the programmer's part?
Ideally the programmer will test for failed memory allocation by using operator ! right after the attempt to make memory happen. This could come about because of input, assignment, or concatenation with assignment.
Do String class objects ever use more memory than is absolutely necessary to store their string? Explain.
In a way, yes. They store a null character at the end of their data so that the cstring functions can be used internally. But what's a single byte per object between friends?
TRUE ✓ | FALSE ✗ | Our String class has been made compatible with the built-in characters and C-strings. | ||
---|---|---|---|---|
TRUE ✗ | FALSE ✓ | This required 6 versions of almost all of the overloaded operators. | ||
TRUE ✓ | FALSE ✗ | Some handled built-in types as the left-hand operand, while others handled built-in types as the right-hand operand. | ||
TRUE ✗ | FALSE ✓ | This allows our String class objects to be used anywhere a char or char* can be used. |
In order to make the maintenance members of the String class changeable even in ____ objects, we introduced the ____ keyword. This keyword means that the data member may be altered even inside a method which has been declared ____. It is only useful inside classes.
Why did we have to code += for the String class? After all, we'd already defined + and = separately?
+= is a separate operator from + and =. These three operators work as a team on built-in types, but are not designed to do that for all data types. Any data types we come up with have to make that "with assignment" judgement for themselves, as it were.
Show what is produced (on the screen) when the following code is executed. (For bonus credit, also explain what happens inside all the String objects as the code executes — step by step.)
short a, i; String x(50, '*'), y('\n'), d, t; d = x + y + '*' + String(48, ' ') + '*' + y + "* "; cout << "Enter your name: "; t.getline(cin); a = min(46,t.size()); if (a < t.size()) { for (i = 0; i < a; i++) { d += t[i]; } } else { for (i = 0; i < (46-a)/2; i++) { d += ' '; } d += t; for (i = 0; i < (46-a)/2; i++) { d += ' '; } } d += " *" + y + '*' + String(48, ' ') + '*' + y + x; cout << d << endl;
The code centers their name or the first 46 characters of their name if it is longer in a box of stars. To check the bonus, come see me.
Show an overloaded ~ operator for the String class which can return a new String whose contents is the caller's contents in reverse.
String operator~(const String & s) { String t = s; size_t len = t.length(); for (size_t i = 0; i < len; ++i) { swap(t[i],t[len-i-1]); } return t; }
Briefly explain how the |= operator is used in the String::setf method. Why didn't we simply use assignment?
This operator uses bit-wise or to combine the bits between two memory
locations. This allows us to manage multiple flags in a single integer.
An assignment would have overwritten the other flags with 0s when setting
a single new flag. A bit-wise or sets the new flag while leaving the other
flags alone.
Show the code needed (using the String class) to test whether the user has entered the mystery phrase (read from a file named mystery). Ignore any (mis)capitalization they may enter by accident.
String fname = "mystery", user_phrase, file_phrase; ifstream file(fname.Cstring()); file_phrase.getline(file); cout << "Enter your phrase: "; user_phrase.getline(cin); user_phrase.unsetf(String::case_on); if (user_phrase == file_phrase) { cout << "\nYou guessed the mystery phrase!\a\n"; } else { cout << "\nI'm sorry, but that is incorrect...please try again!\n"; }
Assuming you had a ~ operator in the String class, show how you would use it to test if the user's word is a palindrome. (Question: Should case matter?)
String user_word; cout << "What is your word? "; cin >> user_word; user_word.unsetf(String::case_on); if (user_word == ~user_word) { cout << "\nThat was a palindrome!\n"; } else { cout << "\nThat was NOT a palindrome!\n"; }
Show the code needed to read a user's menu choice from a menu having 15 choices which can be chosen by either capitalized letter (case-insensitively) or by number. (Hint: Since the numbers are 2 digits long, you might want to use something that can read letters or numbers of more than one digit.) Show how to test their entry against menu item 1 (letter O) and menu item 15 (letter X).
String choice; short code; cin >> choice; if (isdigit(choice[0])) { choice >> code; } else { code = static_cast<short>(toupper(choice[0])); } switch (code) { case 1: case 'O': { // first option chosen } break; // etc... case 15: case 'X': { // last option chosen } break; }