Here I wrote 2 overloaded operators:
stringb operator+(const stringb &a,const stringb &b)
{
stringb temp;
temp.len = a.len + b.len;
temp.p=new char[(temp.len)+1];
strcpy(temp.p, a.p);
strcat(temp.p, b.p);
return (temp);
}
stringb operator-(const stringb &a,const stringb &b)
{
stringb temp;
temp.len=a.len-b.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,a.p);
return (temp);
}
However, when I compile the actual code, the whole code works except the part when I call these operator, and I get garbage out put. What's wrong with my functions?
EDIT: Declaration of stringb class:
class stringb
{
public:
char *p;
int len;
public:
stringb()
{
len=0;
p=0;
}
stringb(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
stringb(const stringb &s)
{
len=s.len;//strlen(s);
p=new char[len+1];
strcpy(p,s.p);
}
~stringb()
{
delete p;
}
friend int operator==(const stringb &a,const stringb &b);
friend stringb operator+(const stringb &a,const stringb &b);
friend stringb operator-(const stringb &a,const stringb &b);
friend void putstring(const stringb a);
};