C++ Avoid unnecessary if statements. These can be replaced by returning the cond
ID: 3599904 • Letter: C
Question
C++
Avoid unnecessary if statements. These can be replaced by returning the condition evaluated.
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".
CLIENT PROGRAM:
REQUIRED OUTPUT:
Name your source code file(s) mystring.cpp and mystring.h. Execute the given client program and copy/paste the output into the bottom of the implementation file, making it into a comment.
Explanation / Answer
File: mystring.h
#pragma once
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
namespace cs2b_mystring
{
class myString
{
friend bool operator<(const char *charArr, const myString &obj);
friend bool operator<=(const char *charArr, const myString &obj);
friend bool operator>(const char *charArr, const myString &obj);
friend bool operator>=(const char *charArr, const myString &obj);
friend bool operator==(const char *charArr, const myString &obj);
friend bool operator!=(const char *charArr, const myString &obj);
friend ostream &operator<<(ostream &out, const myString &obj);
friend istream &operator>> (istream &out, myString &obj);
friend myString operator+(const myString obj1, const myString obj2);
public:
myString();
myString(const char *charArr);
myString(const myString &obj);
~myString();
void operator=(myString &obj);
void operator=(const char *charArr);
bool operator<(const myString &obj) const;
bool operator<=(const myString &obj) const;
bool operator>(const myString &obj) const;
bool operator>=(const myString &obj) const;
bool operator==(const myString &obj) const;
bool operator!=(const myString &obj) const;
char &operator [](int index);
const char &operator[](int index) const;
int length() const;
void read(istream& ins, char delimiter);
myString& operator+=(const myString &obj);
private:
char *strPtr;
};
}
#endif
File: mystring.cpp
#include "mystring.h"
using namespace cs2b_mystring;
myString::myString()
{
strPtr = new char[1];
strPtr[0] = '';
}
myString::myString(const char *charArr)
{
int len = strlen(charArr);
strPtr = new char[len + 1];
for (int i = 0; i < len; i++)
{
strPtr[i] = charArr[i];
}
strPtr[len] = '';
}
myString::myString(const myString &obj)
{
int len = strlen(obj.strPtr);
strPtr = new char[len + 1];
for (int i = 0; i < len; ++i)
{
strPtr[i] = obj.strPtr[i];
}
strPtr[len] = '';
}
myString::~myString()
{
delete[] strPtr;
}
void myString::operator=(myString &obj)
{
if (*this != obj)
{
int len = strlen(obj.strPtr);
strPtr = new char[len + 1];
for (int i = 0; i < len; ++i)
{
strPtr[i] = obj.strPtr[i];
}
strPtr[len] = '';
}
}
void myString::operator=(const char *charArr)
{
int len = strlen(charArr);
strPtr = new char[len + 1];
for (int i = 0; i < len; ++i)
{
strPtr[i] = charArr[i];
}
strPtr[len] = '';
}
bool myString::operator<(const myString &obj) const
{
return strcmp(strPtr, obj.strPtr) < 0;
}
bool myString::operator<=(const myString &obj) const
{
return strcmp(strPtr, obj.strPtr) <= 0;
}
bool myString::operator>(const myString &obj) const
{
return strcmp(strPtr, obj.strPtr) > 0;
}
bool myString::operator>=(const myString &obj) const
{
return strcmp(strPtr, obj.strPtr) >= 0;
}
bool myString::operator==(const myString &obj) const
{
return strcmp(strPtr, obj.strPtr) == 0;
}
bool myString::operator!=(const myString &obj) const
{
return strcmp(strPtr, obj.strPtr) != 0;
}
bool cs2b_mystring::operator<(const char *charArr, const myString &obj)
{
return strcmp(charArr, obj.strPtr) < 0;
}
bool cs2b_mystring::operator<=(const char *charArr, const myString &obj)
{
return strcmp(charArr, obj.strPtr) <= 0;
}
bool cs2b_mystring::operator>(const char *charArr, const myString &obj)
{
return strcmp(charArr, obj.strPtr) > 0;
}
bool cs2b_mystring::operator>=(const char *charArr, const myString &obj)
{
return strcmp(charArr, obj.strPtr) >= 0;
}
bool cs2b_mystring::operator==(const char *charArr, const myString &obj)
{
return strcmp(charArr, obj.strPtr) == 0;
}
bool cs2b_mystring::operator!=(const char *charArr, const myString &obj)
{
return strcmp(charArr, obj.strPtr) != 0;
}
ostream &cs2b_mystring::operator<<(ostream &out, const myString &obj)
{
out << obj.strPtr;
return out;
}
istream &cs2b_mystring::operator>>(istream &in, myString &obj)
{
in >> obj.strPtr;
return in;
}
char& myString::operator [](int i)
{
int len = strlen(strPtr);
assert(0 <= i && i < len);
return strPtr[i];
}
const char& myString::operator[](int i) const
{
int len = strlen(strPtr);
assert(0 <= i && i < len);
return strPtr[i];
}
int myString::length() const
{
return strlen(strPtr);
}
void myString::read(istream &ins, char delimiter)
{
char tempArr[128];
delete[] strPtr;
ins.getline(tempArr, 127, delimiter);
strcpy(strPtr, tempArr);
}
myString cs2b_mystring::operator+(const myString obj1, const myString obj2)
{
myString tempObj;
tempObj.strPtr = new char[strlen(obj1.strPtr) + strlen(obj2.strPtr) + 1];
strcat(obj1.strPtr, obj2.strPtr);
strcpy(tempObj.strPtr, obj1.strPtr);
return tempObj;
}
myString& myString::operator+=(const myString &obj)
{
*this = *this + obj;
return *this;
}
File: main.cpp
#include "mystring.h"
#include <fstream>
#include <cctype> // for toupper()
#include <string> // for strchr(), strstr(), etc.
#include <cassert>
#include <iostream>
using namespace std;
using namespace cs2b_mystring;
bool eof(istream& in);
void BasicTest();
void RelationTest();
void ConcatTest();
void CopyTest();
myString AppendTest(const myString& ref, myString val);
int main()
{
BasicTest();
RelationTest();
ConcatTest();
CopyTest();
return 0;
}
bool eof(istream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
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("string.data");
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("string.data");
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? " << (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");
const 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");
const 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 ";
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]) << 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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.