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

A string in C++ is simply an array of characters with the null character(\\0) us

ID: 3888653 • Letter: A

Question

A string in C++ is simply an array of characters with the null character() used to mark the end of the string. C++ provides a set of string handling function in as well as I/O functions in . With the addition of the STL (Standard Template Library), C++ now provides a string class.

But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with a C++ class, define constructors and destructors, define member functions outside of the class body, develop a copy constructor and assignment operator (and understand why!), work with C-Style strings and pointers, dynamically allocate memory and free it when done.

Of course, you must also do suitable testing which implies writing a main function that uses your new string class.

The following is the skeleton of the Mystring class declaration. Mystring.h file is provided. You must produce the Mystring.cpp and main.cpp files.

class Mystring

{

private:

char *pData; //pointer to simple C-style representation of the string

//(i.e., sequence of characters terminated by null)

//pData is only a pointer. You must allocate space for

//the actual character data

int length; //length of the string

//… //possibly other private data

public:

MyString(); //constructor --- create empty string

MyString(char *cString); //constructor --- create a string whose data is a copy of

//cString

~MyString(); //destructor -- don't forget to free space allocated by the constructor

//i.e., the space allocated for the character data

MyString(MyString const& s); //override the default copy constructor --- why?

//important -- think about it -- possible test question

MyString operator = (MyString const& s); //override default assignment operator

void Put(); //output string

void Reverse(); //reverse the string

MyString operator + (MyString const& s); //concatenation operator

// …                                                                       //other useful member functions

//as you wish

};

Explanation / Answer

//main.cpp

#include "MyString.h"
#include <iostream>


using namespace std;

int main()
{
   MyString s1;                       //creating instances of MyString using the different constructors
   MyString s2 = "Pepperoni";
   MyString s3 = "Pizza";
   MyString s4(s2);
                                       //Verifying constructors work
   cout << "The value of MyString s1 with default constructor is: "; s1.Put();
   cout << endl << "The value of MyString s2, copy contructor of a cstring: "; s2.Put();
   cout << endl << "The value of MyString s4, initialized with the contents of s2: "; s4.Put();
   cout << endl << "Now lets test the operator overloading. Testing s2 + s3........" << endl << "s2 now contains: ";
   s2 + s3; s2.Put();
   cout << endl << "And now testing s1 = s2....." << endl << "s1 now contains: ";
   s1 = s2; s1.Put();
   cout << endl << "And Finally we will Test the reverse function. The reverse of s1 is: ";
   s1.Reverse(); s1.Put();
   cout << endl << "That is all. The program will now automatically call the destructor functions. Freeing memory. " << endl;


   return 0;
}

===========================================================================

//MyString.cpp

#ifndef MYSTRING_CPP
#define MYSTRING_CPP
#include <string.h>
#include <iostream>
#include "MyString.h"

using namespace std;

MyString::MyString()
{
   length = 0;                       //initializing array to size of 0. Will later be overwritten
   pData = new char[NULL];
   pData[length] = '';
}
/*
Input - c-string
copy this c-string into MyString. update critical value in MyString
output - returns nothing.
*/
MyString::MyString(char *cString)           //copy constructor of an existing c-string
{
   length = strlen(cString);               //using build in function to get the length of string we are copying
   this->pData = new char[length + 1];               //creating are array of the appropriate length to hold the c-string
   for (int i = 0; i < length; i++)       //copying array contents one by one
   {
       pData[i] = cString[i];
   }
   pData[length] = '';
}

/*
Input - MyString
initialize MyString to the same values of the inputed MyString
OutPut - nothing
*/
MyString::MyString(MyString const& s)       //we already have MyString just need to copy. Will copy one element at a time until NULL
{
   length = s.length;                           //our cstring will be at least one character long
   this->pData = new char[length + 1];                  
   for (int i = 0; i < length; i++)
   {
       pData[i] = s.pData[i];               //copying passed mystring into curret cstring
   }
   pData[length] = '';                   //adding null character to end of array
}


/*
Input - nothing
de-allocate memory
Output - nothing
*/
MyString::~MyString()           //deletes dynamic array and deletes references to it
{  
   if (pData != NULL)
   {
   delete [] pData;
   pData = NULL;
   length = NULL;
   }
}

/*
Input - MyString - constant we do(MyString1 = MyString2)
We assign the inputed MyString2 to MyString1. Update length of MyString1. Copy element by element
Output - MyString1
*/
MyString& MyString::operator=(MyString const& s)
{
   length = s.length;
   for (int i = 0; i < s.length; i++)
   {
       this->pData[i] = s.pData[i];
   }
   pData[length] = '';

   return *this;
}

/*
Input - MyString constant
Get Length of left and right side arrays. Add them together to get the new length. Update length in left side array.
Add the characters from the right side array to the end of the left side array. Add terminating character to end of
array.
Output - Return the left side array, which is now leftside + rightside
*/
MyString& MyString::operator+(MyString const& s)
{
   int lLength = length;
   length = lLength + s.length;
   for (int i = lLength; i < length; i++)
   {
           this->pData[i] = s.pData[i-lLength];
   }
   this->pData[length] = '';

   return *this;
}

/*
Input - Nothing
Output - pData at indicated element until termination character is met
*/
void MyString::Put()
{
   for(int i = 0; pData[i] != ''; i++)
   {
       cout << pData[i];
   }
}

/*
Input - Nothing
Reverse function. Create a temp value to hold value to be swapped. -1 from length to account for termination character. Swap 0 with max.
Increment low value. Decrement High Value. Check to see that high-low is >=1. If true continue. Else Exit. Rinse. Repeat.
output - Nothing
*/
void MyString::Reverse()
{
   int max = length - 1;               //dont want to be moving the null character
   char temp = NULL;
   for(int i = 0; (max - i) >= 1; i++)
   {
       temp = pData[i];
       this->pData[i] = this->pData[max];
       this->pData[max] = temp;
       max--;
   }
}

#endif

============================================================================

//MyString.h

#ifndef MYSTRING_H
#define MYSTRING_H

class MyString  
{
private:
char *pData;       //pointer to simple C-style representation of the string
                           //(i.e., sequence of characters terminated by null)
                           //pData is only a pointer. You must allocate space for
                           //the actual character data
int length;       //length of the string
                           //
                   //possible other private data

public:
MyString();               //constructor --- create empty string
MyString(char *cString);   //constructor --- create a string whose data is a copy of
                               //cString
~MyString();               //destructor -- don't forget to free space allocated by the constructor
                               //i.e., the space allocated for the character data

MyString(MyString const& s);   //override the default copy constructor --- why?
                                   //important -- think about it -- possible test question
MyString& operator = (MyString const& s); //override default assignment operator
void Put();       //output string
void Reverse();   //reverse the string

MyString& operator + (MyString const& s); //concatenation operator
//                    //other useful member functions
                       //as you wish
};

#endif // MYSTRING_H

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