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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s