C++ Programming Homework 3 Write a String class which will be a wrapper class to the C style. strings. The strings will be of varying logical lengths, but will have a fixed physical (maximum) length of MAXLEN (defined to be 128 characters). (30 points) Your String class must implement all the appropriate methods including constructors, assignment, equality operators, the index operator [], reverse, indexOf (find), print, and read. These public methods should check MAXLEN to see if the physical capacity has been exceeded. If so, print an error message and do something reasonable to recover so the program can continue running. (30 points) Do not use any of the C str functions (e.g. strcmp, strlen, strcat, or strcpy), however write them yourself, as static methods, then use your static methods. REMEMBER: never copy/paste code from any source - you must write everything yourself. These str functions should not check MAXLEN and we will reuse them in HW4. File organization: Put the full definition of class String in a file named String.h. Put your main program and testing functions in a file named string_test.cpp. string_test.cpp must #include String.h. You can compile your program with the comand g++ string_test.cpp -o string_test . You can run it by calling string_test Class String declaration: #define MAXLEN 128 class String { public: /// Both constructors should construct this String from the parameter s explicit String( const char * s = &"&"); String( const String s ); String perator = ( const String s ); char operator [] ( int index ); int size(); String reverse(); // does not modify this String int indexOf( const char c ); int indexOf( const String pattern ); bool perator == ( const String s ); bool operator != ( const String s ); bool operator > ( const String s ); bool perator = ( const String s ); /// concatenates this and s to return result String operator + ( const String s ); /// concatenates s onto end of this string String operator += ( const String s ); void print( ostream out ); void read( istream in ); ~String(); private: bool inBounds( int i ) { return i >= 0 i > ( istream in, String str ); (20 points) Write a main function, in a file called test_string.cpp, which tests each public method defined in your class String. Give at least two and at most 4 tests for each method. Good organization would be something like the following: void test_constructors_and_print() { String s(“Hello World”); // this calls the regular constructor cout firstString) = firstString) > firstString; cout fourthString) = fifthString) return 0; } First, read this carefully. If you don’t understand something, read it again, but change your focus so you are paying attention. Follow each link and read the relevant part of what is linked. You should think how you are going to get from an empty class definition to one that is fully functional. The foolish approach is to code it all up, then start debugging. You don’t have a working program until it is nearly complete. The best approach is a combination of “test driven development” and &"stepwise refinement&" AKA “top-down programming” where you identify the minimum functionality you need to test, then write a test case for that, then implement the funct代写留学生C/C++程序、C/C++实验代做、代写留学生C/C++课程设计、C/C++编程作业代做ionality until the test performs correctly. Then you identify the next feature to add, write the test, implement the feature, test, etc. Here is an abstract example. Note, in practice, we often use a combination of bottomup and top-down programming. In this assignment, we start bottom-up by identifying some useful operations: strcpy(), strlen(), strcmp(), etc. Then we move to top-down and implement each String method, but keep in mind the operations we defined and wrote bottom-up. E.g., use strcpy() to write the constructors and use strcmp() to write the relational operators.. For example, if you are writing class String, to write the simlest test program, you need functioning constructors and a functioning print method. First write a simple main function that declares a String and initializes it via the constructor, then print out the string to see if it was constructed correctly. Now go write both constructors and print and operator To write the constructors, you need the utility function, strcpy(), which is defined as a static method (static means it has no this parameter, but is still a member function so it can access private parts of strings). Here is a description of how strcpy should behave with examples of use http://www.cplusplus.com/reference/cstring/strcpy/ Test your program until it is functioning correctly. Note, you may also need the copy constructor if any strings are passed as parameter by copy or returned by copy (as opposed to by reference). The copy constructor for a class that takes one parameter that is a reference to an instance of the class. It may or may not be const as well. Constructors are responsible for building this string to look just like the c-string or string object they are being constructed from. strcpy() should do the work. Both constructors should call strcpy to copy the characters from the parameter into this buf. Next, you can think about writing perator = or perator ==, but whatever you decide to write next, first add the test case to your main function, then implement the new function and test it. The relationals and equality operators can use strcmp() to do the hard work. You can read about any of these functions via google because they are standard C functions, but we are writing them ourselves to use in implementing our string class. One more major suggestion, do not call strlen() unless absolutely necessary. It is expensive, O(N), and is not the way to iterate through a c-string with for loops. I showed you how to iterate through c-strings using the test for the null terminating character. That is the correct and efficient way to do it. Note it is difficult to write reverse() without first calling strlen(). Another thing to avoid is calling strlen in the terminating condition of a loop, e.g., void f(char *s) { for (int i=0; i do something with(s[i]); } Note if N is the number of charaacters in the string s, this has just turned this functin f into an O(N^2) function! You can easily convert it back to O(N) by factoring out the call to strlen() as follows: void f(char *s) { int len = strlen(s); for (int i=0; i do something with(s[i]); } but this is still making two passes down the string s. An even better solution is to not call strlen() at all and use the null string terminator to end the loop. void f(char *s) { for (int i=0; s[i] != ‘\0’; ++i) do something with(s[i]); } & 转自:http://ass.3daixie.com/2018060328883111.html