Question: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = Hello s2 = there s1

Overload the operator + to perform string concatenation.

Overload the operator += to work as shown here:

s1 = "Hello "

s2 = "there"

s1 += s2 // Should assign "Hello there" to s1

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

//Header file myString.h #ifndef H_myString #define H_myString

#include

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

#include #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; }

//Implementation file myStringImp.cpp #include #include #include #include #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; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!