Question :Create a 'STRING' class which overloads ‘= = ' operator to compare two STRING objects
#include <iostream>
#include <string>
using namespace std;
class STRING {
private:
string inputString;
public:
STRING(string input_String){
this->inputString=input_String;
}
~STRING(){}
bool operator==(const STRING& other) {
return inputString.compare(other.inputString)==0;
}
};
int main(){
STRING STRING1("Test");
STRING STRING2("Test");
cout<<"STRING1 is Test\n";
cout<<"STRING2 is Test\n";
if(STRING1==STRING2){
cout<<"STRING1 == STRING2\n";
}else{
cout<<"STRING1 <> STRING2\n";
}
STRING STRING3("Test");
STRING STRING4("Test 2");
cout<<"STRING3 is Test\n";
cout<<"STRING4 is Test2\n";
if(STRING3==STRING4){
cout<<"STRING3 == STRING4\n";
}else{
cout<<"STRING3 <> STRING4\n";
}
system("pause");
return 0;
}
Comments
Leave a comment