I do not know why string3 is not being displayed here

I am adding two strings using an overloaded operator+

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
//Header file newString.h
#ifndef H_newString
#define H_newString
#include <iostream>

using namespace std;

class newString
{
		//Overloads the stream insertion and extraction operators
    friend ostream& operator<<(ostream&, const newString&);
    friend istream& operator>>(istream&, newString&);

public:
    const newString& operator=(const newString&);
		//Overloads the assignment operator
		 newString operator+(const newString& ) const;
    newString(const char *);
		//constructor; conversion from the char string
    newString();
		//default constructor to initialize the string to null
    newString(const newString&);
		//copy constructor
    ~newString();
		//destructor

    char& operator[] (int);
    const char& operator[](int) const;

		//Overloads the relational operators
    bool operator==(const newString&) const;
    bool operator!=(const newString&) const;
    bool operator<=(const newString&) const;
    bool operator<(const newString&)  const;
    bool operator>=(const newString&) const;
    bool operator>(const newString&)  const;

private:
    char *strPtr;   //pointer to the char array
                    //that holds the string
    int strLength;  //data member to store the length
					//of the string
	string s1, s2;
};
#endif
 

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250



//Implementation file newString.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "newString.h"

using namespace std;


      //constructor: conversion from the char string to newString
newString::newString(const char *str)
{
   strLength = strlen(str);
   strPtr = new char[strLength+1]; 	//allocate memory to store
  									//the char string
   assert(strPtr != NULL);
   strcpy(strPtr,str);				//copy the string into strPtr
}

	//default constructor to store the null string
newString::newString()
{
		strLength = 0;
		strPtr = new char[1];
		assert(strPtr != NULL);
		strcpy(strPtr,"");
}

newString::newString(const newString& rightStr)  //copy constructor
{
	strLength = rightStr.strLength;
	strPtr = new char[strLength + 1];
	assert(strPtr != NULL);
	strcpy(strPtr, rightStr.strPtr);
}

newString::~newString()  //destructor
{
	delete [] strPtr;
}

//Overload the assignment operator
const newString& newString::operator=(const newString& 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;
}

char& newString::operator[] (int index)
{
	assert(0 <= index && index < strLength);
	return strPtr[index];
}

const char& newString::operator[](int index) const
{
	assert(0 <= index && index < strLength);
	return strPtr[index];
}

		//Overloads the relational operators
bool newString::operator==(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) == 0);
}

bool newString::operator<(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) < 0);
}

bool newString::operator<=(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) <= 0);
}

bool newString::operator>(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) > 0);
}

bool newString::operator>=(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) >= 0);
}

bool newString::operator!=(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) != 0);
}

newString newString::operator+(const newString& right) const
 {
     newString temp;
         temp.s1 = s1 + right.s1;
         temp.s2 = s2 + right.s2;

         return temp;
 }
//Overloads the stream insertion operator <<
ostream& operator<<(ostream& osObject, const newString& str)
{
	osObject<<str.strPtr;
	return osObject;
}

//Overload the stream extraction operator >>
istream& operator>>(istream& isObject, newString& str)
{
	char temp[81];

	isObject>>setw(81)>>temp;
	str = temp;
	return isObject;
}

//Implementation file newString.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "newString.h"

using namespace std;


      //constructor: conversion from the char string to newString
newString::newString(const char *str)
{
   strLength = strlen(str);
   strPtr = new char[strLength+1]; 	//allocate memory to store
  									//the char string
   assert(strPtr != NULL);
   strcpy(strPtr,str);				//copy the string into strPtr
}

	//default constructor to store the null string
newString::newString()
{
		strLength = 0;
		strPtr = new char[1];
		assert(strPtr != NULL);
		strcpy(strPtr,"");
}

newString::newString(const newString& rightStr)  //copy constructor
{
	strLength = rightStr.strLength;
	strPtr = new char[strLength + 1];
	assert(strPtr != NULL);
	strcpy(strPtr, rightStr.strPtr);
}

newString::~newString()  //destructor
{
	delete [] strPtr;
}

//Overload the assignment operator
const newString& newString::operator=(const newString& 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;
}

char& newString::operator[] (int index)
{
	assert(0 <= index && index < strLength);
	return strPtr[index];
}

const char& newString::operator[](int index) const
{
	assert(0 <= index && index < strLength);
	return strPtr[index];
}

		//Overloads the relational operators
bool newString::operator==(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) == 0);
}

bool newString::operator<(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) < 0);
}

bool newString::operator<=(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) <= 0);
}

bool newString::operator>(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) > 0);
}

bool newString::operator>=(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) >= 0);
}

bool newString::operator!=(const newString& rightStr) const
{
	return(strcmp(strPtr, rightStr.strPtr) != 0);
}

