Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with my C++. Instructions and given data are below. THANK YOU!!! Ins

ID: 3697710 • Letter: P

Question

Please help with my C++. Instructions and given data are below. THANK YOU!!!

Instructions -

Given Client program (main.cpp) -

Correct Output -

No SIM 4:58 PM 100% See also slient program and correct output Write a string class. To avoid conflicts wth other similany 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 -strings, (although it will be implemented as a c-string behind the scenes). The mystring class wil handle constructing strings, reading/printing, and accessing characters. In ackdition, the myString object will have the ablity to make a full deep-copy of tsef when copied Your class must have only one data member, a e-string implemented as a dynamic array. In particular, you must not use a data member to keep track f thie size or length of th 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 wil 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 e-string. You should copy the string data, net just stone a pointer to an argument passed to the constructor. Constructing a myString with no arguments creates an empty myString object (l.e ..). A mystring ooiect should be implemented efficiently (space-wise) which to say you should nat have a fioxed-size buffer of chars, but instead allocate space for chars on an as-needed basis. Use strepy) Printing a myString to a stream using an overloaded

Explanation / Answer


#include "mystring.h"
#include <cctype>      // for toupper()
#include <iostream>
#include <string>
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();
}

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 << "----- 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;
}

string boolString(bool convertMe) {
    if (convertMe) {
        return "true";
    } else {
        return "false";
    }
}

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;
}

myString AppendTest(const myString& ref, myString val)
{
    val[0] = 'B';
    return val;
}

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.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>

using namespace std;


namespace cs_mystring
{
    class myString
    {
        public:
            myString();
            myString(const char *inString);                 // default constructor
      
            myString(const myString& right);                // copy constructor
      
            ~myString();                                    // destructor
      
            myString operator=(const myString& right);      // assignment opperator
      
            friend ostream& operator<<(ostream& out, const myString& right);
            friend istream& operator>>(istream& in, myString& right);
          
            char operator[](int index) const;          // []overload (value)
            char& operator[](int index);               // []overload (reference)
          
            friend bool operator<(const myString &left,
                                  const myString &right);
            friend bool operator<=(const myString &left,
                                   const myString &right);
            friend bool operator>(const myString &left,
                                  const myString &right);
            friend bool operator>=(const myString &left,
                                   const myString &right);
            friend bool operator==(const myString &left,
                                   const myString &right);
            friend bool operator!=(const myString &left,
                                   const myString &right);
      
            int length() const;
      
        private:
            char *string;
    };
}

#endif


mystring.cpp

#include <iostream>
#include <cassert>
#include "mystring.h"
using namespace std;

namespace cs_mystring
{
    myString::myString()
    {
        string = new char[1];
        strcpy(string, "");
    }
    myString::myString(const char *inString)
    {
        string = new char[strlen(inString) + 1];
        strcpy(string, inString);
    }
  
    myString::myString(const myString& right)
    {
        string = new char[strlen(right.string) + 1];
        strcpy(string, right.string);
    }
  
    myString::~myString()
    {
        delete [] string;
    }
    myString myString::operator=(const myString &right)
    {
        if (this != &right){
            delete [] string;
            string = new char[strlen(right.string) + 1];
            strcpy(string, right.string);
        }
        return *this;
    }
  
    ostream& operator<<(ostream& out, const myString& right)
    {
        out << right.string;
        return out;
    }
  
    istream& operator>>(istream& in, myString& right)
    {
        while (isspace(in.peek())){
            in.ignore();
        }
      
        char temp[128];
        in.getline(temp, 127);
        delete [] right.string;
        right.string = new char[strlen(temp) + 1];
        strcpy(right.string, temp);
      
        return in;
    }
    // []overload (value)
    char myString::operator[](int index) const
    {
        assert(index >= 0 && index < strlen(string));
        return string[index];
    }

   // []overload (reference)
    char& myString::operator[](int index)
    {
        assert(index >= 0 && index < strlen(string));
        return string[index];
    }
  
    bool operator<(const myString& left,
                   const myString& right)
    {
        if(strcmp(left.string, right.string) < 0)
        {
            return true;
        } else {
            return false;
        }
    }

  
  
  
  
  
  
  
  
    bool operator<=(const myString& left,
                   const myString& right)
    {
        if(strcmp(left.string, right.string) < 0 || strcmp(left.string, right.string) == 0)
        {
            return true;
        } else
            return false;
    }

  
  
  
  
  
  
  
  
    bool operator>(const myString& left,
                   const myString& right)
    {
        if(strcmp(left.string, right.string) > 0)
        {
            return true;
        } else {
            return false;
        }
    }

  
  
  
  
  
  
  
  
    bool operator>=(const myString& left,
                    const myString& right)
    {
        if(strcmp(left.string, right.string) > 0 || strcmp(left.string, right.string) == 0)
        {
            return true;
        } else
            return false;
    }

  
  
  
  
  
  
  
  
    bool operator!=(const myString& left,
                    const myString& right)
    {
        if(strcmp(left.string, right.string) == 0)
        {
            return false;
        } else {
            return true;
        }
    }

  
    bool operator==(const myString& left,
                    const myString& right)
    {
        if(strcmp(left.string, right.string) == 0)
        {
            return true;
        } else {
            return false;
        }
    }
  
    int myString::length() const
    {
        return strlen(string);
    }
}

sample 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
Program ended with exit code: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote