Comp 272 Program 4 Spring 2004 Dordal Due: Wed, Apr 7 (although I won't count it late until Apr 12). For your last program, you are to build a simple string class, to be called "string2". This class should contain a copy ctor, ctors from (the existing class) string and (native C strings) char*, a destructor (dtor), ie string2(string2 &) string2(string) string2(char*) ~string2() and support for the following operations (all within the class string2 except for operator<<, to be defined outside): operator= operator== operator[] operator<<(ostream&, string2&) operator+=(string2 &) // append a string operator+=(char) // append a char The string2 class is intended to support frequent appending to the end of the string, *so* more space in general will be allocated than is actually needed. String2s will thus have a current size(), or the number of characters in the string, and also a maxsize() >= size() representing the size to which the string can grow without memory reallocation. Appending a single character to a string2 is similar to vector::push_back(), except that push_back() forces a reallocation every time (as we saw in class with the demo that printed every call to the copy ctor). Basically you are to follow the model of the T_array example, except that because of the desire to support appending without constant reallocation of memory, you should when creating a string **leave some extra space**. Thus, a string2 object will have the following private data: char* _data; int _currsize; int _maxsize; // amount actually allocated. When a string is first created, you should be sure that _maxsize is bigger than the size needed, say twice _currsize, or equal to _currsize+100, or the next highest power of 2. This allows some room for growth, so that adding one character doesn't always force reallocation. This is the only real difference between string2 and T_array (with T changed to "char"). A native C array of characters is type char[], identical to char* on the theory that an array is really a pointer to an array, and a pointer to a character is identified with a pointer to the start of an array (string) of characters. Thus, if you allocate space for 100 characters: _data = new char[100]; then you can access the ith character (starting at 0) as _data[i]. Native C strings assume one more thing: that the first byte *after* the end of the string is a 0 byte, to mark the end. With this 0 byte in place you can use the functions int strlen(char*) and strcpy() to find the length of a string and copy it. Or, you can just skip that and just handle the bytes directly. Demonstrate your program on some simple examples, showing += cases that both do and do not force reallocation. For diagnostic purposes, you should have your append (operator+=) print out a message when it does reallocate, and then demonstrate the reallocations involved in: string2 s; for (i=0; i<1000; i++) s+= 'x'; // build 1000-char string2 There should be some reallocation, because you don't want to allocate 1000 bytes for every string2, but there should be *much* less than 1000 reallocations. For example, if you Be sure to test the ctors too.