newString newString::operator+(const newString& right) const
 {
     newString temp;
         temp.s1 = s1 + right.s1;
         temp.s2 = s2 + right.s2;

         return temp;
 }
//Overloads the stream insertion operator <<
ostream& operator<<(ostream& osObject, const newString& str)
{
	osObject<<str.strPtr;
	return osObject;
}

//Overload the stream extraction operator >>
istream& operator>>(istream& isObject, newString& str)
{
	char temp[81];

	isObject>>setw(81)>>temp;
	str = temp;
	return isObject;
}

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
//Test Program
#include <iostream>
#include <cstring>
#include "newString.h"

using namespace std;

int main()
{
	newString s1 = "Sunny"; 	//Initialize s1 using
							//the assignment operator
	const newString s2("Warm"); 	//Initialize s2 using
 								//the conversion constructor
	newString s3;  //Initialize s3 to null
	newString s4;  //Initialize s4 to null

    newString str1 = "Hello";
    newString str2 = "there";
    newString str3 ;
    str3 = str1 + str2;
    cout <<endl << str3;
	cout<<"Line 1: "<<s1<<"    "<<s2<<"  ***"
		<<s3<<"###."<<endl; 							//Line 1

	if(s1 <= s2)     		      	//Compare s1 and s2; Line 2
		cout<<"Line 3: "<<s1<<" is less than or equal to "<<s2
			<<endl; 									//Line 3
	else												//Line 4
		cout<<"Line 5: "<<s2<<" is less than "<<s1
			<<endl; 									//Line 5

	cout<<"Line 6: Enter a string that has "
		<<"at least 7 characters --> ";  				//Line 6
	cin>>s1;         							//input s1; Line 7
	cout<<endl<<"Line 8: New value of s1 = "<<s1
		<<endl;   		 								//Line 8

	s4 = s3 = "Birth Day"; 								//Line 9
	cout<<"Line 10: s3 = "<<s3<<", s4 = "<<s4<<endl;  	//Line 10

	s3 = s1;  											//Line 11
	cout<<"Line 12: The new value of s3 = "<<s3<<endl;	//Line 12

	s1 = "Bright Sky";  								//Line 13

	s3[1] = s1[5];										//Line 14
	cout<<"Line 15: After replacing the second character of s3 = "
		<<s3<<endl; 									//Line 15

	s3[2] = s2[3];				  						//Line 16
	cout<<"Line 17: After replacing the third character of s3 = "
		<<s3<<endl; 									//Line 17

	s3[5] = 'g'; 										//Line 18
	cout<<"Line 19: After replacing the sixth character of s3 = "
		<<s3<<endl; 									//Line 19

	return 0;
}
Everything else works fine and gets displayed except for str3 where str1 and str2 are added with the operator +

please help
Hello Bopaki,

This is just a guess, but in the ctor, overloaded ctor and overloaded "=" you use "strcpy" to copy the char pointers. Why in the overloaded "+" do you think you can add two char pointers together?

To my knowledge there is no overloaded "+" for char pointers like there is for the "std::string".

I am thinking that you would want to use "strcpy(...)" and maybe even "strcat(...)" in the overloaded "+" .

Just a thought for now. I will have to give it a try.

Hope that helps,

Andy
You only use members s1 and s2 in the + operator. You don't need them and shouldn't have them.

I'd create a method to assign a newString from a const char * and then implement the various constructors and assignment operator using that. You just have to make sure that the object is consistent before calling the assignment operator. In this case that means setting strPtr to 0 so the delete[] will be harmless.

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
//constructor: conversion from the char string to newString
newString::newString(const char *str) :
    strPtr(0)
{
    *this = str;
}

const newString & newString::operator=(const char *str)
{
    if (str != strPtr) {
        delete[] strPtr;
        strLength = strlen(str);
        strPtr = new char[strLength + 1];       //allocate memory to store
        //the char string
        assert(strPtr != NULL);
        strcpy(strPtr, str);                     //copy the string into strPtr
    }
    return *this;
}

        //default constructor to store the null string
newString::newString() :
    strPtr(0)
{
    *this = "";
}

newString::newString(const newString & rightStr) :      //copy constructor
    strPtr(0)
{
    *this = rightStr.strPtr;
}

Hello Bopaki,

Overall dhayden has the best solution.

For what it is worth and something to consider I came up with this for a solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
newString newString::operator+(const newString& right) const
{
	newString temp;

	temp.strLength = this->strLength + right.strLength + 1;

	temp.strPtr = new char[temp.strLength + 1];

	strcpy(temp.strPtr, this->strPtr);
	strcat(temp.strPtr, " ");
	strcat(temp.strPtr, right.strPtr);

	return temp;
}

This is not the best solution, but it is in line with some of your other code.

Something to think about,

Andy
Topic archived. No new replies allowed.