Question: You are given two files mystring1.h the document and mystring1.cpp the document which define a String class implemented by using static array as the data
You are given two files mystring1.h the document and mystring1.cpp the document which define a String class implemented by using static array as the data member. The purpose of this assignment is to rewrite this class by replacing the static array with a pointer (dynamic array). You are required to modify the String class accordingly:
Data: we wish to allow our String class to accommodate strings of different sizes. To that end, instead of storing a strings characters in a fixed-size array, each String object should contain the following data as part of its internal representation:
(1) a character pointer meant to point to a dynamically-allocated array of characters.
(2) a length field that will hold the current length of the string at any given moment.
Operations: same as the methods in String class, plus the big three( destructor, copy constructor, and assignment operator overloading).
Modularity: The String class code must be put in its own module, i.e., a file mystring2.h (the header file) should hold the class definition (use conditional compilation directives) and a file mystring2.cpp (the implementation file) should hold the method definitions.
************************************************************************************************************
For this program we have to file the code in the template header file and main file.
*************************************************************************************************************
//File: mystring1.h
// ================
// Interface file for user-defined String class.
#include"mystring1.h
String::String()
{
contents[0] = '\0';
len = 0;
}
String::String(const char s[])
{
len = strlen(s);
strcpy(contents, s);
}
void String::append(const String &str)
{
strcat(contents, str.contents);
len += str.len;
}
bool String::operator ==(const String &str) const
{
return strcmp(contents, str.contents) == 0;
}
bool String::operator !=(const String &str) const
{
return strcmp(contents, str.contents) != 0;
}
bool String::operator >(const String &str) const
{
return strcmp(contents, str.contents) > 0;
}
bool String::operator <(const String &str) const
{
return strcmp(contents, str.contents) < 0;
}
bool String::operator >=(const String &str) const
{
return strcmp(contents, str.contents) >= 0;
}
String String::operator +=(const String &str)
{
append(str);
return *this;
}
void String::print(ostream &out) const
{
out << contents;
}
int String::length() const
{
return len;
}
char String::operator [](int i) const
{
if (i < 0 || i >= len) {
cerr << "can't access location " << i
<< " of string \"" << contents << "\"" << endl;
return '\0';
}
return contents[i];
}
ostream & operator<<(ostream &out, const String & s) // overload ostream operator "<<" - External!
{
s.print(out);
return out;
}
*************************************************************************************************************
//File: mystring1.h
// ================
// Interface file for user-defined String class.
#ifndef _MYSTRING_H
#define _MYSTRING_H
#include
#include
using namespace std;
#define MAX_STR_LENGTH 200
class String {
public:
String();
String(const char s[]); // a conversion constructor
void append(const String &str);
// Relational operators
bool operator ==(const String &str) const;
bool operator !=(const String &str) const;
bool operator >(const String &str) const;
bool operator <(const String &str) const;
bool operator >=(const String &str) const;
String operator +=(const String &str);
void print(ostream &out) const;
int length() const;
char operator [](int i) const; // subscript operator
private:
char contents[MAX_STR_LENGTH+1];
int len;
};
ostream & operator<<(ostream &out, const String & r); // overload ostream operator "<<" - External!
#endif /* not defined _MYSTRING_H */
*************************************************************************************************************
/**
* cmpsc122 Assignment test file
* File Name: mystringDriver.cpp
*
* Description: This program demonstrates a basic String class that implements
* dynamic allocation and operator overloading.
*
*/
#include
#include "mystring2.h
using namespace std;
/************************ Function Prototypes ************************/
/*
* Function: PrintString
* Usage: PrintString(str);
*
* Prints out the value and length of the String object passed to it.
*/
void PrintString(const char *label,
const String &str); // overloaded ostream operator << is used in the definition.
/*************************** Main Program **************************/
int main()
{
String str1, str2("init2"), str3 = "init3"; // Some String objects. Using constructor for copy
char s1[100], s2[100], s3[100]; // Some character strings.
// Print out their initial values...
cout << "Initial values:" << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Store some values in them...
cout << " Enter a value for str1 (no spaces): ";
cin >> s1;
str1 = s1;
cout << " Enter a value for str2 (no spaces): ";
cin >> s2;
str2 = s2;
cout << " Enter a value for str3 (no spaces): ";
cin >> s3;
str3 = s3;
cout << " After assignments..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Access some elements...
int i;
cout << " Enter which element of str1 to display: ";
cin >> i;
cout << "Element #" << i << " of str1 is '" << str1[i]
<< "'" << endl;
cout << " Enter which element of str2 to display: ";
cin >> i;
cout << "Element #" << i << " of str2 is '" << str2[i]
<< "'" << endl;
cout << " Enter which element of str3 to display: ";
cin >> i;
cout << "Element #" << i << " of str3 is '" << str3[i]
<< "'" << endl;
// Append some strings...
cout << " Enter a value to append to str1 (no spaces): ";
cin >> s1;
// str1.append(s1); // Actually, the cstring is converted to String object here by the constructor
str1 += s1; // same as above
cout << " Enter a value to append to str2 (no spaces): ";
cin >> s2;
str2 += s2;
cout << " Enter a value to append to str3 (no spaces): ";
cin >> s3;
str3 += s3;
cout << " After appending..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Compare some strings...
cout << " Comparing str1 and str2..." << endl;
cout << "\"";
cout<< str1; // test the overloading of ostream operator <<
cout << "\" is ";
if (str1 < str2) { // test the overloading of comparison operator <
cout << "less than";
} else if (str1 > str2) {
cout << "greater than";
} else {
cout << "equal to";
}
cout << " \"";
cout << str2;
cout << "\"" << endl;
cout << " test the = operator, after str1 = str2; "<< endl;
str1 = str2;
PrintString("str1", str1);
PrintString("str2", str2);
str1 += s1;
cout << " After str1 = str1 + s1: "<< endl;
PrintString("str1", str1);
PrintString("str2", str2);
String str4(str3);
cout << " test the copy constructor, after str4(str3);"<< endl;
PrintString("str3", str3);
PrintString("str4", str4);
cout << " after appending str3 by str2" << endl;
str3 += str2;
PrintString("str3", str3);
PrintString("str4", str4);
cout<< " str2, str4 are not changed. Type any letter to quit." << endl;
char q;
cin >> q;
return 0;
}
/*********************** Function Definitions **********************/
void PrintString(const char *label,
const String &str)
{
cout << label << " holds \"";
cout << str; // << is overloaded
cout << "\" (length = " << str.length() << ")" << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
