Jan 6, 2016 at 2:21pm UTC
#include<iostream.h>
#include<conio.h>
#include<string.h>
class strings
{
char s[20];
public:
strings()
{
s[0]='\0';
}
strings(char *c)
{
strcpy(s,c);
} char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}
};
void main()
{
clrscr();
strings s1("test"), s2("run\0");
char *concatstr;
concatstr=s1+s2;
cout<<"\n Concatenated string"<<concatstr;
getch();
}
Can any one explain this step only. How will working this function.
strings(char *c)
{
strcpy(s,c);
}
char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}
Jan 6, 2016 at 2:57pm UTC
This is a constructor.
1 2 3 4
strings(char *c)
{
strcpy(s,c);
}
This is a completely separate member function:
1 2 3 4 5 6 7
char *operator +(strings x1)
{
char *temp; // not initialised
strcpy(temp,s); // error, writing to random memory
strcat(temp,x1.s); // error
return temp;
}
This code is not valid.
temp
is an uninitialised pointer.
Last edited on Jan 6, 2016 at 2:58pm UTC
Jan 6, 2016 at 3:39pm UTC
is there any reason of using <conio.h>
?
I wonder if this header has something specific that usual c++ headers do not have :/
Jan 6, 2016 at 3:46pm UTC
Probably the same reason as using <iostream.h>
Outdated tools, being taught ancient methods, etc.
Jan 6, 2016 at 4:02pm UTC
It's an old Borland compiler. It uses the original I/O Stream library, hence the stuff ending with .h.
conio.h is the interface to Borland's console library.
In the age of good free C++ compilers and supporting software, it's unforgivable to teach using 20 year old compilers that pre-date standardization.
Jan 11, 2016 at 1:42pm UTC
Kindly explain this step only..
strings(char *c)
{
strcpy(s,c);
} char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}
Jan 11, 2016 at 3:33pm UTC
MikeyBoy wrote:Please use code tags when posting code, to make it readable.
http://www.cplusplus.com/articles/z13hAqkS/
After 3 years and 49 posts on this forum, you should know this already. Why should we put any effort into helping you, if you can't be bothered putting in any effort to help us?
@r 4 raja:
I'm starting to suspect you're a troll.
Last edited on Jan 11, 2016 at 3:34pm UTC