Create a 'STRING' class which overloads ‘= = ' operator to compare two STRING objects.
#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;
}
Comments
Leave a comment