i am trying to overload +operator to concat 2 strings
i tried to write a code but it is giving 2 errors and i am also not sure whether logic is correct..MY CODE is

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{

 char *str;
 public:
       string()
       {
	cout<<"enter string";
	cin>>str;
       }
       void display()
       {
	  cout<<str;
       }

	 friend  string operator + (string,string);

};
	string operator + (string a,string b)
 {
       string c;
	 int m=strlen(a.str);
	strcpy(c.str,a.str);
       c.str[m]=" ";             //error:cannot convert char * to char
       m++;
       for(int len=m+1;b.*str!='\0';len++)  //error:undefined symbol str
       {
	c.str[len]=b.*str;
	b.str++;
       }
       return(c);
       }


void main()
{
 string p,q,c;
c=p+q;
c.display();
getch();
}

Recommended Answers

All 13 Replies

Line 27 should be:

c.str[m]=' ';

Line 29 should be:

for(int len=m+1;(*b.str)!='\0';len++)

This is how I would solve it:

class string
{

public:

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

friend string operator+(const string & lhs, const string & rhs); 

};

const char& string::operator[](int offset) const
{
	if (offset < itssize)
		return thestring[offset];
	throw xboundary(offset);
	return thestring[0];
}

char& string::operator[](int offset)
{
	if (offset < itssize)
		return thestring[offset];
	throw xboundary(offset);
	return thestring[0];
}

string operator+(const string & lhs, const string & rhs)
{
	string temp(lhs.itssize + rhs.itssize);//itssize is size of strings
	int i = 0;
	for (i = 0; i < lhs.itssize; ++i)
		temp.thestring[i] = lhs.thestring[i];
	for (int j = 0; j < rhs.itssize; ++i, ++j)
		temp.thestring[i] = rhs.thestring[j];
	temp.thestring[temp.itssize] = '\0';
	return temp;
}

it removed errors ,i tried like this

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{

 char *str;
 public:
      void input()
       {
	cout<<"enter string";
	cin>>str;
       }
       void display()
       {
	  cout<<str;
       }

	 friend  string operator + (string,string);

};
	string operator + (string a,string b)
 {
       string c;
	 int m=strlen(a.str);
	strcpy(c.str,a.str);
       c.str[m]=' ';
       m++;
       for(int len=m+1;(*b.str)!='\0';len++)
       {
	c.str[len]=*b.str;
	b.str++;
       }
       return(c);
       }


void main()
{
 string p,q,r;
 p.input();
 q.input();
r=p+q;
r.display();
getch();
}

but after entering 1st and 2nd string the program returns back to the blue screen of tc

strcpy(c.str,a.str);

When you call the above on line 26, c.str doesn't have any memory allocated to it yet. Determine the length of p and q, and then allocate adequate memory to str to hold p, q and a null char. Then call strcpy() as you did and then you can call strcat().

In addition, when you write

r = p + q;

You will be doing a shallow copy of the return value of p + q to r. That could be problematic down the road. Better to overload the assignment operator for the string class implementing a deep copy instead of a shallow copy.

Also, I would encourage you to call your class String or myString instead of string to avoid confustion with the STL string class.

And, although nothing to do with your question, main() should be declared with return type of int, not void, even if your compiler allows you to use type void in this context.

how can i allocate memory to str?

Use dynamic memory allocation with the new operator using the syntax for more than one item:

str = new char[x];

where x is the number of char you want str to have (remember to include enough char to allow for a terminating NULL char). The compiler will then do x * amount of memory needed for a single char to determine how much memory to allocate to str. str will actually be the first memory address allocated. All of the memory in str will be sequential/consecutive in RAM, just like an array that is declared statically (that is without using the new operator).

it comes an error //undefined symbol str

i am really sorry for the foolish question above.two strings got concated but some extra characters were visible

So what input did you use and what output did you get? It doesn't help to say "It didn't work". Posting revised code is also helpful.

i got it.it was bcoz of i forgot to write this

n=strlen(b.str);
c.str[m+n]='\0';

now the code works perfectly
thankyou all of u without u it wont have been possible:)

i still have a doubt p.str and q.str had no memory allocated but they still accepted a string each:-O

i also understand this but how to concatenate two or more string usin operating overloading

Member Avatar for iamthwee

std:string does it for you.

string a = "hi";
string b = "mofo";

cout << a + b;

etc...

Please don't revive old threads, create a new one for your question.

i also understand this but how to concatenate two or more string usin operating overloading

Did you study the code in this thread? The OP created a class similar to std::string that concantinates two character arrays (not std::strings).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.