Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a string class. To avoid conflicts with other similarly named classes, we will call our version myString. This object is designed to make working

Write a string class. To avoid conflicts with other similarly named classes, we will call our version myString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The myString class will handle constructing strings, reading/printing, and accessing characters. In addition, the myString object will have the ability to make a full deep-copy of itself when copied. Here is a list of the operations this class must support: -A length member function which returns the number of characters in the string. Use strlen(). -Construction of a myString from a const c-string. You should copy the string data, not just store a pointer to an argument passed to the constructor. Constructing a myString with no arguments creates an empty myString object (i.e. ""). A myString object should be implemented efficiently (space-wise) which is to say you should not have a fixed-size buffer of chars, but instead allocate space for chars on an as-needed basis. Use strcpy(). -Printing a myString to a stream using an overloaded << (insertion) operator, which should simply print out its characters. Use <<. -Your myString object should overload the square brackets [ ] operator to allow direct access to the individual characters of the string. This operation should range-check and assert if the index is out of bounds. You will write two versions of the [ ] operator, a const version that allows read access to the chars, and a non-const version that returns the client a reference to the char so they can change the value. -All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare myString objects to other myStrings as well as myStrings to c-strings. The ordering will be based on ASCII values. You can think of this as essentially alphabetical order; however, because of the way that ASCII values are defined, uppercase letters will always come before lowercase letters, and punctuation will make things even more complicated. Confused? You don't need to worry about any of this: just use the results of calling the strcmp() function. myStrings or c-strings should be able to appear on either side of the comparison operator. -Of course, you'll also need to include the four functions discussed in the lesson that are always required in classes that use dynamic memory. (These functions, excluding the default constructor, are otherwise known as the "big-three".) You may use all of the c-string functionality provided by C++. This will include the strlen(), strcmp(), and strcpy() functions, along with the overloaded insertion operator for c-strings. These functions are all covered in detail in the text. When you use strcpy() treat it as a void function despite the fact that it has a return value. Do not use strncpy(), strncat(), or strncmp() since they are not implemented in all versions of C++. You may NOT use anything from the C++ string class!! You must place your header file and implementation file in a namespace. Normally one would call a namespace something more likely to be unique, but for purposes of convenience we will call our namespace "cs_mystring". Here is the client program: /* * These functions are designed to help you test your myString objects, * as well as show the client usage of the class. * * The BasicTest function builds an array of strings using various * constructor options and prints them out. It also uses the String * stream operations to read some strings from a data file. * * The RelationTest function checks out the basic relational operations * (==, !=, <, etc) on Strings and char *s. * * * The CopyTest tries out the copy constructor and assignment operators * to make sure they do a true deep copy. * * Although not exhaustive, these tests will help you to exercise the basic * functionality of the class and show you how a client might use it. * * While you are developing your myString class, you might find it * easier to comment out functions you are ready for, so that you don't * get lots of compile/link complaints. */ #include "mystring.h" #include // for toupper() #include #include using namespace std; using namespace cs_mystring; void BasicTest(); void RelationTest(); void CopyTest(); myString AppendTest(const myString& ref, myString val); string boolString(bool convertMe); int main() { BasicTest(); RelationTest(); CopyTest(); } Here is the correct output: ----- Testing basic String creation & printing string [0] = Wow string [1] = C++ is neat! string [2] = string [3] = a-z ----- Testing access to characters (using const) Whole string is abcdefghijklmnopqsrtuvwxyz now char by char: abcdefghijklmnopqsrtuvwxyz ----- Testing access to characters (using non-const) Start with abcdefghijklmnopqsrtuvwxyz and convert to ABCDEFGHIJKLMNOPQSRTUVWXYZ ----- Testing relational operators between myStrings Comparing app to apple Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing apple to Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing to Banana Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing Banana to Banana Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false ----- Testing relations between myStrings and char * Comparing he to hello Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing why to wackity Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true ----- Testing copy constructor and operator= on myStrings original is cake, copy is fake original is cake, copy is fake after self assignment, copy is Copy Cat Testing pass & return myStrings by value and ref after calling Append, sum is Binky val is winky after assign, val is Binky Expert Answer Anonymous Anonymous answered this Was this answer helpful? 0 0 797 answers main.cpp #include "mystring.h" #include #include // for toupper() #include #include #include using namespace std; using namespace cs_myString; bool eof(istream& in); void BasicTest(); void RelationTest(); void ConcatTest(); void CopyTest(); myString AppendTest(const myString& ref, myString val); string boolString(bool convertMe); int main() { BasicTest(); RelationTest(); ConcatTest(); CopyTest(); } bool eof(istream& in) { char ch; in >> ch; in.putback(ch); return !in; } string boolString(bool convertMe) { if (convertMe) { return "true"; } else { return "false"; } } void BasicTest() { myString s; cout << "----- Testing basic String creation & printing" << endl; const myString strs[] = {myString("Wow"), myString("C++ is neat!"), myString(""), myString("a-z")}; for (int i = 0; i < 4; i++){ cout << "string [" << i <<"] = " << strs[i] << endl; } cout << endl << "----- Now reading myStrings from file" << endl; cout << endl << "----- first, word by word" << endl; ifstream in("/Users/reenasing/Desktop/C++Assignments/a13_1/a13_1/mystring.txt"); assert(in); while (in.peek() == '#'){ in.ignore(128, ' '); } in >> s; while (in) { cout << "Read string = " << s << endl; in >> s; } in.close(); cout << endl << "----- now, line by line" << endl; ifstream in2("/Users/reenasing/Desktop/C++Assignments/a13_1/a13_1/mystring.txt"); assert(in2); while (in2.peek() == '#'){ in2.ignore(128, ' '); } s.read(in2, ' '); while (in2) { cout << "Read string = " << s << endl; s.read(in2, ' '); } cout << endl << "----- Testing access to characters (using const)" << endl; const myString s1("abcdefghijklmnopqsrtuvwxyz"); cout << "Whole string is " << s1 << endl; cout << "now char by char: "; for (int i = 0; i < s1.length(); i++){ cout << s1[i]; } cout << endl << "----- Testing access to characters (using non-const)" << endl; myString s2("abcdefghijklmnopqsrtuvwxyz"); cout << "Start with " << s2; for (int i = 0; i < s2.length(); i++){ s2[i] = toupper(s2[i]); } cout << " and convert to " << s2 << endl; } void RelationTest() { cout << " ----- Testing relational operators between myStrings "; const myString strs[] = {myString("app"), myString("apple"), myString(""), myString("Banana"), myString("Banana")}; for (int i = 0; i < 4; i++) { cout << "Comparing " << strs[i] << " to " << strs[i+1] << endl; cout << " Is left < right? " << boolString(strs[i] < strs[i+1]) << endl; cout << " Is left <= right? " << boolString(strs[i] <= strs[i+1]) << endl; cout << " Is left > right? " << boolString(strs[i] > strs[i+1]) << endl; cout << " Is left >= right? " << boolString(strs[i] >= strs[i+1]) << endl; cout << " Does left == right? " << boolString(strs[i] == strs[i+1]) << endl; cout << " Does left != right ? " << boolString(strs[i] != strs[i+1]) << endl; } cout << " ----- Testing relations between myStrings and char * "; myString s("he"); const char *t = "hello"; cout << "Comparing " << s << " to " << t << endl; cout << " Is left < right? " << boolString(s < t) << endl; cout << " Is left <= right? " << boolString(s <= t) << endl; cout << " Is left > right? " << boolString(s > t) << endl; cout << " Is left >= right? " << boolString(s >= t) << endl; cout << " Does left == right? " << boolString(s == t) << endl; cout << " Does left != right ? " << boolString(s != t) << endl; myString u("wackity"); const char *v = "why"; cout << "Comparing " << v << " to " << u << endl; cout << " Is left < right? " << boolString(v < u) << endl; cout << " Is left <= right? " << boolString(v <= u) << endl; cout << " Is left > right? " << boolString(v > u) << endl; cout << " Is left >= right? " << boolString(v >= u) << endl; cout << " Does left == right? " << boolString(v == u) << endl; cout << " Does left != right ? " << boolString(v != u) << endl; } void ConcatTest() { cout << " ----- Testing concatentation on myStrings "; const myString s[] = {myString("outrageous"), myString("milk"), myString(""), myString("cow"), myString("bell")}; for (int i = 0; i < 4; i++) { cout << s[i] << " + " << s[i+1] << " = " << s[i] + s[i+1] << endl; } cout << " ----- Testing concatentation between myString and char * "; const myString a("abcde"); const char *b = "XYZ"; cout << a << " + " << b << " = " << a + b << endl; cout << b << " + " << a << " = " << b + a << endl; cout << " ----- Testing shorthand concat/assign on myStrings "; myString s2[] = {myString("who"), myString("what"), myString("WHEN"), myString("Where"), myString("why")}; for (int i = 0; i < 4; i++) { cout << s2[i] << " += " << s2[i+1] << " = "; cout << (s2[i] += s2[i+1]) << "and"; cout << s2[i] << endl; } cout << " ----- Testing shorthand concat/assign using char * "; myString u("I love "); const char *v = "programming"; cout << u << " += " << v << " = "; cout << (u += v) << endl; } myString AppendTest(const myString& ref, myString val) { val[0] = 'B'; return val + ref; } void CopyTest() { cout << " ----- Testing copy constructor and operator= on myStrings "; myString orig("cake"); myString copy(orig); // invoke copy constructor copy[0] = 'f'; // change first letter of the *copy* cout << "original is " << orig << ", copy is " << copy << endl; myString copy2; // makes an empty string copy2 = orig; // invoke operator= copy2[0] = 'f'; // change first letter of the *copy* cout << "original is " << orig << ", copy is " << copy2 << endl; copy2 = "Copy Cat"; copy2 = copy2; // copy onto self and see what happens cout << "after self assignment, copy is " << copy2 << endl; cout << "Testing pass & return myStrings by value and ref" << endl; myString val = "winky"; myString sum = AppendTest("Boo", val); cout << "after calling Append, sum is " << sum << endl; cout << "val is " << val << endl; val = sum; cout << "after assign, val is " << val << endl; } myString.cpp #include #include #include "myString.h" using namespace std; namespace cs_myString { /*Default construtor takes no parameter : initialize desc with memory of size 1 and copy empty string to it.*/ myString::myString() { desc = new char[1]; strcpy(desc, ""); } /*The parameterized constructor takes pointer argument pointing to the object. First create space to copy object to at 'desc' a private member data. and then copy to destination. The memory created is equal to object length + 1.*/ myString::myString(const char *inDesc) { desc = new char[strlen(inDesc) + 1]; strcpy(desc, inDesc); } /*The parameterized construtor takes one reference parameter the address to the object location. First create space to copy object to at 'desc' a private member data. and then copy to destination. The memory created is equal to object length + 1.*/ myString::myString(const myString& right) { desc = new char[strlen(right.desc) + 1]; strcpy(desc, right.desc); } //Destructor is written to destroy object of whatever length myString::~myString() { delete [] desc; } /* The overloaded operator = , takes one argument reference to object and is kept const. first we free memory of private data member desc, then assign memory of appropriate length according to size of object reference. Later copy object of specified length to private data member and return pointer to object. */ myString myString::operator=(const myString& right) { if (this != &right) { delete [] desc; desc = new char[strlen(right.desc) + 1]; strcpy(desc, right.desc); } return *this; } /*Overloaded operator == function contains addresses of two valid myString:- left and right. strcmp function compares. It return's true, on when left.desc and right.desc == 0*/ bool operator==(const myString& left, const myString& right) { return strcmp(left.desc, right.desc) == 0; } /*Overloaded operator < function contains addresses of two valid myString:- left and right. strcmp function compares. It return's true, on when left.desc and right.desc < 0*/ bool operator<(const myString& left, const myString& right) { return strcmp(left.desc, right.desc) < 0; } /*Overloaded operator != function contains addresses of two valid myString:- left and right. It return's true, on when left is not equal right */ bool operator!=(const myString& left, const myString& right) { return !(left == right); } /*Overloaded operator > function contains addresses of two valid myString:- left and right. It return's true, on when left is not equal right and is not less than right*/ bool operator>(const myString& left, const myString& right) { return !(left == right) && !(left < right); } /*Overloaded operator <= function contains addresses of two valid myString:- left and right. It return's true, on when left is less than right or equals to right*/ bool operator<=(const myString& left, const myString& right) { return left < right || left == right; } /*Overloaded operator >= function contains addresses of two valid myString:- left and right. It return's true, on when left is not less than right*/ bool operator>=(const myString& left, const myString& right) { return !(left < right); } // return length of string in int int myString::length() const { return (int) strlen(desc); } /*Overloaded operator << function takes computed address of source and is passed to out object of ostream to display results.*/ ostream& operator<<(ostream& out, const myString& source) { out << source.desc; return out; } /* Overloading The Square Brackets returns a const char for the index returned. Its operator takes index and use assert to check whether the index falls is >= 0 and index is < string length. If it does then return the index to myString specifying the char to that index.*/ char myString::operator[](int index) const { assert(index >= 0 && index < strlen(desc)); return desc[index]; } /* Overloading The Square Brackets returns an reference to char at that index. Its operator takes index and use assert to check whether the index falls is >= 0 and index is < string length. If it does then return the index to myString.*/ char& myString::operator[](int index) { assert(index >= 0 && index < strlen(desc)); return desc[index]; } /* Overloaded Extraction >> operator function takes address of source. A limit of 127 on the number of characters this function will read.We temporarily read into a non-dynamic array and then copy it into your data member, which will be a dynamic array. */ istream& operator>>(istream& in, myString &source) { char temp[127]; delete [] source.desc; in >> temp; source.desc = new char[strlen(temp) + 1 ]; strcpy(source.desc, temp); return in; } /* The read() function returns void and takes two arguments, a stream and the delimiting character. It does not skip leading spaces. The limit of 127 characters imposed on the >> function above also applies to this function. The function :-reads character by character in a loop. Use the in.getline() function to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member. */ void myString::read(istream &source, char delim) { char temp[127]; delete [] desc; source.getline(temp,127,delim); desc = new char[strlen(temp) + 1 ]; strcpy(desc,temp); } /*Overloaded operator + function, takes two parameter which is reference to myString 'left' & 'right' and is declared const. Overload the + operator does myString concatenation. The operator able to handle either myString objects or C-strings on either side of the operator. The memory is allocated to hold the new myString. strcpy() gets the left operand into the result myString, and then strcat() to append the right operand. Both strcpy() and strcat() is used as if they are void, even though they do have return values. */ const myString operator+(const myString &left, const myString &right) { myString temp; temp = new char[strlen(left.desc) + strlen(right.desc) + 1]; strcpy(temp.desc, left.desc); strcat(temp.desc, right.desc); return temp; } /* Overloaded the shorthand += to combine concatenation and assignment. The argument to overloaded += operator is reference to right myString object. Dynamic allocate memory to hold size of the new myString and strcat() to append the right operand. */ myString myString::operator+=(const myString& right) { myString temp; temp = new char[strlen(right.desc) + 1]; delete [] desc; desc = new char[strlen(temp.desc) + strlen(right.desc) + 1]; strcat(desc, right.desc); return *this; } /* Overloaded the shorthand += to combine concatenation and assignment. The argument to overloaded += operator is pointer to right char object. Dynamic allocate memory to hold the size of the new myString and strcat() to append the right operand. */ myString myString::operator+=(const char *right) { myString temp; temp = new char[strlen(right) + 1]; delete [] desc; desc = new char[strlen(temp.desc) + strlen(right) + 1]; strcat(desc, right); return *this; } } myString.h #ifndef myString_H #define myString_H #include using namespace std; namespace cs_myString { class myString { public: //DEFAULT CONSTRUCTOR myString(); //Preconditon: Takes no parameter and return nothing..not even void //Postcondition: initialize data member-'desc' with memory of size 1 and copy empty string to it //CONSTRUCTOR myString(const char *inDesc); //Preconditon: Takes one argument - pointer to char object 'inDesc' and is kept const //Postcondition: object is created with size accordingly with specified object pointer given at input //COPY CONSTRUCTOR myString(const myString& right); //Preconditon: Takes one argument - reference to myString object 'right' and is kept const //Postcondition: object is created with size accordingly with specified object reference given at input //DESTRUCTOR ~myString(); //Preconditon: an myString object exist with valid length //Postcondition: myString object is destroyed //ASSIGNMENT OPERATOR myString operator=(const myString& right); //Preconditon: Input argument contains address to myString object:-Right. //Postcondition: object is copied with size accordingly with specified object reference given at input // CONCAT/ASSIGNMENT OPERATOR myString operator+=(const myString& right); //Preconditon: Input argument contains address to myString object:-Right. //Postcondition: returns pointer to this object which was created by combination of concatenation and assignment operator myString operator+=(const char *right); //Preconditon: Input argument contains pointer to char object:-Right. //Postcondition: returns pointer to this char object which was created by combination of concatenation and assignment operator int length() const; //Preconditon: length donot take any argument //Postcondition: returns length of myString object // OVERLOADING THE EXTRACTION OPERATOR friend ostream& operator<<(ostream& out, const myString& source); //Precondition: The out of ostream object is waiting to receive the myString output. //Postcondition: myString source has been passed by reference to the ostream out and is kept const. //OVERLOADED INSERTION OPERATOR friend istream& operator>>(istream& in, myString &source); //Precondition: The in of istream object is waiting to receive the myString input. //Postcondition: myString source has been passed by reference into the istream 'in'. //OVERLOADED All SIX OF THE RELATIONAL OPERATOR (<, <=, >, >=, ==, !=) friend bool operator<(const myString &Left, const myString &Right); //Precondition:- Input argument contains address to two myString object:- Left and Right. //Postcondition: function return's true, if Left < Right, else it return's false. friend bool operator<=(const myString &Left, const myString &Right); //Precondition:- Input parameters contains reference to two myString object:- Left and Right. //Postcondition: function return's true, if Left <= Right, else it return's false. friend bool operator==(const myString &Left, const myString &Right); //Precondition:- Input parameters contains reference to two myString object:- Left and Right. //Postcondition: function return's true, if Left == Right, else it return's false. friend bool operator!=(const myString &Left, const myString &Right); //Precondition:- Input parameters contains reference to two myString object:- Left and Right. //Postcondition: function return's true, if Left != Right, else it return's false. friend bool operator>=(const myString &Left, const myString &Right); //Precondition:- Input parameters contains reference to two myString object:- Left and Right. //Postcondition: function return's true, if Left >= Right, else it return's false. friend bool operator>(const myString &Left, const myString &Right); //Precondition:- Input parameters contains reference to two myString object:- Left and Right. //Postcondition: function return's true, if Left > Right, else it return's false. //READ FROM FILE void read(istream& inString,char delim); //Preconditon: Input argument contains two argument, a stream and the delimiting character. //Postcondition: Reads character by character in a loop. Use the in.getline() function to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member. return void. //CONCAT OPERATOR friend const myString operator+(const myString &left, const myString &right); //Precondition:- Input parameters contains reference to two myString object:- Left and Right. //Postcondition: function return's addition of two myString objects. // OVERLOADING THE SQUARE BRACKETS char operator[](int index) const; //Preconditon: Input argument contains value of index. //Postcondition: Returns the index to myString specifying the char to that location which is const. char& operator[](int index); //Preconditon: Input argument contains value of index. //Postcondition: Returns the index to myString specifying the char to that location. private: char *desc; //POINTER DATA MEMBER }; } #endif sample output ----- Testing basic String creation & printing string [0] = Wow string [1] = C++ is neat! string [2] = string [3] = a-z ----- Now reading myStrings from file ----- first, word by word Read string = The Read string = first Read string = time Read string = we Read string = will Read string = read Read string = individual Read string = words, Read string = next Read string = we Read string = read Read string = whole Read string = lines ----- now, line by line Read string = The first time we will Read string = read individual words, next Read string = we read whole lines ----- Testing access to characters (using const) Whole string is abcdefghijklmnopqsrtuvwxyz now char by char: abcdefghijklmnopqsrtuvwxyz ----- Testing access to characters (using non-const) Start with abcdefghijklmnopqsrtuvwxyz and convert to ABCDEFGHIJKLMNOPQSRTUVWXYZ ----- Testing relational operators between myStrings Comparing app to apple Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing apple to Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing to Banana Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing Banana to Banana Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false ----- Testing relations between myStrings and char * Comparing he to hello Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing why to wackity Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true ----- Testing concatentation on myStrings outrageous + milk = outrageousmilk milk + = milk + cow = cow cow + bell = cowbell ----- Testing concatentation between myString and char * abcde + XYZ = abcdeXYZ XYZ + abcde = XYZabcde ----- Testing shorthand concat/assign on myStrings who += what = whowhatandwhowhat what += WHEN = whatWHENandwhatWHEN WHEN += Where = WHENWhereandWHENWhere Where += why = WherewhyandWherewhy ----- Testing shorthand concat/assign using char * I love += programming = I love programming ----- Testing copy constructor and operator= on myStrings original is cake, copy is fake original is cake, copy is fake after self assignment, copy is Copy Cat Testing pass & return myStrings by value and ref after calling Append, sum is BinkyBoo val is winky after assign, val is BinkyBoo

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions