write C++ code to test mystring Class #include <iostream> #include \"myString.h\
ID: 3862517 • Letter: W
Question
write C++ code to test mystring Class
#include <iostream> #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<epsilon) << " " << (alpha == epsilon) << " " << (alpha>epsilon) << endl; cout << " epsilon alpha " << (epsilon<alpha) << " " << (epsilon == alpha) << " " << (epsilon>alpha) << endl; cout << " alpha alpha " << (alpha<alpha) << " " << (alpha == alpha) << " " << (alpha>alpha) << endl; cout << " alp alpha " << (alp<alpha) << " " << (alp == alpha) << " " << (alp>alpha) << endl; cout << " alpha alp " << (alpha<alp) << " " << (alpha == alp) << " " << (alpha>alp) << endl; cout << " a alpha " << (a<alpha) << " " << (a == alpha) << " " << (a>alpha) << endl; cout << " alpha a " << (alpha<a) << " " << (alpha == a) << " " << (alpha>a) << endl; cout << " empty alpha " << (empty<alpha) << " " << (empty == alpha) << " " << (empty>alpha) << endl; cout << " alpha empty " << (alpha<empty) << " " << (alpha == empty) << " " << (alpha>empty) << endl; cout << " empty 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 : "<<endl; }
Explanation / Answer
//File1- mystring.h
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
class myString
{
private:
string mystr;
int SIZE;
public:
myString();
myString(string mystring);
myString(const myString&);
~myString();
void showStructure();
unsigned int getLength();
char operator[](int i);
myString& operator=(const myString&);
bool operator<(const myString& rhs);
bool operator>(const myString& rhs);
bool operator==(const myString& rhs);
friend istream &operator>>(istream& input, myString& rhs);
myString toUpper();
myString toLower();
void clear();
};
// File2 -mystring.cpp
#include "mystring.h"
#include<iostream>
#include <string>
using namespace std;
char myString::operator[](int i)
{
if (i > SIZE)
{
cout << "Index out of bounds" << endl;
return this->mystr[0];
}
return this->mystr[i];
}
myString::myString()
{
mystr = "";
SIZE = 0;
}
myString::myString(string str)
{
mystr = str;
SIZE = str.length();
}
myString::myString(const myString& str)
{
}
myString::~myString()
{
}
myString& myString::operator=(const myString& str)
{
mystr = str.mystr;
SIZE = str.mystr.length();
return *this;
}
void myString::showStructure()
{
cout << this->mystr <<endl;
}
unsigned int myString::getLength()
{
return mystr.length();
}
void myString::clear()
{
this->mystr = "";
}
bool myString::operator<(const myString& rhs)
{
if (mystr < rhs.mystr)
return true;
else
return false;
}
bool myString::operator>(const myString& rhs)
{
if (mystr>rhs.mystr)
return true;
else
return false;
}
bool myString::operator==(const myString& rhs)
{
if (mystr==rhs.mystr)
return true;
else
return false;
}
istream &operator>>(istream& input, myString& rhs)
{
input >> rhs.mystr;
return input;
}
myString myString::toUpper()
{
for (unsigned int i = 0; i < mystr.length(); i++)
mystr[i] = toupper(mystr[i]);
cout << mystr << endl;
return (myString)mystr;
}
myString myString::toLower()
{
for (unsigned int i = 0; i < mystr.length(); i++)
mystr[i] = tolower(mystr[i]);
cout << mystr << endl;
return (myString)mystr;
}
// File3 Test.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "mystring.h"
using namespace std;
void copyTester(myString copymyString); // copymyString is passed by value
void print_help();
int main()
{
myString a("a"); // Predefined test myString objects
myString alp("alp");
myString alpha("alpha");
myString epsilon("epsilon");
myString empty;
myString assignmyString; // Destination for assignment
myString inputmyString; // Input myString object
int n; // Input subscript
char ch; // Character specified by subscript
char selection; // Input test selection
print_help(); // Get user test selection.
// 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<epsilon)
<< " " << (alpha == epsilon) << " "
<< (alpha>epsilon) << endl;
cout << " epsilon alpha " << (epsilon<alpha)
<< " " << (epsilon == alpha) << " "
<< (epsilon>alpha) << endl;
cout << " alpha alpha " << (alpha<alpha) << " "
<< (alpha == alpha) << " " << (alpha>alpha) << endl;
cout << " alp alpha " << (alp<alpha) << " "
<< (alp == alpha) << " " << (alp>alpha) << endl;
cout << " alpha alp " << (alpha<alp) << " "
<< (alpha == alp) << " " << (alpha>alp) << endl;
cout << " a alpha " << (a<alpha) << " "
<< (a == alpha) << " " << (a>alpha) << endl;
cout << " alpha a " << (alpha<a) << " "
<< (alpha == a) << " " << (alpha>a) << endl;
cout << " empty alpha " << (empty<alpha) << " "
<< (empty == alpha) << " " << (empty>alpha) << endl;
cout << " alpha empty " << (alpha<empty) << " "
<< (alpha == empty) << " " << (alpha>empty) << endl;
cout << " empty empty " << (empty<empty) << " "
<< (empty == empty) << " " << (empty>empty) << endl;
break;
default:
cout << "'" << selection << "' specifies an inactive or invalid test" << endl;
}
return 0;
}
// Dummy routine that is passed a myString object using call by value. Outputs
// copymyString and clears it.
void copyTester(myString copymyString)
{
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 : " << endl;
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.