Answer to Question #193476 in C++ for Sharath kumar

Question #193476

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


1
Expert's answer
2021-05-14T11:08:58-0400
#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;
}





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