Not sure as to why I am getting a garbage output there

I get:
1
2
3
C:\Dev-Cpp\chapter3>question17
`8=
C:\Dev-Cpp\chapter3>


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 #include<iostream>
#include<string>
#include<cassert>
#include<iomanip>
using namespace std;

class overloadOprPlus
{
      friend ostream& operator<<(ostream&, const overloadOprPlus&);
      friend istream& operator>>(istream&, overloadOprPlus&);
public:
       const overloadOprPlus& operator+(const overloadOprPlus&);
       overloadOprPlus(const char*);
          //constructor; conversion from the char string
       overloadOprPlus();
          //default constructor to initialize string to null
       overloadOprPlus(const overloadOprPlus&);
          //copy constructor
       ~overloadOprPlus();
 private:
      char *strPtr;       //pointer to the char array
                        //that holds the string
         int strLength; //data member to store the length
                        // of the string
};
overloadOprPlus::overloadOprPlus(const char *str)
{
    strLength = strlen(str);
    strPtr = new char[strLength + 1];
    assert(strPtr != NULL);
    strcpy(strPtr, str);
}
    //default constructor to store the null string
overloadOprPlus::overloadOprPlus()
{
    strLength = 0;
    strPtr = new char[1];
    assert(strPtr != NULL);
    strcpy(strPtr,"");
}
   //copy constructor
overloadOprPlus::overloadOprPlus(const overloadOprPlus& rightStr)
{
   strLength = rightStr.strLength;
   strPtr = new char[strLength + 1];
   assert(strPtr != NULL);
   strcpy(strPtr, rightStr.strPtr);
}
overloadOprPlus::~overloadOprPlus()   //destructor
{
     delete [] strPtr;
}
     //Overload the assignment operator
const overloadOprPlus& overloadOprPlus::operator+(const 
                       overloadOprPlus& rightStr)
{
   if(this != &rightStr)    //avoid self-copy
   {
        delete [] strPtr;
        strLength = rightStr.strLength;
        strPtr = new char[strLength + 1];
        assert(strPtr != NULL);
        strcpy(strPtr, rightStr.strPtr);
    }
    return *this;
}
    //Overload the insertion operator <<
ostream& operator<<(ostream& osObject, const overloadOprPlus& str)
{
        osObject << str.strPtr;
        return osObject;
}
     //Overload the extraction operator >>
istream& operator>>(istream& isObject, overloadOprPlus &str)
{
        char temp[81];
        isObject >> setw(81) >> temp;
        str = temp;
        return isObject;
}

int main()
{
    overloadOprPlus s1, s2, s3;
    
    s1 = "Hello " ;
    s2 = "there ";
    s3 = s1 + s2;
    cout << s3;
}  
The problems are line 86/87. You assign a string while you don't have an assignment operator for this.

What happens on that lines is that a temporary object of type overloadOprPlus is created. The content of that temporary object is copied to s1/s2 (the copy constructor is not used there). After the assignment the temporary object is destroyed and the memory freed. Now s1/s2 containing invalid pointer.

If you write this:

explicit overloadOprPlus(const char*);

The implicit conversion to a temporary object isn't possible and the compiler will throw errors on line 86/87.

What you can do is creating a copy assignment operator (recommended: rule of three) or an assignment operator for the c-string or both.
There are two problems.

First, you've overridden the copy constructor and the implicit conversion constructor from const char *, but not the assignment operator. On line 86 what you're actually doing is
 
s1 = overloadOprPlus("Hello ");
The default assignment operator performs a bitwise copy of the members of overloadOprPlus, but overloadOprPlus has a non-trivial destructor. Once the temporary overloadOprPlus("Hello ") is destructed, s1 is left in an unrecoverable invalid state, becuase its pointer is left pointing to memory that has been deleted.

Second, your definition of the operator+() overload is nonsense. You implemented as if it was operator=().
The typical operator+() overload will follow these rules:
1. It will accept a const T & (where T is the same class).
2. It will return a T, which is constructed anew in the implementation.
3. It will be a const function (this will not be modified).
4. Its semantics should be analogous to addition for the given type (operators should not be repurposed willy-nilly without good reason).
Thank you all!!!!
I got it right eventually
1
2
3
4
5
newString s1, s2, s3;
    s1 = "Hello " ;
    s2 = "there ";
    s3 = s1 + s2;
    cout << s3;

I got:
1
2
3
4
C:\Dev-Cpp\chapter3>untitled2
Hello there

C:\Dev-Cpp\chapter3>
Topic archived. No new replies allowed.