Error in execution of program...(operator overloaading)

Mar 12, 2013 at 1:15pm
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?
Mar 12, 2013 at 1:22pm
You are comparing the pointers (if they point to the same memory address)

Also, your compiler does not follow the standard.
Mar 12, 2013 at 1:26pm
so, please tell the solution...
Mar 12, 2013 at 1:38pm
oh got it i have to use strcmp :)
Topic archived. No new replies allowed.