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.
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_ ==...
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...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >=...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >= as member operators. The comparison operators will compare students by last name first name and id number. Also, overload << and >> as friend operators in the Student class. Overload << and >> operators in Roster class as well to output Rosters in a nice format as well as input Roster. Provide a function sort in a Roster class as a private function to...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT