Unexpected output

Desired output:
hi
hi samir
11
hi samir samir

But I am getting output:

hi
hi samir
11
hi

Why is this happening,what should I do to overcome this problem
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
  #include <iostream>
#include<string.h>
using namespace std;

class String
{
    char *c;
    int length;
public:
    String(){length=0;c=new char[2];strcpy(c,"");}
    String(char *p){length=strlen(p);c=new char[length+1];strcpy(c,p);}
    String(String &p){delete c;length=strlen(p.c);cout<<length<<endl;c=new char[length+1];strcpy(c,p.c);}
//    String(const String &p){length=strlen(p);c=new char[length+1];strcpy(c,p);}
    String operator+(String &p)
    {
        String s;
        s.length=length+p.length+1;
        s.c=new char[s.length+1];
        strcpy(s.c,c);
        strcat(s.c,p.c);
        cout<<s.length<<endl;
        return s;
    }
    bool operator==(String &p)
    {
        if(!strcmp(c,p.c))
            return true;
    }
    void showdata()
    {
        cout<<"hi "<<c<<endl;
    }
    ~String(){delete c;}
};
int main()
{
    String s;
    String s1("samir");
    s.showdata();s1.showdata();
    String s3;
    s3=s1+s1;
    s3.showdata();
    return 0;
}
Last edited on
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstring>
using namespace std;

class String {
    char *c;
    int length;

public:
    String() {
        length = 0;
        c = new char[1];
        *c = '\0';
    }

    String(const char *p) {
        length = strlen(p);
        c = new char[length + 1];
        strcpy(c, p);
    }

    String(const String &p) {
        // delete c; // this doesn't make sense; c doesn't point to anything yet
        length = strlen(p.c);
        c = new char[length + 1];
        strcpy(c, p.c);
    }

    String operator+(const String &p) {
        String s;
        delete s.c;  // you should delete the default empty string
        s.length = length + p.length;   // no + 1 needed here
        s.c = new char[s.length + 1];
        strcpy(s.c, c);
        strcat(s.c, p.c);
        return s;
    }

    String operator=(const String &p) {
        delete c;
        length = p.length;
        c = new char[length + 1];
        strcpy(c, p.c);
        return *this;
    }

    bool operator==(String &p) {
        return strcmp(c, p.c) == 0;
    }

    ~String() {
        delete c;
    }
    
    friend ostream& operator<<(ostream& os, const String& s) {
        return os << s.c;
    }
};

int main() {
    String s, s1("samir");
    cout << "Hi " << s << '\n';
    cout << "Hi " << s1 << '\n';
    String s2(s1 + s1);
    cout << "Hi " << s2 << '\n';
    String s3;
    s3 = s2 + s2;
    cout << "Hi " << s3 << '\n';
}

Last edited on
Thanks a lot
Actually, the deletes should be the array kind, e.g.,
 
    delete [] c;

Topic archived. No new replies allowed.