Operator== Overload
Oct 2, 2013 at 10:58pm UTC
One of the questions on my midterm tomorrow is as follows:
1. Given the class definition below, write a member function that checks if two strings (as defined below) are equal. Give PRE and POST conditions for the operator== you write.
1 2 3 4 5 6 7
class String {
public :
string() { s[0] = 0; };
String operator ==(const String) const ;
private :
char s[256]; //null terminated character array
};
Can someone tell me if what my answer is is correct? Or if someone could push me in the right direction? Here's what I have..
1 2 3 4 5 6 7 8
bool String::operator == (const String& rhs)const {
for (int i=0; i < 256; i++)
if (s[i] != rhs.s[i])
return false ;
return true ;
}
Oct 3, 2013 at 12:08am UTC
There are three errors in the class definition. Are those typos?
Have you tried compiling what you have?
Oct 3, 2013 at 3:59am UTC
No I have not tried compiling it. It isnt going to compile because there is no main or anything; it is just a snippet of code.
Oct 3, 2013 at 4:01am UTC
If you ignore the error about there not being a main function, you can still check the syntax.
Oct 3, 2013 at 4:06am UTC
Does the code I have look correct on checking if two strings are equal? I don't see why it wouldn't work but I just wanted a second opinion on it.
Oct 3, 2013 at 4:09am UTC
There's typos, so to be technical, no it doesn't look correct.
Topic archived. No new replies allowed.