Create a 'STRING' class which overloads ‘= = ' operator to compare two STRING objects
/*
*C++ Program that compares tow stirngs using operator overloading
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class STRING
{
char str[20];
public:
//Read user input
void getdata()
{
gets(str);
}
//Overload the operator
int operator ==(STRING s)
{
if(!strcmp(str,s.str))
return 1;
return 0;
}
};
//Driver program
int main()
{
STRING s1,s2;
cout<<"Enter first string :";
s1.getdata();
cout<<"Enter second string :";
s2.getdata();
if(s1==s2)
{
cout<<"Strigs are Equal"<<endl;
}
else
{
cout<<"Strings are Not Equal"<<endl;
}
return 0;
}
Comments
Leave a comment