Answer to Question #183045 in C++ for Smith

Question #183045

Create a 'STRING' class which overloads ‘= = ' operator to compare two STRING objects.


1
Expert's answer
2021-04-20T15:22:13-0400
#include <iostream>
#include <string>
using namespace std;
class String{
    string s;
    public:
    String(string s){
        this->s = s;
    }
    bool operator==(const String &a){
        if(this->s.length() != a.s.length()){
            cout<<"\nfalse";
            return false;
        }
        else{
            for(int i = 0; i < this->s.length(); i++){
                if(this->s[i] != a.s[i]){
                    cout<<"\nfalse";
                    return false;
                }
            }
            cout<<"\ntrue";
            return true;
        }
    }
};
int main(){
    String s1("string"), s2("string2"), s3("string");
    s1 == s2;
    s1 == s3;
    s2 == s3;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS