$29
Program Note: For this assignment the normal C++ string and cstring functions can not be used (except >> and << with cstrings). You may not #include them. You must write all the functions that will be used.
Classes, Objects, Pointers and Dynamic Memory
Program Description: This assignment you will need to create your own string class. For the
name of the class, use your initials from your name. For example: my string class would be called LHString. Since each of you will be using a unique class name, I will use the name MYSting as the generic name of the class (but make sure for you program, that you use the correct name based on your name).
The MYString objects will hold a cstring and allow it to be used and changed. We will be changing this class over the next couple programs, to be adding more features to it (and correcting some problems that the program has in this simple version).
Below will be the details of the class. Since this is your first class that you will be writing, I would recommend that you write and test this class a few member functions at a time. For example you may want to start by writing the two constructors and the at function and then test them with some simple code in main like this:
MYString testStr("hello");
cout << "Testing testStr.at() function\n"; // testStr.at(0) should return 'h'
for(int i = -1; i < 10; i++ ){ // purposely testing out of bounds
cout << i << ":" << testStr.at(i) << ' '
<< static_cast<int>(testStr.at(i)) << endl;
}
cout << testStr.c_str() << endl;
cout << endl << endl;
Your first job will be to create and test the MYString class. As you write each member function, you should then write some code in main to test that the member function works well. Notice in the above example I was purposely using indexes outside of the bounds of the string to check if this would cause problems.
Your MYString class needs to be written using the .h and .cpp format.
Inside the class we will have the following data members:
|
|
|
|
|
|
Member Data |
|
Description |
|
|
|
|
|
|
|
char * str |
|
pointer to dynamic memory for storing the string |
|
|
|
|
|
|
|
|
|
size of the memory that is available to be used |
|
|
int cap |
|
(start with 20 char's and then double it whenever this is not |
|
|
|
|
enough) |
|
|
|
|
|
|
|
int end |
|
index of the end of the string (the '\0' char) |
|
|
|
|
|
|
The class will store the string in dynamic memory that is pointed to with the pointer. When you first create an MYString object you should allocate 20 spaces of memory (using the new command). The string will be stored as a cstring in this memory. {NOTE: Here on Canvas in Files\Review Lectures from 131\ there is a pdf of the pointer and cstring lecture slides from CS 131 to use as a review}.
You need to be allow your string that is held in the class to be able to be larger than 20 chars. It should start with a capacity of 20, but when needed it would grow in increments of 20. The capacity should always be a multiple of 20.
For example if we were storing the string "cat" in a MYString object, our data member would have the following values:
starting addr
str of dynamic
array
cap 20
end 3
Dynamic array:
c
a
t
\0
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
The MYString class will need to have the following member functions:
Programming Note: Write and test one or two functions at a time
indexed vector<MYString>. (do not use vector's push_back function. See programming note below*). Example: while ( words[count].read(fin) ) {...}
*Programming Note: Do not use the push_back() member function of vector, because this won't work for this program (it calls the copy constructor of our MYString class, which we haven't written).
Turn in: For turn in, you will have three files: your main program, the .h file and the .cpp file. For all programs which include class definitions, I want you to place them in that order (main, interface/header (.h), and implementation (.cpp) )
In addition to the program header documentation (above main), you should also have class documentation (and author info: name, Sect #, and explanation of the class ) at the top of the .h file.
Ways to lose points:
Comments: Comments are a way of documenting a program (explaining who did what and how). All programs for the rest of the course are required to have the following program header documentation (above main, and in any interface files (.h) ) and inline documentation to explain any tricky pieces of code.
////
////
#include <…>
.......the rest of the program