Part 2:
• A member function compare(String) that returns –1, 0, or 1 depending upon
whether the string is lexicographically less than, equal to, or greater than the
argument. Then, using this member function, provide definitions for the comparison
operators <, <=, ==, !=, >, and >=. |
Basically, the compare function will return one of the three values based on:
1) -1: The second string is longer or the characters are "larger"
--Ex: "abc" compared with "abcd" or "abc" compared with "bdp"
2) 0: The two strings are identical. Self-explanatory.
3) 1: The second string is shorter or the characters are "smaller"
--Ex: "abcd" compared with "abc" or "bdp" compared with "abc"
So, assuming this is a member function, it might look like:
1 2 3 4 5 6 7 8 9 10
|
//Mark this as read-only (const at the end) since this function should not
// be modifying anything. I'll also explain why this is useful later.
int String::compare(const String& sec) const{
if(len < sec.len) return -1; //Shorter
else if(len > sec.len) return 1; //Longer
//If both conditions above fail, we know that they are of equal length,
// so now you compare each individual character. Hint: A char has an
// ASCII value, so you can write stuff like 'a' < 'b' == true as if it were
// an int.
}
|
Once you've finished writing this function, you can use it to write your comparison operators in a flash. Because they're so trivial, I'll jot down all of them here (These don't need to be members or friends):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
bool operator==(const String& lhs, const String& rhs){
return (lhs.compare(rhs) == 0);
}
bool operator!=(const String& lhs, const String& rhs){
return !(lhs == rhs); //Calls previous function and reverses result
}
bool operator<=(const String& lhs, const String& rhs){
return (lhs.compare(rhs) == 0 || lhs.compare(rhs) == -1);
}
bool operator>=(const String& lhs, const String& rhs){
return (lhs.compare(rhs) == 0 || lhs.compare(rhs) == 1);
}
bool operator<(const String& lhs, const String& rhs){
return (lhs.compare(rhs) == -1);
}
bool operator>(const String& lhs, const String& rhs){
return (lhs.compare(rhs) == 1);
}
|
Note that I called compare() in each function. Here's a kicker: if compare() wasn't read-only, the compiler would refuse to compile this code. Why? Because that
const
at the end guarantees that the objects won't be modified. Since the parameters are
const
, you may only call member functions marked as read-only, so the compiler knows you aren't doing something illegal like modifying a
const
object.
• A function resize(int n, char c) that changes the size of the string to n, either truncating characters from the end, or inserting new copies of character c. |
The instruction is pretty clear. If you call resize(5, 'W') on a string holding "abc", the result should look like "abcWW" and resize(1, 'Q') would result in "a". Here's a code snippet (this function, too, should be a member function):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void String::resize(int n, char c){
char* hold = buffer; //Be sure to keep the old c-string temporarily
buffer = ???; //Similar to one line in the constructor I gave hints about
// Remember to make room for '\0' !
//Copy back cstring without '\0';
for(int i(0); i < len; ++i)
???;
//Now "fill" the string if the new length is longer
for(; len < n; ++len)
*( ??? ) = c;
len = n; //In case len > n
//hold now points to unused data on the heap; remember to free that memory
delete[] hold;
}
|
For this function, you should consider having a default value for
char c
.
• The function call operator, so that s(int start, int length) returns a substring starting at the given position of the given size. |
I'm surprised you guys aren't just making String::substr(). The function call operator is
( )
as in:
1 2
|
String myString;
String substring = myString(3, 6);
|
The result is what's called a "function object", or a "functor". Like the other operators, the function call operator follows the same basic prototype syntax. The only difference is that there are no restrictions on how many arguments it can take. For what this assignment asks of you, it might look like (my function will use some of the other member functions, but you don't have to do the same thing):
1 2 3 4 5 6
|
String String::operator() (int start, int length){
String toreturn(length, ' '); //Again, to hold the results; new constructor
for(; length > 0; --length)
??? = ???; //Overloaded subscript operator
return toreturn;
}
|
Hope this helps (if it wasn't superfluous for you).