Question
Create an implementation file (myString.cpp) in C++ for the following header (myString.h) and test file (test.cpp). HEADER FILE: #ifndef myString_H #define myString_H #include #include using
Create an implementation file (myString.cpp) in C++ for the following header (myString.h) and test file (test.cpp).
HEADER FILE:
#ifndef myString_H
#define myString_H
#include
#include
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 lenght
char operator [] (int n) const; // will take int locations and return a char from srting
void clear(); // makes head null
void showStructure() const; //print strin
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 == '\0')
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;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started