Write a string class. To avoid conflicts with other similarly named classes, we
ID: 3602754 • Letter: W
Question
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.
Your class must have only one data member, a c-string implemented as a dynamic array. In particular, you must not use a data member to keep track of the size or length of the MyString.
This is the first part of a two part assignment. In the next assignment you will be making some refinements to the class that you create in this assignment. For example, no documentation is required this week, but full documentation will be required next week.
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!!
Unfortunately, Visual C++ will, under its default settings, report an error when you try to use strcpy() or strcat(), even though they are standard C++. You can prevent this by adding this line as the first line in your file:
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".
Explanation / Answer
Mystring.cpp:
#define MYSTRING_H
#ifndef MYSTRING_H
#include <iostream>
using namespace std;
class MyString {
public:
MyString(); //1st
MyString(cons char *inString); //2nd
MyString operator=(cons MyString right); //3rd
MyString(cons MyString &right); //4th
~MyString(); //the destructor
int length()cons;
friend ostream& operator<<(ostream& out,cons MyString &outString);
friend istream& operator>>(istream& in,MyString &inString);
void read(istream& inString,char delimit);
char& operator[](int index);
char operator[](int index)cons;
friend MyString operator+(cons MyString left,cons MyString right);
MyString& operator+=( cons MyString &right);
friend bool operator<(cons MyString &left, cons MyString &right);
friend bool operator<=(cons MyString &left, cons MyString &right);
friend bool operator>=(cons MyString &left, cons MyString &right);
friend bool operator>(cons MyString &left, cons MyString &right);
friend bool operator==(cons MyString &left, cons MyString &right);
friend bool operator!=(cons MyString &left, cons MyString &right);
private:
char * string;
};
#endif
/*
* -------------------
* 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 ConcatTest functions checks the overloaded + and += operators that
* do string concatenation.
*
* 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 <cctype> // for toupper()
#include <fstream>
#include <cassert>
#include <string>
#include <iostream>
using namespace std;
bool eof(istream& in);
void BasicTest();
void RelationTest();
void ConcatTest();
void CopyTest();
MyString AppendTest(cons MyString& ref, MyString val);
int main()
{
BasicTest();
RelationTest();
ConcatTest();
CopyTest();
}
bool eof(istream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
void BasicTest()
{
cout << "----- Testing basic String creation & printing" << endl;
cons 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("string.data");
assert(in);
while (!eof(in)) {
MyString s; // this will creates an empty string
if (in.peek() == '#') { // peek at char, comments start with #
in.ignore(128, ' '); // To skip this line, it's a comment
} else {
in >> s;
cout << "Read string = " << s << endl;
}
}
in.close();
cout << endl << "----- now, line by line" << endl;
ifstream in2("mystring.txt");
assert(in2);
while (!eof(in2)) {
MyString s; // creates an empty string
if (in2.peek() == '#') { // peek at char, comments start with #
in2.ignore(128, ' '); // skip this line, it's a comment
} else {
s.read(in2, ' ');
cout << "Read string = " << s << endl;
}
}
in2.close();
cout << endl << "----- Testing access to characters (using cons)" <<
endl;
cons MyString s("abcdefghijklmnopqsrtuvwxyz");
cout << "Whole string is " << s << endl;
cout << "now char by char: ";
for (int i = 0; i < s.length(); i++){
cout << s[i];
}
cout << endl << "----- Testing access to characters (using non-cons)"
<< endl;
MyString s2("abcdefghijklmnopqsrtuvwxyz");
cout << "Start with " << s2;
for (int i = 0; i < s.length(); i++){
s2[i] = toupper(s2[i]);
}
cout << " and convert to " << s2 << endl;
}
void RelationTest()
{
cout << " ----- Testing relational operators between MyStrings ";
cons 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? " << (strs[i] < strs[i+1]) << endl;
cout << " Is left <= right? " << (strs[i] <= strs[i+1]) << endl;
cout << " Is left > right? " << (strs[i] > strs[i+1]) << endl;
cout << " Is left >= right? " << (strs[i] >= strs[i+1]) << endl;
cout << " Does left == right? " << (strs[i] == strs[i+1]) << endl;
cout << " Does left != right ? " << (strs[i] != strs[i+1]) << endl;
}
cout << " ----- Testing relations between MyStrings and char * ";
MyString s("he");
cons char *t = "hello";
cout << "Comparing " << s << " to " << t << endl;
cout << " Is left < right? " << (s < t) << endl;
cout << " Is left <= right? " << (s <= t) << endl;
cout << " Is left > right? " << (s > t) << endl;
cout << " Is left >= right? " << (s >= t) << endl;
cout << " Does left == right? " << (s == t) << endl;
cout << " Does left != right ? " << (s != t) << endl;
MyString u("wackity");
cons char *v = "why";
cout << "Comparing " << v << " to " << u << endl;
cout << " Is left < right? " << (v < u) << endl;
cout << " Is left <= right? " << (v <= u) << endl;
cout << " Is left > right? " << (v > u) << endl;
cout << " Is left >= right? " << (v >= u) << endl;
cout << " Does left == right? " << (v == u) << endl;
cout << " Does left != right ? " << (v != u) << endl;
}
void ConcatTest()
{
cout << " ----- Testing concatentation on MyStrings ";
cons 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 * ";
cons MyString a("abcde");
cons 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]) << endl;
}
cout << " ----- Testing shorthand concat/assign using char * ";
MyString u("I love ");
cons char *v = "programming";
cout << u << " += " << v << " = ";
cout << (u += v) << endl;
}
MyString AppendTest(cons 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); // This will be invoking 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::MyString()
{
string = new char[0];
string[0] = 0;
}
MyString::MyString(cons char *inString)
{
string = new char[strlen(inString)+1];
strcpy(string,inString);
}
MyString::~MyString()
{
delete []string;
}
ostream& operator<<(ostream &out,cons MyString &outString)
{
out<<outString.string;
return out;
}
MyString MyString:: operator=(cons MyString right)
{
if (this != &right)
{
delete []string;
string = new char[strlen(right.string)+1];
strcpy(string,right.string);
}
return *this;
}
MyString::MyString(cons MyString &right)
{
string = new char[strlen(right.string)+1];
strcpy(string,right.string);
}
char& MyString::operator[](int index)
{
assert(index>=0 && index < strlen(string));
return string[index];
}
char MyString::operator[](int index)cons
{
assert(index>=0 && index < strlen(string));
return string[index];
}
istream& operator>>(istream& in,MyString &inString)
{
char temp[128];
delete [] inString.string;
in>>temp;
strcpy(inString.string,temp);
return in;
}
void MyString::read(istream &inString,char delimit)
{
char temp[128];
delete []string;
inString.getline(temp,127,delimit);
strcpy(string,temp);
}
MyString operator+(cons MyString left,cons MyString right)
{
MyString temp;
//temporary mystring to hold contents of left.string+right.string
temp.string = new char[strlen(left.string)+strlen(right.string)+1];
//dynamic array size of left.string+right.string+1
strcat(left.string,right.string);
//To make left.string+right.string into left.string
strcpy(temp.string,left.string);
//To copy the contents of left.string into temp.string;
return temp;
//To return the copy of MyString temp
}
MyString& MyString :: operator+=(cons MyString &right)
{
*this = *this+right;
return *this;
}
int MyString::length()cons
{
return strlen(string);
}
bool operator<(cons MyString &left, cons MyString &right)
{
return strcmp(left.string,right.string) < 0;
}
bool operator<=(cons MyString &left, cons MyString &right)
{
return strcmp(left.string,right.string) <= 1;
}
bool operator>(cons MyString &left, cons MyString &right)
{
return strcmp(left.string,right.string) > 0;
}
bool operator>=(cons MyString &left, cons MyString &right)
{
return strcmp(left.string,right.string) >= 1;
}
bool operator==(cons MyString &left, cons MyString &right)
{
return strcmp(left.string,right.string) == 1;
}
bool operator!=(cons MyString &left, cons MyString &right)
{
return strcmp(left.string,right.string) != 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.