ques>Define a class String. Use overloaded == operator to compare two strings
ans>
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
int length;
char *p;
public:
string(char *s)
{
length=strlen(s);
p=new char[length+1];
strcpy(p,s);
}
void operator==(string &s)
{
if(p==s.p)
{
cout<<"Equal\n";
}
else
{
cout<<"not equal\n";
}
}
};
main()
{
clrscr();
string s1="surbhi";
string s2="jain";
s1==s2;
string s3="surbhi";
string s4="surbhi";
s3==s4;
getch();
return 0;
}
ans should be not equal, equal
but ans coming is not equal, not equal
pls help where is the error?
You are comparing the pointers (if they point to the same memory address)
Also, your compiler does not follow the standard.
so, please tell the solution...
oh got it i have to use strcmp :)