In: Computer Science
Complete the following:
Extend the newString class (attached) to include the following:
//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;
}
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;
}