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: 3888286 • 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

Mystring.h

class Mystring

{

private:

char *pData;

int length;

public:

MyString();

MyString(char *cString);

~MyString();

MyString(MyString const& s);

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

};

Mystring.cpp

#include <iostream.h>

#include <stdlib.h>

#include <stdio.h>

#include "string.h"

#include "Mystring.h"

Mystring::Mystring()

{

// Make pData an empty string

pData = new char[1];

*pData = '';

}

Mystring::Mystring(char* cString)

{

// Allocate enough space for the char*

pData = new char[strlen(cString)+1];

// Copy the char* into the internal string data

strcpy(pData, cString);

}

Mystring::~Mystring()

{

delete pData;

}

Mystring::Mystring(Mystring const& s)

{

// Allocate enough space for the new string

pData = new char[s.length()+1];

strcpy(pData, s.pData);

}

Mystring& Mystring::operator=(Mystring const& s)

{

if (&s == this)

{

return *this;

}

// Delete the old string that this class was holding.

delete [] pData;

// Allocate enough space for the new string.

pData = new char[s.length() + 1];

// Copy the new string into myself

strcpy(pData, s.pData);

// Return myself

return *this;

}

Mystring Mystring::operator+(const Mystring& s)

{

// Temp string to old the original data

Mystring tmp;

// Allocate enough space for both string

tmp.pData = new char[s.length() + this->length() + 1];

// Copy and concat the strings

strcpy(tmp.pData, pData);

strcat(tmp.pData, s.pData);

// Return the temporary string

return tmp;

}

Mystring::Put()

{

cout<<pData<<endl;

}

Mystring::Reverse()

{

int n = strlen(pData);

for (int i=0; i<n/2; i++)

swap(str[i], str[n-i-1]);

}

main.cpp

#include <iostream.h>

#include "Mystring.h"

int main(){

  

// construction and initialization

Mystring c;

Mystring a = "Hello ";

Mystring b = a;

Mystring d = a+b;

Mystring x = d.Reverse();

d.Put();

return 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