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.

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 do not see the point of reserving memory for a string when you already know >how much space to save

First of all, your goal should be clear. Here is it:
You want to create a string class which will wrap the conventional char[] so that the length of the string can be change at run-time.

Getting me? I mean, you are basically writing a string class because C++ don't allow you to create variable length arrays. So the following statements are not valid:

int i;
std::cin>>i;
char myString[i]; //error, the length of string should be constant

To solve the above problem, you are actually designing the string class.

Now the next point to consider is, what help do the member functions Capacity and Length give.
At every point of time, the Capacity holds the size of the dynamically alloted memory(alloted with perhaps new) to the Mem, while the Length stores the actual length of the string.
These two, Capacity and Length often may have the same value.
So, there is a point of having Length and Capacity since you don't know(at the compile time) what would be the size of the char[]

How do you use them? Consider how the C++ string works. When you assign some content to the string, it allocated enough memory to hold that, plus usually a little extra. Length will tell you how many characters are assigned (like strlen( )), capacity tracks the amount of memory allocated. When you modify the string, if the new string content would be larger than capacity, you have to allocate a new block of memory that is larger. You'll know you need to do this when, say doing a concatenation, length of string plus length of added text exceeds capacity.

Notice that mem is a char*. You will have to allocate, at run time, at least as much memory as needed for the initial content assigned to the string. (Hint: allocate in some fixed block size, say 16 or 32 char increments.)

Also note that you don't need to worry about ending a string with a NULL terminator ( '\0' ) as you do when directly manipulating char[] strings. Length will tell you when to stop any functions upon your strings.

>Also note that you don't need to worry about ending a string with a NULL terminator ( '\0' )
Good! But writing that extra (1 Byte) null character will give him possibility to use the whole range of <cstring> library!
But then, it is not unreasonable to omit the '\0' and write each functions(strcat(), strcmp() etc) on your own.
Both have its own pros and cons.
If you are in learning phase( or otherwise), I still go with vmanes comment and won't suggest you to use <cstring> library.

I am still confused. So I have capacity say at 16 bits and a string that takes 22 bits. It seems like I need to know the amount of bits for each string.
How do you just allocate enough space?
Should i create an array of chars ( char[16] ) and then change each letter as it is put in by the user?

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.