I was trying to implement operator overloading in c-string in the following code
#include <cstring>
#include <iostream>
using namespace std;
class string
{
char *st;
int len;
public:
string():st(NULL), len(0) {}
string(const char *s);
string(const string & s);
~string();
friend string operator+(const string &s1, const string & s2);
friend int operator<=(const string &s1, const string &s2);
void show()
{
cout<<st<<endl;
}
};
string::string(const char *s)
{
len = strlen(s);
st = new char[len+1];
strcpy(st,s);
}
string::string(const string &s)
{
len = strlen(s);
st = new char[len+1];
strcpy(st,s);
}
string::~string()
{
delete st;
}
string operator+(const string &s1, const string &s2)
{
string ts;
ts.len = s1.len + s2.len;
ts.st = new char[ts.len+1];
strcpy(ts.st, s1);
strcat(ts.st, s2);
return ts;
}
string operator<=(const string &s1, const string &s2)
{
return strcmp(s1.st, s2.st);
}
int main()
{
string s1 = "new";
string s2 = "york";
string s3 = "delhi";
char ch;
cin>>ch;
return 0;
}
can anyone tell me the bug in this code.