Okay this program is really pissing me off! After line 95, the member variable is suddenly getting changed (That is, the 2nd string). Even after looking at it for 3 hours, I've failed to crack it :(
Please help me out guys!
# include <iostream>
# include <cstring>
using namespace std;
class strings
{
private :
char *strng;
public :
strings(const char * str)
{
int length = strlen(str);
strng = new char[length];
int i;
for(i = 0; i < length; i++)
{
strng[i] = str[i];
}
strng[length] = '\0';
}
~strings()
{
delete(strng);
strng = NULL;
}
void string_show()
{
cout << strng;
}
int string_length()
{
return strlen(strng);
}
int operator==(strings obj)
{
if(strlen(strng) != strlen(obj.strng))
{
cout << "\n\n\t* INSIDE FUNCTION!";
cout << "\n\n\t" << strng;
cout << "\n\n\t" << obj.strng;
return 0;
}
int i;
for(i = 0; i < strlen(strng); i++)
{
if(strng[i] != obj.strng[i])
{
cout << "\n\n\t** INSIDE FUNCTION!";
cout << "\n\n\t" << strng;
cout << "\n\n\t" << obj.strng;
return 0;
}
}
cout << "\n\n\t*** INSIDE FUNCTION!";
cout << "\n\n\t" << strng;
cout << "\n\n\t" << obj.strng;
return 1;
// **UPTO HERE, THE STRING IS NOT CHANGED!!**
}
};
int main()
{
strings obj_1("Hello");
strings obj_2("Hello");
cout << "\n\n\t1st String : ";
obj_1.string_show();
cout << "\n\n\t2nd String : ";
obj_2.string_show();
cout << "\n\n\tLength Of 1st String : " << obj_1.string_length();
cout << "\n\n\tLength Of 2nd String : " << obj_2.string_length();
cout << "\n\n\t1st String ~ Length : " << obj_1.string_length() << "\n\n\t";
obj_1.string_show();
cout << "\n\n\t2nd String ~ Length : " << obj_2.string_length() << "\n\n\t";
obj_2.string_show();
// **UPTO THIS, THE STRING IS NOT CHANGED**
if(obj_1 == obj_2) // **HERE, INSIDE THE FUNCTION, THE STRING IS NOT CHANGED**
cout << "\n\n\t\tEqual Strings!";
else
cout << "\n\n\t\tUnequal Strings!";
// **THE STRING IS NOW CHANGED!**
cout << "\n\n\t1st String ~ Length : " << obj_1.string_length() << "\n\n\t";
obj_1.string_show();
cout << "\n\n\t2nd String ~ Length : " << obj_2.string_length() << "\n\n\t";
obj_2.string_show();
return 0;
}