-1
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class String
{
private:
    char* s;
    int size;
public:
    String()
    {
        s = NULL;
        size = 0;
    }

    String(const char* str)
    {
        size = strlen(str);
        s = new char[size];
        for (int i = 0; i < size; i++)
            s[i] = str[i];
    }

    String(const String& obj)
    {
        size = obj.size;
        s = new char[size];
        for (int i = 0; i < size; i++)
            s[i] = obj[i];
    }

    String(int x)
    {
        size = x;
        s = new char[size];
    }

    char& operator[](int i)
    {
        return *(s + i);
    }

    const char operator[](int i) const
    {
        return *(s + i);
    }

    String& operator+(const char& str) // append a char at the end of string
    {
        int newsize = size + 1;
        String newstr(size);
        for (int i = 0; i < size; i++)
        {
            newstr.s[i] = s[i];
        }
        newstr.s[size] = str;
        delete[]s;
        s = newstr.s;
        size = newsize;
        return *this;
    }

    String& operator+(char*& str) // append a string at the end of string
    {
        int newsize = size + strlen(str);
        String newstr(newsize);
        for (int i = 0; i < size; i++)
        {
            newstr.s[i] = s[i];
        }
        for (unsigned i = 0; i < strlen(str); i++)
        {
            newstr.s[size + i] = str[i];
        }
        delete[]s;
        s = newstr.s;
        size = newsize;
        return *this;
    }

    operator int() const
    {
        int m;
        m = size;
        return m;
    }
};

int main()
{
    String s1("abcd");

    String s2;
    s2 = s1 + 'e';

    cout << s2[3];
    cout << s2[4];
    char* c = (char*)"asdfgh";
    s2 = s1 + c;
    cout << s2[3];
    cout << s2[4];
    cout << s2[8];

}

My code runs perfectly until the statement char* c = (char*)"asdfgh". After this statement, I am not getting actual output. s2[3] after the statement char* c = (char*)"asdfgh" is supposed to give 'd' in the output, and similarly s2[4] is supposed to give 'a' in the output, and s2[8] is supposed to give 'g' in the output. But, the problem is, when I run this program, I am not getting these actual values.

Kindly look into this and tell me where I need to make changes.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

2 Answers2

2

Both of your operator+ implementations are wrong.

You are modifying the String object that is being added to, and then returning a reference to that object. You need to instead return a new String object that is the concatenation of the added-to and added-from String objects, without modifying the added-to String at all. What you have implemented would be more appropriate for an operator+= instead.

See What are the basic rules and idioms for operator overloading? for more details.

That said, there are some other problems with your code, as well. For instance, you are missing a destructor and a copy assignment operator, per the Rule of 3/5/0. You could also consider adding a move constructor and move assignment operator, as well.

Try something more like this:

#include <iostream>
#include <cstring>
#include <utility>
using namespace std;

class String
{
private:
    char* s;
    size_t size;

public:
    String()
    {
        s = nullptr;
        size = 0;
    }

    String(const char* str)
    {
        size = strlen(str);
        s = new char[size+1];
        for (int i = 0; i < size; ++i)
            s[i] = str[i];
        s[size] = '\0';
    }

    String(const String& str)
    {
        size = str.size;
        s = new char[size+1];
        for (int i = 0; i < size; ++i)
            s[i] = str[i];
        s[size] = '\0';
    }

    String(String&& str)
    {
        size = str.size; str.size = 0;
        s = str.s; str.s = nullptr;
    }

    String(size_t x)
    {
        size = x;
        s = new char[size+1];
        s[size] = '\0';
    }

    ~String()
    {
        delete[] s;
    }

    void swap(String& other)
    {
        std::swap(s, other.s);
        std::swap(size, other.size);
    }

    String& operator=(char ch)
    {
        String newstr(1);
        newstr[0] = ch;
        newstr.swap(*this);
        return *this;
    }

    String& operator=(const char* str)
    {
        String(str).swap(*this);
        return *this;
    }

    String& operator=(const String& str)
    {
        if (this != &str)
            String(str).swap(*this);
        return *this;
    }

    String& operator=(String&& str)
    {
        String(std::move(str)).swap(*this);
        return *this;
    }

    char& operator[](size_t i)
    {
        return s[i];
    }

    const char operator[](size_t i) const
    {
        return s[i];
    }

    String operator+(char ch)
    {
        /*
        String newstr(*this);
        newstr += ch;
        return newstr;
        */
        String newstr(size + 1);
        for (int i = 0; i < size; ++i)
        {
            newstr[i] = s[i];
        }
        newstr[size] = ch;
        return newstr;
    }

    String operator+(const char* str)
    {
        /*
        String newstr(*this);
        newstr += str;
        return newstr;
        */
        size_t slen = strlen(str);
        String newstr(size + slen);
        for (size_t i = 0; i < size; ++i)
        {
            newstr[i] = s[i];
        }
        for (size_t i = 0; i < slen; ++i)
        {
            newstr[size + i] = str[i];
        }
        return newstr;
    }

    String& operator+=(char ch)
    {
        String newstr(size + 1);
        for (size_t i = 0; i < size; ++i)
        {
            newstr[i] = s[i];
        }
        newstr[size] = ch;
        newstr.swap(*this);
        return *this;
    }

    String& operator+=(const char* str)
    {
        size_t slen = strlen(str);
        if (slen > 0)
        {
            String newstr(size + slen);
            for (size_t i = 0; i < size; ++i)
            {
                newstr[i] = s[i];
            }
            for (size_t i = 0; i < slen; ++i)
            {
                newstr[size + i] = str[i];
            }
            newstr.swap(*this);
        }
        return *this;
    }

    size_t getSize() const
    {
        return size;
    }
};

int main()
{
    String s1("abcd");

    String s2;
    s2 = s1 + 'e';

    cout << s2[3];
    cout << s2[4];
    s2 = s1 + "asdfgh";
    cout << s2[3];
    cout << s2[4];
    cout << s2[8];
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

The reason it fails is actually earlier

String s1("abcd");
String s2;
s2 = s1 + 'e';

becuase your operator+ changes its lhs and then returns a reference to itself s2 is now a copy of s1. But they both point to the same 's' char array, things go bang shortly after that.

This would be fixed if you had an operator= that does the same as your copy constructor, but you dont have that

And as Remy points out, your operator+ semantics are wrong anyway. You should return 'newStr'

pm100
  • 48,078
  • 23
  • 82
  • 145