Append function in a class.

I am working on a project and am having problems with the append function. I am not sure how to finish the implementation. Let's hope i get the code tags right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//////////////////////////////////////
//
//  Function    :  append
//
//  Description :  Concatenates str onto the tail end of *this
//
//  Pre         :  *this and str are correctly formed MyString objects
//  
//  Post        :  The concatenation is complete and length is updated
//
/////////////////////////////////////

void MyString::append (const MyString& str)
{
assert ( 0 <= length && length <= CAPACITY
          &&
         0 <= str.length && str.length <= CAPACITY);

   //finish the function implementation
   //NOTE:  The length of the concatenation cannot exceed CAPACITY
Last edited on
It's not possible to answer this question unless you provide more details about your MyString class. How can we help you with an implementation of append without knowing how the string class is represented, or even what you want to do with it?

Unless you're doing this as a learning project, and want to find you how to implement your own string class, I recommend using std::string instead.
While I can't give you a complete solution, I can give you a little tip:

Since "this" is a MyString, and "str" also is a MyString, you should assume that they are both valid. You shouldn't need to check the length. However, you should check that the resulting length is valid:

1
2
3
void MyString::append(const MyString& str) {
   assert(length + str.length <= CAPACITY);
   // ... 

Although I wouldn't use assert at all, but handle this error in a better way. Coming to think about it, I don't think I would have a maximum capacity at all.
Last edited on
This actually is a project where we are supposed to implement and test our own class. My professor never really explained the project to us, so I really am not sure what it is I am doing with it. The webpage is

http://www.engr.siu.edu/grad2/flanigan/web/projects_B/projects.htm

and it is Project 2, this function is the sixth in the mystring.cpp file, and it is tested in the 4th test in the driver.cpp file.
It is a project where I was initially given a maximum capacity of 256.
So you're actually just posting your homework without trying to figure it out for yourself.

Have a look here:
http://cplusplus.com/forum/articles/1295/
No, it is the one function out of the program that was never covered in the class, and I have already done the rest of the project except for this one. I have looked through the textbook numerous times, but all that I can find on concatenations are examples of things like string1 + string 2, and I have no idea how this applies to this particular case. I am looking for help to understand this function.
closed account (z05DSL3A)
As I don't know anything about the internal of you class, you could try some code similar to this:
1
2
3
4
    //assuming MyString has some sort of char[], I'll call s
    int pos = this.length;
    for ( int i = 0; i < str.length; i++ )
        s[pos + i] = str[i];


you would also need to check that s.length+ str.length would not be larger that your limit.
Last edited on
Topic archived. No new replies allowed.