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

MUST BE C++ MUST WORK MUST HAVE ALL FILES A. Extend the definition of the class

ID: 657211 • Letter: M

Question

MUST BE C++

MUST WORK

MUST HAVE ALL FILES

A. Extend the definition of the class newString as follows:

i.   Overload the operators + and += to perform the string concate-
nation operations.

ii.   Add the function length to return the length of the string

B. Write the definition of the function to implement the operations defined in part A.

C. Write a test program to test various operations on the newString objects.

//Header file myString.h
#ifndef H_myString #define H_myString
#include <iostream> using namespace std;
class newString {

//Overload the stream insertion and extraction operators.
friend ostream& operator << (ostream&, const newString&); friend istream& operator >> (istream&, newString&);
public: const newString& operator=(const newString&);

//overload the assignment operator
newString(const char *); //constructor; conversion from the char string
newString();
//Default constructor to initialize the string to null
newString(const newString&);

//Copy constructor

~newString();

//Destructor
char &operator[] (int); const char &operator[](int) const;
//overload the relational operators
bool operator==(const newString&) const;

bool operator!=(const newString&) const; bool operator<=(const newString&) const;

bool operator<(const newString&) const; bool operator>=(const newString&) const;

bool operator>(const newString&) const;
private:

char *strPtr; //pointer to the char array that holds the string

int strLength; //variable to store the length of the string
}; #endif

//Implementation file myStringImp.cpp
#include <iostream>

#include <iomanip>

#include <cstring>

#include <cassert>

#include "myString.h"
using namespace std;

//Constructor: conversion from the char string to newString
newString::newString(const char *str)

{

strLength = strlen(str);

strPtr = new char[strLength + 1]; //allocate memory to store the char string

strcpy(strPtr, str); //copy string into strPt

}

//Default constructor to store the null string

newString::newString()

{

strLength = 0;

strPtr = new char[1];

strcpy(strPtr, "");

}

newString::newString(const newString& rightStr) //copy constructor

{

strLength = rightStr.strLength;

strPtr = new char[strLength + 1];

strcpy(strPtr, rightStr.strPtr);

}

newString::~newString() //destructor

{

delete [] strPtr;

}

//overload the assignment operator

const newString& newString::operator=(const newString& rightStr)

{

if (this != &rightStr) //avoid self-copy

{

  delete [] strPtr;

strLength = rightStr.strLength;

strPtr = new char[strLength + 1];

strcpy(strPtr, rightStr.strPtr);

}

return*this;

}

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

{

assert(0 <= index && index < strLength);

return strPtr[index];

}

const char& newString::operator[](int index) const

{

assert(0 <= index && index < strLength);

return strPtr[index];

}

//Overload the relational operators.

bool newString::operator==(const newString& rightStr) const

{

return (strcmp(strPtr, rightStr.strPtr) == 0);

}

bool newString::operator<(const newString& rightStr) const

{
return (strcmp(strPtr, rightStr.strPtr) < 0);

bool newString::operator<=(const newString& rightStr) const
}
{ }
return (strcmp(strPtr, rightStr.strPtr) <= 0); bool newString::operator>(const newString& rightStr) const
{ }
return (strcmp(strPtr, rightStr.strPtr) > 0); bool newString::operator>=(const newString& rightStr) const
{ }
{ }
return (strcmp(strPtr, rightStr.strPtr) >= 0); bool newString::operator!=(const newString& rightStr) const
return (strcmp(strPtr, rightStr.strPtr) != 0);
//Overload the stream insertion operator <<
ostream& operator << (ostream& osObject, const newString& str) {
}
osObject << str.strPtr; return osObject;
//Overload the stream extraction operator >>
istream& operator >> (istream& isObject, newString& str) {
}
char temp[81];
isObject >> setw(81) >> temp; str = temp; return isObject;

