Question

In: Computer Science

Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...

Complete the following:

Extend the newString class (attached) to include the following:

  1. Overload the operator + to perform string concatenation.
  2. Overload the operator += to work as shown here:
    • s1 = "Hello "
    • s2 = "there"
    • s1 += s2 // Should assign "Hello there" to s1
  3. Add a function length to return the length of the string.
  4. Write a test program.

//myString.h (header file)

//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

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

//myStringImp.cpp (implementation file)

//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 strPtr
}

//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;
}

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

//main.cpp (test file)

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

using namespace std;

int main()
{
newString str1 = "Sunny";        //initialize str1 using
//the assignment operator
const newString str2("Warm");   //initialize str2 using the
//conversion constructor
newString str3; //initialize str3 to the empty string
newString str4; //initialize str4 to the empty string

cout << "Line 1: " << str1 << " " << str2
<< " ***" << str3 << "###." << endl; //Line 1

if (str1 <= str2) //compare str1 and str2; Line 2
cout << "Line 3: " << str1 << " is less "
<< "than or equal to " << str2 << endl;//Line 3
else //Line 4
cout << "Line 5: " << str2 << " is less "
<< "than " << str1 << endl; //Line 5

cout << "Line 6: Enter a string with a length "
<< "of at least 7: "; //Line 6
cin >> str1; //input str1; Line 7
cout << endl; //Line 8

cout << "Line 9: The new value of "
<< "str1 = " << str1 << endl; //Line 9

str4 = str3 = "Birth Day"; //Line 10

cout << "Line 11: str3 = " << str3
<< ", str4 = " << str4 << endl; //Line 11

str3 = str1; //Line 12
cout << "Line 13: The new value of str3 = "
<< str3 << endl; //Line 13

str1 = "Bright Sky"; //Line 14

str3[1] = str1[5]; //Line 15
cout << "Line 16: After replacing the second "
<< "character of str3 = " << str3 << endl; //Line 16

str3[2] = str2[0]; //Line 17
cout << "Line 18: After replacing the third "
<< "character of str3 = " << str3 << endl; //Line 18

str3[5] = 'g'; //Line 19
cout << "Line 20: After replacing the sixth "
<< "character of str3 = " << str3 << endl; //Line 20

return 0;
}

Solutions

Expert Solution

TESTS FOR BOTH THE IMPLEMENTATION ARE ADDED AT LAST OF MAIN.CPP

//myString.h (header file)

//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;
    newString operator+ (const newString&);
    newString& operator+=(const newString&);
    private:
    char *strPtr; //pointer to the char array
    //that holds the string
    int strLength; //variable to store the length
    //of the string
};

#endif




// 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 strPtr
}

//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);
}

newString newString::operator+(const newString& rightStr){
    newString res ;
    int temp_len = strLength;
    res.strLength = strlen(strPtr) + strlen(rightStr.strPtr) + 1;
    char *temp = new char[res.strLength];
    strcpy(strcpy(temp,strPtr) + temp_len,rightStr.strPtr);
    res.strPtr = temp;
    return res;
}

newString& newString::operator+=(const newString& rightStr){
   int n = strLength;
   strLength += rightStr.strLength;
   char *temp = new char[strLength + 1];
   temp = strcpy(temp,strPtr);
   strcpy(temp+n,rightStr.strPtr);
   delete[] strPtr;
   strPtr = temp;
   return *this;
}

//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;
}

// main.cpp
#include <iostream>
#include "myString.h"

using namespace std;

int main()
{
    newString str1 = "Sunny";        //initialize str1 using
    //the assignment operator
    const newString str2("Warm");   //initialize str2 using the
    //conversion constructor
    newString str3; //initialize str3 to the empty string
    newString str4; //initialize str4 to the empty string

    cout << "Line 1: " << str1 << " " << str2
    << " ***" << str3 << "###." << endl; //Line 1

    if (str1 <= str2) //compare str1 and str2; Line 2
    cout << "Line 3: " << str1 << " is less "
    << "than or equal to " << str2 << endl;//Line 3
    else //Line 4
    cout << "Line 5: " << str2 << " is less "
    << "than " << str1 << endl; //Line 5

    cout << "Line 6: Enter a string with a length "
    << "of at least 7: "; //Line 6
    cin >> str1; //input str1; Line 7
    cout << endl; //Line 8

    cout << "Line 9: The new value of "
    << "str1 = " << str1 << endl; //Line 9

    str4 = str3 = "Birth Day"; //Line 10

    cout << "Line 11: str3 = " << str3
    << ", str4 = " << str4 << endl; //Line 11

    str3 = str1; //Line 12
    cout << "Line 13: The new value of str3 = "
    << str3 << endl; //Line 13

    str1 = "Bright Sky"; //Line 14

    str3[1] = str1[5]; //Line 15
    cout << "Line 16: After replacing the second "
    << "character of str3 = " << str3 << endl; //Line 16

    str3[2] = str2[0]; //Line 17
    cout << "Line 18: After replacing the third "
    << "character of str3 = " << str3 << endl; //Line 18

    str3[5] = 'g'; //Line 19
    cout << "Line 20: After replacing the sixth "
    << "character of str3 = " << str3 << endl; //Line 20


    newString a =  "abc";
    newString b = "def";
    a += b;
    cout<<a<<endl;


    newString pp = "rho";
    newString qq = "nfd";
    newString aa = pp + qq;
    cout<<aa<<endl;


    return 0;
}

Related Solutions

Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the...
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the widths for the 2 Rectangles and multiple the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the non-member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE. Using the program below, overload the Divide...
In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator.
2.(a) Fraction Operators Objective:In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator. Extend the Fraction class definition so that the -, * and / operators are supported. Write a main function that demonstrates usage of all of these operators.(b)More Custom Types Objective:Define a set of classes that represent a Schedule and Course in the context of a university student who has a schedule with a...
Use the Simulator (Core) and the attached skeleton class (MyPrettySimulator), and extend the framework with the following functions:
Use the Simulator (Core) and the attached skeleton class (MyPrettySimulator), and extend the framework with the following functions:HalfAdder(x,y)FullAdder(x,y,c_i)Switches.
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as...
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as a member of the class clockType. Write the definition of the function to overload the post-increment operator for the class clockType as defined in the step above. Main.cpp //Program that uses the class clockType. #include <iostream> #include "newClock.h" using namespace std; int main() { clockType myClock(5, 6, 23); clockType yourClock; cout << "Line 3: myClock = " << myClock << endl; cout << "Line...
Extend the Hello Goodbye application from the class to include the following using android studio: 1....
Extend the Hello Goodbye application from the class to include the following using android studio: 1. Add a Text Color button underneath the existing Exclamation button, using the same text color and background image. When this button is clicked, toggle the display color for the Hello or Goodbye text between the original color and the color Red. 2. Add a Reset button underneath the new text color button, using the same text color and background image. When this button is...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) //...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) // The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5. //Since type is void, it should not return an array. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag...
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT