Overloading Operators

I am currently reading this topic. The textbook gives an example but cannot function properly. I am using Dev-C++.
It uses the operator +, to make it a tool to combine two strings. For example, if the first word is "Barack ",the second word is "Obama",then using this overloaded operator will make it "Barack Obama".

This file is named "Ch10-05.h"
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
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;

class Str{
public:
void show() {cout<<data;}
Str (const Str&);
Str (const char* ptr);
Str (int);
~Str() {delete [] data;}
Str operator+(Str);
Str operator+(char *);
private:
char* data;
int len;
};

Str::Str (const char* s)
{
int len = strlen(s);
data = new char [len+1];
strcpy(data,s);
}

Str::Str (int n=0)
{
if (n<0)
n=0;
len = n;
data = new char [len+1];
for (int i=0;i<n;i++)
data[i]=' ';
data[n]=0;
}

Str::Str (const Str& s)
{
len = s.len;
data = new char[len+1];
strcpy(data,s.data);
}  

Str Str::operator+(Str s)
{
Str tmp(len+s.len);
strcpy(tmp.data,data);
strcat(tmp.data,s.data);
return tmp;
}

Str Str::operator+(char* ptr)
{
Str tmp(len+strlen(ptr));
strcpy(tmp.data,data);
strcat(tmp.data,ptr);
return tmp;
}


The main code is:
1
2
3
4
5
6
7
8
9
#include "Ch10-05.h"

int main()
{
Str s1("Barack "), s2("Obama");
(s1+s2).show();
cout<<endl;
system("pause");
}


When I run this,the screen displayed(for one second) "This Application has requested the runtime to terminate it in an unusual way. Please contact the application's support team for more information"

I do not know what happens, anybody help me please
Last edited on
Line 22 should not declare len as int, it should be using the data member len.
Thank you. I do not know such a redundant int would pose such a big problem.

And some more questions,please.
1
2
3
4
5
6
7
8

Str Str::operator+(Str s)
{
Str tmp(len+s.len);
strcpy(tmp.data,data);
strcat(tmp.data,s.data);
return tmp;
}

How should I interpret the first and second Str in Str Str::operator+(Str s)?
Is it correct to see the word "Obama" acting as a parameter for operator+ (i.e. the s)?
So where is the first word "Barack " being represented?
Thank you!
Last edited on
The way you're using it, yes, you should see "Obama" acting as the argument and "Barack" is represented by data. You're calling the operator function like this:
(s1+s2).show();

You can think of the s1+s2 as being equivalent to:
s1.operator+(s2)

This means that when you reference variables directly, without using them by accessing s or tmp, you're using the current object's data members. You could write it like this if it would be clearer:
1
2
3
4
5
6
7
8
9
10
11
Str Str::operator+(Str s)
{
// Make a string as large as the current string plus the new string.
Str tmp(this->len + s.len);
// Put the current string in our temp string.
strcpy(tmp.data, this->data);
// Concatenate on the new string.
strcat(tmp.data, s.data);
// And return our result.
return tmp;
}


So, in your main code example you're calling operator+ from s1, which is equal to "Barack", and sending it s2, which is equal to "Obama". One thing that might work a bit better would be:
1
2
Str firstName("Barack"), space(" "), lastName("Obama");
((firstName+space)+lastName).show();


There are more elegant solutions, but that one doesn't require you to change your code. > : )
Topic archived. No new replies allowed.