int main() {
newString str1 = "Sunny";

const newString str2("Warm");

// This program shows how to use the class newString.

#include <iostream>

#include "myString.h"
using namespace std;
int main()

{
newString str1 = "Sunny";

const newString str2("Warm");

newString str3; //initialize str3 to the empty string

newString str4; //initialize str4 to the empty string

cout << "Line 1: " << str1 << "   " << str2

<< " ***" << str3 << "###." << endl;

if (str1<= str2)

cout << "Line 3: " << str1 << " is less "

<< "than  or equal to" << str2 << endl;

else

cout << "Line 5: " << str2 << " is less "

<< "than " << str1 << endl;

cout << "Line 6: Enter a string with a length "

  << "of at least 7: ";

cin >> str1;

cout << endl;

cout << "Line 9: The new value of "

<< "str1 ="<<str1<<endl;

str4 = str3 = "Birth Day";

cout << "Line 11: str3 = " << str3

<<",str4="<<str4<<endl;

str3 = str1;

cout << "Line 13: The new value of str3 = "

<< str3 << endl;

str1 = "Bright Sky";

str3[1] = str1[5];

cout << "Line 16: After replacing the second "

  << "character of str3 = " << str3 << endl;

str3[2] = str2[0];

cout << "Line 18: After replacing the third "

<< "character of str3 = " << str3 << endl;

str3[5] = 'g';

cout << "Line 20: After replacing the sixth "

<< "character of str3 = " << str3 << endl;

return 0;

}

Explanation / Answer

/Header file myString.h
#ifndef H_myString
#define H_myString
#include <iostream>
using namespace std;
class newString {
//Overload the stream insertion and extraction operators.
friend ostream& operator << (ostream&, const newString&);
friend istream& operator >> (istream&, newString&);
public: const newString& operator=(const newString&);

//overload the assignment operator
newString(const char *); //constructor; conversion from the char string
newString();
//Default constructor to initialize the string to null
newString(const newString&);

//Copy constructor

~newString();

//Destructor
char &operator[] (int); const char &operator[](int) const;
//overload the relational operators
bool operator==(const newString&) const;

bool operator!=(const newString&) const; bool operator<=(const newString&) const;

bool operator<(const newString&) const; bool operator>=(const newString&) const;

bool operator>(const newString&) const;
friend newString operator+(const newString& left,const newString& right);
void operator+=(const newString&);
int length();

private:
char *strPtr; //pointer to the char array that holds the string
int strLength; //variable to store the length of the string
};
#endif

//Implementation file myStringImp.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "myString.h"
using namespace std;
//Constructor: conversion from the char string to newString
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1]; //allocate memory to store the char string
strcpy_s(strPtr, str); //copy string into strPt
}
//Default constructor to store the null string
newString::newString()
{
strLength = 0;
strPtr = new char[1];
strcpy_s(strPtr, "");
}
newString::newString(const newString& rightStr) //copy constructor
{
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
strcpy_s(strPtr, rightStr.strPtr);
}

newString::~newString() //destructor
{
delete [] strPtr;
}
int newString::length()
{
   return strLength;
}
//overload the assignment operator
const newString& newString::operator=(const newString& rightStr)
{
if (this != &rightStr) //avoid self-copy
{
delete [] strPtr;
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
strcpy_s(strPtr, rightStr.strPtr);
}
return*this;
}
char& newString::operator[] (int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char& newString::operator[](int index) const
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
//Overload the relational operators.
newString operator+(const newString& left,const newString& right)
{
   newString temp;
   temp.strLength=right.strLength+left.strLength;
   temp.strPtr=left.strPtr;
   strcat(temp.strPtr,right.strPtr);
   return temp;
}
void newString::operator+=(const newString& right)
{
   this->strLength=right.strLength+this->strLength;
   strcat(this->strPtr,right.strPtr);
}
bool newString::operator==(const newString& rightStr) const
{
   return (strcmp(strPtr, rightStr.strPtr) == 0);
}
bool newString::operator<(const newString& rightStr) const
{
   return (strcmp(strPtr, rightStr.strPtr) < 0);
}
bool newString::operator<=(const newString& rightStr) const
{
   return (strcmp(strPtr, rightStr.strPtr) <= 0);
}
bool newString::operator>(const newString& rightStr) const
{
   return (strcmp(strPtr, rightStr.strPtr) > 0);
}
bool newString::operator>=(const newString& rightStr) const
{
   return (strcmp(strPtr, rightStr.strPtr) >= 0);
}
bool newString::operator!=(const newString& rightStr) const
{
   return (strcmp(strPtr, rightStr.strPtr) != 0);
}
//Overload the stream insertion operator <<
ostream& operator << (ostream& osObject, const newString& str)
{
osObject << str.strPtr; return osObject;
}
//Overload the stream extraction operator >>
istream& operator >> (istream& isObject, newString& str)
{
   char temp[81];
   isObject >> setw(81) >> temp; str = temp; return isObject;
}

//Testing function containing Main()
// This program shows how to use the class newString.
#include <iostream>
#include "myString.h"
using namespace std;
int main()
{
   newString str1 = "Sunny";
   const newString str2("Warm");
   newString temp;
   temp=str1+str2;
   cout<<"After Concatenating str1 and str2 :"<<temp<<endl;
   temp+=str2;
   cout<<"After Concatenating temp and str2 :"<<temp<<endl;

   newString str3; //initialize str3 to the empty string
   newString str4; //initialize str4 to the empty string
   cout << "Line 1: " << str1 << "    " << str2<< " ***" << str3 << "###." << endl;
   if (str1<= str2)
   cout << "Line 3: " << str1 << " is less "<< "than or equal to" << str2 << endl;
   else
   cout << "Line 5: " << str2 << " is less "<< "than " << str1 << endl;
   cout << "Line 6: Enter a string with a length "<< "of at least 7: ";
   cin >> str1;
   cout << endl;
   cout << "Line 9: The new value of "<< "str1 ="<<str1<<endl;
   str4 = str3 = "Birth Day";
   cout << "Line 11: str3 = " << str3<<",str4="<<str4<<endl;
   str3 = str1;
   cout << "Line 13: The new value of str3 = "<< str3 << endl;
   str1 = "Bright Sky";
   str3[1] = str1[5];
   cout << "Line 16: After replacing the second "<< "character of str3 = " << str3 << endl;
   str3[2] = str2[0];
   cout << "Line 18: After replacing the third "<< "character of str3 = " << str3 << endl;
   str3[5] = 'g';
   cout << "Line 20: After replacing the sixth "<< "character of str3 = " << str3 << endl;

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