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

Create an implementation file (myString.cpp) in C++ for the following header (my

ID: 3710495 • Letter: C

Question

Create an implementation file (myString.cpp) in C++ for the following header (myString.h) and test file (test.cpp).

HEADER FILE:

class myString

{

public:

myString(const char *charString = ""); //links to pointer

   myString(const myString &other); //copy const - only has one arg. and is passed by ref.

   void operator = (const myString &other); //assignment operator

   ~myString(); //destructor is preceded by ~

int getLength() const; //returns length

   char operator [ ] (int n) const; // will take int locations and return a char from string

   void clear(); // makes head null

   void showStructure() const; //print string

   myString toupper() const; //convert a -> A

   myString tolower() const; //conver A->a

bool operator == (const myString& other) const; //equal length

   bool operator < (const myString& other) const; //less than length

   bool operator > (const myString& other) const; //greater length

private:

// Data members

   int myStringSize;

   char *Arr;

   // myString input/output

   friend istream & operator >> (istream& input, myString& inputmyString); //can access private data members

   friend ostream & operator << (ostream& output, const myString& outputmyString); //friend only in h file purpose is to outputstring

};

#endif

TEST FILE:

#include

#include "myString.h"

//--------------------------------------------------------------------

//

// Function prototype

void copyTester(myString copymyString); // copymyString is passed by value

void print_help();

//--------------------------------------------------------------------

int main()

{

   myString a("a"), // Predefined test myString objects

       alp("alp"),

       alpha("alpha"),

       epsilon("epsilon"),

       empty,

       assignmyString, // Destination for assignment

       inputmyString; // Input myString object

   int n; // Input subscript

   char ch, // Character specified by subscript

       selection; // Input test selection

// Get user test selection.

   print_help();

// Execute the selected test.

   cin >> selection;

cout << endl;

   switch (selection)

   {

   case '1':

       // Test 1 : Tests the constructors.

       cout << "Structure of various myString objects: " << endl;

       cout << "myString object: alpha" << endl;

       alpha.showStructure();

       cout << "myString object: epsilon" << endl;

       epsilon.showStructure();

       cout << "myString object: a" << endl;

       a.showStructure();

       cout << "empty myString object" << endl;

       empty.showStructure();

       break;

   case '2':

       // Test 2 : Tests the length operation.

       cout << "Lengths of various myString object:" << endl;

       cout << " alpha : " << alpha.getLength() << endl;

       cout << " epsilon : " << epsilon.getLength() << endl;

       cout << " a : " << a.getLength() << endl;

       cout << " empty : " << empty.getLength() << endl;

       break;

case '3':

       // Test 3 : Tests the subscript operation.

       cout << "Enter a subscript : ";

       cin >> n;

       ch = alpha[n];

       cout << " alpha[" << n << "] : ";

       if (ch == '')

           cout << "\0" << endl;

       else

           cout << ch << endl;

       break;

case '4':

       // Test 4 : Tests the assignment and clear operations.

       cout << "Assignments:" << endl;

       cout << "assignmyString = alpha" << endl;

       assignmyString = alpha;

       assignmyString.showStructure();

       cout << "assignmyString = a" << endl;

       assignmyString = a;

       assignmyString.showStructure();

       cout << "assignmyString = empty" << endl;

       assignmyString = empty;

       assignmyString.showStructure();

       cout << "assignmyString = epsilon" << endl;

       assignmyString = epsilon;

       assignmyString.showStructure();

       cout << "assignmyString = assignmyString" << endl;

       assignmyString = assignmyString;

       assignmyString.showStructure();

       cout << "assignmyString = alpha" << endl;

       assignmyString = alpha;

       assignmyString.showStructure();

       cout << "Clear assignmyString" << endl;

       assignmyString.clear();

       assignmyString.showStructure();

       cout << "Confirm that alpha has not been cleared" << endl;

       alpha.showStructure();

       break;

case '5':

       // Test 5 : Tests the copy constructor and operator= operations.

       cout << "Calls by value:" << endl;

       cout << "alpha before call" << endl;

       alpha.showStructure();

       copyTester(alpha);

       cout << "alpha after call" << endl;

       alpha.showStructure();

cout << "a before call" << endl;

       a.showStructure();

       a = epsilon;

       cout << "a after call" << endl;

       a.showStructure();

       cout << "epsilon after call" << endl;

       epsilon.showStructure();

       break;

case '6':

       // Test 6 : Tests toUpper and toLower

       cout << "Testing toUpper and toLower."

           << "Enter a mixed case string: " << endl;

       cin >> inputmyString;

       cout << "Input string:" << endl;

       inputmyString.showStructure();

       cout << "Upper case copy: " << endl;

       inputmyString.toUpper().showStructure();

       cout << "Lower case copy: " << endl;

       inputmyString.toLower().showStructure();

       break;

   case '7':

       // Test 7 : Tests the relational operations.

       cout << " left right < == > " << endl;

       cout << "--------------------------------" << endl;

       cout << " alpha epsilon " << (alpha

           << " " << (alpha == epsilon) << " "

           << (alpha>epsilon) << endl;

       cout << " epsilon alpha " << (epsilon

           << " " << (epsilon == alpha) << " "

           << (epsilon>alpha) << endl;

       cout << " alpha alpha " << (alpha

           << (alpha == alpha) << " " << (alpha>alpha) << endl;

       cout << " alp alpha " << (alp

           << (alp == alpha) << " " << (alp>alpha) << endl;

       cout << " alpha alp " << (alpha

           << (alpha == alp) << " " << (alpha>alp) << endl;

       cout << " a alpha " << (a

           << (a == alpha) << " " << (a>alpha) << endl;

       cout << " alpha a " << (alpha

           << (alpha == a) << " " << (alpha>a) << endl;

       cout << " empty alpha " << (empty

           << (empty == alpha) << " " << (empty>alpha) << endl;

       cout << " alpha empty " << (alpha

           << (alpha == empty) << " " << (alpha>empty) << endl;

       cout << " empty empty " << (empty

           << (empty == empty) << " " << (empty>empty) << endl;

       break;

default:

       cout << "'" << selection << "' specifies an inactive or invalid test" << endl;

   }

return 0;

}

//--------------------------------------------------------------------

void copyTester(myString copymyString)

// Dummy routine that is passed a myString object using call by value. Outputs

// copymyString and clears it.

{

   cout << "Copy of myString object" << endl;

   copymyString.showStructure();

   cout << "Clear copy" << endl;

   copymyString.clear();

   copymyString.showStructure();

}

//--------------------------------------------------------------------

void print_help()

{

   cout << endl << "Tests:" << endl;

   cout << " 1 Tests the constructors" << endl;

   cout << " 2 Tests the length operation" << endl;

   cout << " 3 Tests the subscript operation" << endl;

   cout << " 4 Tests the assignment and clear operations" << endl;

   cout << " 5 Tests the copy constructor and operator= operations" << endl;

   cout << " 6 Tests the toUpper and toLower operations " << endl;

   cout << " 7 Tests the relational operations " << endl;

   cout << "Select the test to run : "<

}

PLEASE INCLUDE COMMENTS IN THE IMPLEMENTATION FILE CODE.

Explanation / Answer


Given below is the code for the question. I had to change the myString.h to make the function names same as in test.cpp. Changed tolower() to toLower() and toupper() to toUpper()

Please do rate the answer if it was helpful. Thank you


myString.h
------------


#ifndef myString_h
#define myString_h

#include <iostream>
using namespace std;
class myString

{

public:

myString(const char *charString = ""); //links to pointer

myString(const myString &other); //copy const - only has one arg. and is passed by ref.

void operator = (const myString &other); //assignment operator

~myString(); //destructor is preceded by ~

int getLength() const; //returns length

char operator [ ] (int n) const; // will take int locations and return a char from string

void clear(); // makes head null

void showStructure() const; //print string

myString toUpper() const; //convert a -> A

myString toLower() const; //conver A->a

bool operator == (const myString& other) const; //equal length

bool operator < (const myString& other) const; //less than length

bool operator > (const myString& other) const; //greater length

private:

// Data members

int myStringSize;

char *Arr;

// myString input/output

friend istream & operator >> (istream& input, myString& inputmyString); //can access private data members

friend ostream & operator << (ostream& output, const myString& outputmyString); //friend only in h file purpose is to outputstring

};

#endif /* myString_h */


myString.cpp
------------

#include "myString.h"
#include <cstring>
#include <string>
#include <iostream>
#include <cctype>
using namespace std;

myString::myString(const char *charString) //links to pointer
{
myStringSize = strlen(charString);
Arr = new char[myStringSize+1];
strcpy(Arr, charString);
}

myString::myString(const myString &other) //copy const - only has one arg. and is passed by ref.
{
myStringSize = other.myStringSize;
Arr = new char[myStringSize+1];
strcpy(Arr, other.Arr);

}

void myString::operator = (const myString &other) //assignment operator
{
if(&other != this)
{
if(Arr != NULL)
delete[] Arr;
myStringSize = other.myStringSize;
Arr = new char[myStringSize+1];
strcpy(Arr, other.Arr);
}
}

myString::~myString()//destructor is preceded by ~
{
delete []Arr;
}

int myString::getLength() const //returns length
{
return myStringSize;
}

char myString::operator [ ] (int n) const // will take int locations and return a char from string
{
return Arr[n];
}

void myString::clear() // makes head null
{
if(Arr != NULL)
delete[] Arr;
Arr = NULL;
myStringSize = 0;
}

void myString::showStructure() const //print string
{
cout << *this << endl;
}

myString myString::toUpper() const //convert a -> A
{
myString result(*this);
for(int i = 0; i < result.myStringSize; i++)
result.Arr[i] = std::toupper(result.Arr[i]);
return result;
}

myString myString::toLower() const //conver A->a
{
myString result(*this);
for(int i = 0; i < result.myStringSize; i++)
result.Arr[i] = std::tolower(result.Arr[i]);
return result;
}

bool myString::operator == (const myString& other) const //equal length
{
return myStringSize == other.myStringSize;
}

bool myString::operator < (const myString& other) const //less than length
{
return myStringSize < other.myStringSize;

}

bool myString::operator > (const myString& other) const //greater length
{
return myStringSize > other.myStringSize;

}

istream & operator >> (istream& input, myString& inputmyString) //can access private data members
{
string s;
input >> s;
inputmyString = myString(s.c_str());
return input;
}

ostream & operator << (ostream& output, const myString& outputmyString) //friend only in h file purpose is to outputstring
{
if(outputmyString.Arr != NULL)
output << outputmyString.Arr;
return output;
}

output
=======
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 1

Structure of various myString objects:
myString object: alpha
alpha
myString object: epsilon
epsilon
myString object: a
a
empty myString object

$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 2

Lengths of various myString object:
alpha : 5
epsilon : 7
a : 1
empty : 0
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 3

Enter a subscript : 3
alpha[3] : h
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 4

Assignments:
assignmyString = alpha
alpha
assignmyString = a
a
assignmyString = empty

assignmyString = epsilon
epsilon
assignmyString = assignmyString
epsilon
assignmyString = alpha
alpha
Clear assignmyString
Segmentation fault: 11
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 5

Calls by value:
alpha before call
alpha
Copy of myString object
alpha
Clear copy
Segmentation fault: 11
$ g++ myString.cpp test.cpp
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 1

Structure of various myString objects:
myString object: alpha
alpha
myString object: epsilon
epsilon
myString object: a
a
empty myString object

$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 2

Lengths of various myString object:
alpha : 5
epsilon : 7
a : 1
empty : 0
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 3

Enter a subscript : 3
alpha[3] : h
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 4

Assignments:
assignmyString = alpha
alpha
assignmyString = a
a
assignmyString = empty

assignmyString = epsilon
epsilon
assignmyString = assignmyString
epsilon
assignmyString = alpha
alpha
Clear assignmyString

Confirm that alpha has not been cleared
alpha
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 5

Calls by value:
alpha before call
alpha
Copy of myString object
alpha
Clear copy

alpha after call
alpha
a before call
a
a after call
epsilon
epsilon after call
epsilon
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 6

Testing toUpper and toLower.Enter a mixed case string:
HeLLo
Input string:
HeLLo
Upper case copy:
HELLO
Lower case copy:
hello
$ ./a.out

Tests:
1 Tests the constructors
2 Tests the length operation
3 Tests the subscript operation
4 Tests the assignment and clear operations
5 Tests the copy constructor and operator= operations
6 Tests the toUpper and toLower operations
7 Tests the relational operations
Select the test to run : 7

left right < == >
--------------------------------
alpha epsilon 1 0 0
epsilon alpha 0 0 1
alpha alpha 01 0
alp alpha 10 0
alpha alp 00 1
a alpha 10 0
alpha a 00 1
empty alpha 10 0
alpha empty 00 1
empty empty 01 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