9. Consider the following class mystring Class mystring { char str [100]; Public: // methods }; overload operator “= =” to compare two strings.


#include<iostream.h>
#include<conio.h>
class mystring
{
char str[20];
public:
int operator==(mystring&);
void get()
{
cout<<“\nEnter string: “;
cin>>str;
}
void disp()
{
cout<<str<<endl;
}
};
int mystring::operator==(mystring &t)
{
for(int i=0;str[i]!=”;i++)
{
for(int j=0;t.str[j]!=”;j++)
{
if(str[i]==t.str[j])
{
return 0;
}
else
{
return 1;
}
}
}
}
void main()
{
int result=0;
mystring s1,s2;
clrscr();
s1.get();
s2.get();
cout<<“\nFirst String: “;
s1.disp();
cout<<“\nSecond strind: “;
s2.disp();
result=s1==s2;
if(result==0)
{
cout<<“\nBoth string Equal:\n”;
}
else
{
cout<<“\nString not equal:\n”;
}
getch();
}

Leave a comment