Operator== Overload

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;
}
There are three errors in the class definition. Are those typos?

Have you tried compiling what you have?
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.
If you ignore the error about there not being a main function, you can still check the syntax.
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.
There's typos, so to be technical, no it doesn't look correct.
Topic archived. No new replies allowed.