string class constructor

I have to construct a class for strings. C++ already has one, but my assignment is a home-made version. I am very lost as to how to make the constructor work. I have toyed with a few things, but I was given a header file to follow and I have no clue what to do. Any help would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class String
{
  private:

    unsigned Capacity;  // Number of memory locations reserved
    unsigned Length;    // Number of memory locations in use
    char * Mem;         // Pointer to memory to hold characters

  public:

    // Construct empty string
    //
    String()
    {
      Mem = NULL;
      Capacity = 0;
      Length = 0;
    }


A few specific questions I have:

How are the Mem, Capacity, and Length relavent? After reading the assignment but before looking at the header file I thought i was going to construct a string by taking the sizeof() of the string then applying an array of char[].

I do not see the point of reserving memory for a string when you already know how much space to save...In simpler words I see no point in having a Capacity and a length?


Please help I am in desperate need of help on my c++ classes.
I assume this should work after you finished your implementation:

1
2
3
String a = String("first string") + String("second string")
a = a ; // handle self assignment
cout << a  ; // prints "first stringsecond string" 


Get this code to compile and you're probably done!

To earn some extra credits, get this to compile and show the correct result:

1
2
3
4
if (a == String("first stringsecond string"))
    cout << "concatenation successful!";
else
    cout << "there is stil a but in it..." ; 
Your string class is to work with any string given to it by the user.

For example, I should be able to do:

1
2
3
String s;  // string is zero-length
String hw( "Hello World!" );  // string has length 13 including the \0
String longStr( "fhalkfhsdakfhaklfhaslfajfhalfhdsafsafas" ); // etc, etc. 


Your string class has to be able to work with any of those. Having said that, how does your string class know at compile time the length of the string being given to it? Answer: it doesn't. At runtime you have to allocate enough memory to hold the string being given to it.

That's what Length is for.

Capacity works as follows: it is typical for STL containers (such as vector<>, deque<>, and string) to allocate space for more elements/characters than is needed. Why? So that when the user adds elements to the vector/deque in a loop, the data structure does not have to be resized one larger each time through. Same for strings. Capacity therefore is a measure of the number of elements/bytes actually allocated.

So for example,

String hw( "Hello World!" );

has Length = 13, but the String class might allocate 20 bytes instead, and just not use the last 7. then when the programmer does:

hw += "Hi!";

the string already has enough Capacity to store the additional 3 characters without having to reallocate a larger Mem block.

So assume I allocate memory for 20 characters. What if the user inputs a string that is longer than that. Allocating memory before seems like it would waste a lot of space. I have to allocate enough memory to hold all of the characters when all of the characters are unknown?
You need a couple of constructors.

The default one, as above, should not allocate any memory.

A second constructor should take a const char* as parameter and allocate
strlen( str ) + 1 characters at least.

Topic archived. No new replies allowed.