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

Help with this C++ code please, I\'m using Visual Studio Project Title: String C

ID: 3854209 • Letter: H

Question

Help with this C++ code please, I'm using Visual Studio

Project Title: String Class implementation. DO NOT USEINCLUDE string type (e g #include Objective: Learning design and use of object oriented programming, operator overloading, constructors (copy constructor, default constructor etc.), destructors, 'this pointer, friendship relation, and static variables. Input: String or strings (e.g. s1+s2 will return concatenation of strings sl and s2) Output: Required operations' outputs e.g. s1>s2 returns true or false, s1[2] returns third char in string s1, s1Length0 returns length of s1 etc Project Description: You are required to create your own Strin class (Note: String class is different than built in string class) like string type in C++. This class should include following operations (10 Points) Separate specifications from implementation. Keep header file and class implementation in separate files * (20 Points) Can initialize created object with a given string or given instance of the same class (use copy constructor (10 Points) Dynamically allocates memory for a given string (e.g. String s("Joe"); in this case allocates 3 memory spaces for String class instanc * (10Points) Returns string length if it is requested (e.g. sLengthO) (10 Points) Clears dynamically allocated memory for String object while destructing (do it in destructor) · * (20 Points) Checks boundaries (e.g. if String s size is 10, s[11]- 'A'; will display error message on screen) (20 Points) In public section of the String class only include interfaces. All other variables and functions must be in private section (30 Points) Overloads+ operator so that two string can be concatenated. (e.g. String sLs2,s3s3 = s1+s2) . when only have sl+s2; concatenated string will be included in sl (30 Points) Overloads (30 Points) Compares two given objects (e.g. String ab a>b or a operators (30 Points) Copies one object to another (e.g. a=b or a=b=c) (overload-operator for this purpose) . (20 Points) When required returns number of objects created so far. (e.g. String ob·ou count(). Obicount ) returns total number of non-empty String objects) . (30 Points) Can get input from the steam class instance (e.g. cin»s;). Make it friend of the String class .

Explanation / Answer

mystring.cpp

#include "mystring.h"

#include <iostream>

#include <string.h>   

using namespace std;

myString::myString() : length(0), buff(NULL)

{

cout << " myString default constr. ";

}

void myString::Put()

{

for( int i=0; i < length; i++ ) cout<<( buff[i] );

}

myString::myString( char* cString ) :

length( my_strlen(cString) ), // count the length of init value

buff( new char[length] ) // allocate storage

{

cout << " myString( char * ) constr. ";

my_strncpy( buff, cString, length ); // copy init value into storage

}

myString::myString( const myString& s ) :

length( s.length ),

buff( new char[length] )

{

  

my_strncpy( buff, s.buff, length );

}

myString::~myString()

{

cout << " myString destr. ";

delete [] buff;

}

int myString::size() { return length; }

  

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

{

cout << " myString::operator= ";

if( this != &s ){ // guard against a = a;

delete [] buff; // release old memory & then

length = s.length; // allocate new memory

buff = new char[length];

my_strncpy( buff, s.buff, length );

}

return *this; // return a reference to itself

}

char& myString::operator[] (int index )

{

if( index < 0 || index > length ) {

cerr << "Invalid index in myString ";

  

}

return buff[index];

}

myString myString::operator+( const myString& s1)

{

myString res;

res.length = s1.length + this->length;

res.buff = new char[ res.length ];

my_strncpy( res.buff, this->buff, this->length );

my_strncpy( res.buff + this->length, // pointer arithmetic!

s1.buff,

s1.length );

return res;

}

void myString::Reverse()

{

char result[20];

for (int i = 0; i<length; ++i)

{

result[i]=buff[length-i-1];

}

my_strncpy(buff,result,length);

}

ostream& operator<< ( ostream& os, const myString& s )

{

// print char after char from buff

for( int i=0; i < s.length; i++ ) os.put( s.buff[i] );

return os; // this is to allow multiple <<, as in cout << a << b;

}   

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

mystring.h

#ifndef MY_STRING_H
#define MY_STRING_H

#include <iostream>

#include <string.h>   

using namespace std;
int my_strlen( char * ptr ){
int len = 0;
char * p = ptr;
while( *p != '' ) {
len++;
p++;
}
return len;
}

void my_strncpy( char * ptr_dest, char * ptr_src, int n ){
for( int i=0; i < n; i++ )
ptr_dest[i] = ptr_src[i];
}

class myString {
  
public:

int length; // length of the string
char * buff; // pointer to strorage

  

myString();
~myString();
myString( char* cString );
myString( const myString& other );
int size();
myString& operator= ( const myString& other );
char& operator[] (int index );
myString operator+( const myString& s1 );
void Put();
void Reverse();
  
  
  

  

  
};

#endif // MY_STRING_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