-1

I have just started learning C++ a few days back. I was given an assignment to demonstrate + operator overloading to concatenate two strings. I came up with this solution:

#include <iostream>

using namespace std;

class Strcpy{
private:
    char* wrd;
    int len;

public:
    Strcpy();
    Strcpy(char* );

    void Display();

    friend Strcpy operator + (Strcpy, Strcpy);
    friend Strcpy concatinator(Strcpy, Strcpy);
};

Strcpy :: Strcpy(){
    wrd = '\0';
    len = 0;
}

Strcpy :: Strcpy(char* w){
    int i; len = 0;
    for(i = 0; w[i] != '\0' ; i++)
        len ++;
    wrd = w;
}

void Strcpy :: Display(){
    cout << "\nOutput: " << wrd << " "<< len;
}


Strcpy operator + (Strcpy obj1, Strcpy obj2){
    Strcpy temp;
    int i;
    temp.wrd = new char[obj1.len + obj2.len];
    temp = concatinator(temp, obj1);
    temp = concatinator(temp, obj2);
    temp.wrd[temp.len] = '\0';
    return temp;

}

Strcpy concatinator(Strcpy obj, Strcpy temp){
    for(int i = 0; temp.wrd[i] != '\0'; i++)
        {
            obj.wrd[obj.len] = temp.wrd[i];
            obj.len++;
        }
    return obj;
}

int main(){
    Strcpy word, word_I("Hello"), word_II("World");
    word = word_I + word_II;
    word.Display();
    return 1;
}

Some things to be noted:

  • deprecated conversion from string constant to 'char*' [-Wwrite-strings] I realize this is being caused because I am converting an immutable type to a mutable one but what alternative approach can I try to get rid of this.
  • I want to avoid using friend functions, but the overloaded operator needs two arguments which isn't possible if it remains a class member.
  • The following line works the same even if it is changed, why is this happening:

    temp.wrd = new char[obj1.len + obj2.len];
    //changed to
    temp.wrd = new char[//any number here];
    
  • I want avoid using string functions if that is possible at all.

  • Whenever i try taking an input in the following form, it crashes:

    char* Strcpy :: get(){
        char* temp;
        cin >> temp;
        return temp;
    }
    
    int main(){
        Strcpy word;
        Strcpy word_I(word.get()), word_II(word.get());
        word = word_I + word_II;
        word.Display();
        return 1;
    }
    
  • Lastly, I would appreciate any help that would help me improve on the existing solution and some explanation so as to why it is better and the mistakes I am making.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 4
    You have more fundamental problems to worry about. You are leaking memory. You are [violating the Rule Of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). You must fix these problems first, then worry about this. – Sam Varshavchik Feb 24 '17 at 14:14
  • 1
    Additionally in get you don't allocate any memory – Daniel Jour Feb 24 '17 at 14:14
  • A + operator inside a class can be used. The binary + has one argument - the 2nd. The first argument is the instance itself (i.e. this). – Scheff's Cat Feb 24 '17 at 14:16
  • @SamVarshavchik is totally right. Better learn C++ first before writing some code which you may have googled while programming and put together – Lazcano Feb 24 '17 at 14:22
  • @SamVarshavchik thank you for the information il start there. – Shaurya Singh Feb 24 '17 at 14:22
  • do not remove information that changes the question so much. Edit and add information instead. And do not put javascript snippets in any other languages' code – phuclv Mar 06 '17 at 16:29
  • Oh, sorry i'll keep this in mind @LưuVĩnhPhúc – Shaurya Singh Mar 06 '17 at 16:33

2 Answers2

0

If I understood this right

I want to avoid using friend functions, but the overloaded operator needs two arguments which isn't possible if it remains a class member.

your statement is wrong.

Sample for a binary + operator in a class:

#include <iostream>

class Int {
  private: int _i;
  public:
    Int(int i = 0): _i(i) { }
    Int operator + (const Int &i) const
    {
      return Int(_i + i._i);
    }
    int get() const { return _i; }
};

int main(int, char**)
{
  Int i1(1), i2(2);
  Int i;
  i = i1 + i2;
  std::cout << "i: " << i.get() << std::endl;
  return 0;
}

Compiled and tested with gcc on cygwin:

$ g++ -o test-op-plus test-op-plus.cc

$ ./test-op-plus.exe 
i: 3
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • What i meant to say was this: Strcpy::operator+(Strcpy, Strcpy)' must take either zero or one argument| Thank you for the information though, much appreciated. – Shaurya Singh Feb 24 '17 at 14:36
0

deprecated conversion from string constant to 'char*' [-Wwrite-strings] I realize this is being caused because I am converting an immutable type to a mutable one but what alternative approach can I try to get rid of this.

You never modify *w, so you can use a pointer to const instead.

I want to avoid using friend functions, but the overloaded operator needs two arguments which isn't possible if it remains a class member.

The first argument of a member operator overload is the implicit this pointer. If you declare Strcpy Strcpy::operator+(const Strcpy&) const, it will be a binary operator. That said, the friend operator is probably a better approach.

The following line works the same even if it is changed, why is this happening:

temp.wrd = new char[obj1.len + obj2.len];
//changed to
temp.wrd = new char[//any number here];

It will work as long as "any number" is large enough to contain the entire string. If you write outside of the bounds, the behaviour is undefined.

Whenever i try taking an input in the following form, it crashes:

char* temp;
cin >> temp;

The stream extraction operator requires that a char* passed to it must point to an array sufficiently large to contain the user input. You forgot to initialize temp, so the requirement is not satisfied. As a result, the behaviour of the program is undefined. Solution: Allocate some memory and initialize temp to point to that memory.

The same bug occurs the constructor Strcpy(char*). You don't initialize this->wrd, but you dereference it. Therefore the behaviour is undefined. The solution is the same as above.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thanks for that last addition about initializing to an array before dereferencing and all the other inputs. Probably I will be looking up memory allocation before attempting the problem again. – Shaurya Singh Feb 24 '17 at 15:15