Please explain how this program working?

#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;
}
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?
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
This may be slightly better, though the array s[20] can be easily overflowed, making it very fragile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <conio.h>
#include <cstring>

using namespace std;

class strings
{
    char s[20];

public:
    strings()                  // default constructor
    {
        s[0] = '\0';
    }

    strings(const char *c)     // constructor which takes a c-string
    {
        strcpy(s,c);
    }

    strings operator+(strings x1)  // concatenate with operator +
    {
        strings temp(s);
        strcat(temp.s, x1.s);
        return temp;
    }

	// friend function to allow easy output 
    friend ostream & operator<<(ostream & os, const strings & str)
    {
        return os << str.s;
    }

};

int main()
{
    strings s1("test");
    strings s2("run\0");

    strings concatstr = s1+s2;

    cout << "\n Concatenated string:" << concatstr;
    getch();
}
is there any reason of using <conio.h> ?

I wonder if this header has something specific that usual c++ headers do not have :/
Probably the same reason as using <iostream.h>

Outdated tools, being taught ancient methods, etc.
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.
It seems like this same question has been asked before,

August 2014,
Disch: "I can guarantee the code is broken."
http://www.cplusplus.com/forum/beginner/141438/#msg746934

May 2015:
ne555: "`temp' is uninitialized in `operator+' "
http://www.cplusplus.com/forum/general/165623/#msg837688

January 2016:
Chervil: "This code is not valid. temp is an uninitialised pointer. "
http://www.cplusplus.com/forum/general/182265/#msg893284


I guess sooner or later the message may get through...
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;
}
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
Topic archived. No new replies allowed.