Str Str:: operator+(const Str & append) const
{
	unsigned i,j;

        for(i=0,j=0 ; i<append.used; i++ ,j++)
		
		temp[i]=append.data[j];
	
	for(j=0 ; i<used; i++, j++)
	
		temp[i]=data[j];
	
	temp[i]='\0';
}

used and append.used are the length of strigs. and data is pointer to array.


Hi
I am trying to concatenate two strings in the class using temperory pointer temp. but it is not printing anything.Could you tell me what is wrong with my for loops because program works wit actual strcat().

How big is your string that you allocate as temp?

How did you declare your variable temp? Since this->data and append.data have dynamic lengths your temp-variable needs to be allocated dynamically.

char * temp = new char[used+append.used];

Also this string then must be deleted somewhere

delete [] temp;

also your function does not return anything, so you should at least get a warning in your cpmpiler. the operator should return a Str - object.
Those are the main problems in the moment, but there are further problems. Usually you are advised not to make binary operators like + and / members of a class but global functions because of the assymetry that the implicit this-argument poses.
but one step at a time...

Str Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;

	char *temp=new char[used+append.used+1];
	
	for(i=0,j=0 ; j<used; i++ ,j++)
		
		temp[i]=data[j];
	
	for(j=0 ; j<append.used; i++, j++)
	
		temp[i]=append.data[j];
	
	
		return Str(temp);   
}

Hi
I had return sttement and also new char but I just did not include that , my program is working with strct() but not with these two for loops . could you please tell me ?
Thanks.

looks ok to me, what does your Str(char*) constructor do? and what does your Str copy constructor do?

You are absolutely right , my append overloading is working but my Str (*char) no it is not working.
Here How I wrote that:

Str::Str(const char * s):
allocated(24) ,used(20), data(new char[allocated])
{
	unsigned i=0;
	
	for(used=0; i<used; i++)
	{
		
		  data[i]=s[i];
	}
	
}

See I am giving used an initial value but I should not do that because may be my Str object has mich larger or smaller lenght tahn used. Any help will be highly appriciated?
Thanks.

I'm not 100% sure that deleting data will free that memory (if someone can check me on that) since you are passing it in via the initialization list.

You should be able to use strlen and related functions on the string that's being passed into the constructor. That way you can allocate exactly how much you need.

Nothing wrong per se in allocating in the initialisation list. Everything that is new'd can be delete'd. It's a bit of a curious way thing are done here, but I assume this is a learning project and you have to go through a few things to understand why one way of doing things is better than the other one. You can allocate, initialise with NULL and other things. I'd be more curious about the hardcoding of allocated and used. If you want that, then you surely would be better off using fixed length arrays, no?
Also you have to be a bit careful: There is a slight trap for beginners. I haven't seen your class declaration, but if your variable "data" is declared *BEFORE* the variable allocated, then you code will not work as expected. A half-way decent compiler will warn you about this but not every compiler does. The problemis that members are initialised in th order they are declared and not in the order they appear in the init-list. So if allocated is declared after data then your allocate will have a "random" value. The new will then create an array of random size (or fail).

Thanks guys The purpose of my program is to write c++ string class which should be different from c string (c string is null terminated) but If I dont put the null character at the end of my for loop it appends some garbage characters at the end , so what should I do in order to avoid those characters. because for example if I have s1=Hello and s2=\0world then s1+s2 is just Hello which should not be like this for example in

Str Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;
    	
	char *temp=new char[strlen(data)+strlen(append.data)];	
	
	for(i=0,j=0; i<used;j++, i++ )
	
		
		temp[i]=data[j];
	
	for( i=used,j=0 ; i<append.used+used; j++,i++)
	
		temp[i]=append.data[i-used];
	
	temp[i]='\0';
	
	return Str(temp);                
}

You can see that I have temp='\0' , it means it will append null character at the end , what should I do in order to solve this problem.

If you're not going to null terminate, somehow you have to indicate the length of the string. You could have your custom string class carry around a length member (or member function to return the length).

Thank you , I mean how std:: string is implemented because I heard in std::string null character can be anywhere in string and we can have many null characters in one string ?
How can I convert my + overloading todo something similar?

I'm not sure of the exact implementation, but I was saying for yours, add a member variable to each Str that is called length (it could be public, but it's probably better off private with an accessor). Use that to gauge how many character will be in the concatenated Str, and allocate the space accordingly.

That's what I did , I used (used) in order to show how many character are in use but I have a problem with the last part

Str Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;
    	
	char *temp=new char[strlen(data)+strlen(append.data)];	
	
	for(i=0,j=0; i<used;j++, i++ )
	
		
		temp[i]=data[j];
	
	for( i=used,j=0 ; i<append.used+used; j++,i++)
	
		temp[i]=append.data[i-used];
	
	temp[i]='\0';
	
	return Str(temp);                
}

because If my first string is s1=ABC\0G s2=DFG\0B the s1+s2 is ABCDFG it means it simply ignores the null character, How can I solve this problem tho see characters after null as null?

How do you input the string in the first place?

If you're ignoring nulls then don't use strlen. Step through the string and when you hit a null and you haven't hit the end of the array, then see what the next character is. I'm sure there's a more clever way to do it, but see if that works.

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.