C++,。
Requirement
In this assignment, you will need to write a C++ string class library. This class does not need to exhibit all of the features of the C++ standard library string class, but must support the following features:
- Constructing a string from an const array of characters (e.g. a string literal)
- Constructing a string as a copy of another string
- A method length() which must return the number of characters in the string
- Indexed element access operator (square-brackets access, like mystring[0]), which must support both read and write capabilities
- Support for using the plus operator (+) as a concatenation operation, which will create a new string that combines the characters from the two operands.
- A destructor that will responsibly free any memory held by the object before the object itself is discarded.
Your class should be written in a file called mystring.h (basic starter template here), and your implementation should be written in a file called mystring.cpp.
To test your class, use the provided test library. With the test file and the header file all in the same directory, compile the test file and your implementation file into an executable program (remember to add the -lgtest -lpthread flags.)
Begin by writing all of your function declarations in the header file. You can check that the declarations are at least plausible for the intended use cases by compiling the test.cpp file. You can use the -c compilation flag to avoid errors related to the missing function implementations - it will create a test.o object file if the declarations are compatible with the use cases in the tests. Once the declarations are sound, implement the functions in your .cpp file and test them. Recall that you can get a list of the tests by giving the command-line argument “–gtest_list_tests” to the test program, and then choose to only run individual tests by giving it the argument –gtest_filter with the name of one test, e.g.
quot;./a.out --gtest-filter=constructorquot;
If you encounter segmentation faults, or when your tests seem to pass and you want to check for memory leaks, you can run the tests under the program “valgrind” as we did in class. It can give you additional information about where improper memory accesses occurred in your program, or whether your code failed to free any memory it allocated during the tests.
In addition to the functionality described above, your library must follow good software development practices so far as you have learned them. This means that your code should be well structured, well indented, and well-commented. In particular, each function you write should have a comment describing why the function has the parameters and return value that it does. 20% of your grade will be determined by your code organization and comments.
In your work, you may (but are not required to) use the standard library helper functions for C-style strings (null-character-terminated arrays of characters.) This library contains functions for counting characters and copying characters from one array to another, if you would find these helpful. You can access them by including lt;cstringgt;
.
Make sure that you are following the academic honesty policies regarding programs.
Submitting your work
Make sure your files are named correctly as specified above. In your comments at the top, make sure you:
- acknowledge any help or outside sources, in accordance with the academic honesty policy;
- describe any known bugs in the program.
Submit both mystring.cpp and mystring.h using the online turnin form. Be sure to select Homework 2.
Tips and tricks
If you’re getting too many errors when trying to compile the test file, comment out most of the test cases so that you only see the errors from the first test case. Once you get those compile errors fixed, uncomment the next test case and repeat until the whole file passes.
Keep the predeclared functions where they are, in the private section. You do not need to implement operator= or the default constructor, for instance; in fact you shouldn’t.
If you had downloaded the test file over the weekend, get a new copy - one or two small errors were corrected based on student feedback.
Don’t use “using namespace std;” in the header file. Otherwise, the test file which is using our class’ namespace will not be able to tell the difference between our strings and standard library strings.