M
Mike Jones
Program description:
Write a C++ class called STRING. Place the declaration of the class
in the header file, and place the method definitions and friend
function definitions in the corresponding cpp file. Also download the
main function which is provided here, and place it in a separate file
from the STRING class. You must not change the main function itself,
although you will need to modify the first #include instruction so
that your program compiles and links.
The STRING class is similar to the string class provide in the
<string> library, and all operations of class STRING should work
identically to those of string, except where explicitly stated
otherwise below. However, your program is not permitted to include
<string>, nor any other libraries except <iostream>.
The STRING class must provide the following constructors, operations,
and functions.
Members and friends Meaning Example usage
STRING( ) Default constructor creates an empty STRING STRING s;
STRING(char) Constructor creates a STRING of length one with the given
character; not provided in <string> library STRING t('x');
STRING(char *) Constructor takes a NULL-terminated character array of
arbitrary size and creates a STRING with the same size and characters
STRING u("xyz");
STRING(STRING&) Copy constructor STRING v(u);
<<(ostream&, STRING&) Output operator cout << v << endl;characters; then reads input until reaching the following blank, tab,
or newline character cin >> s;
getline(istream&, STRING&) Reads input until reaching the next newline
character getline(cin, s);
STRING = STRING Assignment operator s=t;
assign(STRING) Assignment method; same as = s.assign(t);
STRING + STRING Concatenation returns a STRING s=t+u;
STRING += STRING Concatenation and assignment operator s+=v;
append(STRING) Concatenation and assignment method; same as +=
s.append(v);
STRING == STRING Equals comparison returns boolean if (s==t) ...
STRING != STRING Not-equals comparison returns boolean if (s!=t) ...
STRING < STRING Less-than comparison returns boolean if (s<t) ...
STRING <= STRING Less-or-equal comparison returns boolean if (s<=t)
....
STRING >= STRING Greater-or-equal comparison returns boolean if (s>=t)
....
STRING > STRING Greater-than comparison returns boolean if (s>t) ...
length( ) Returns an integer that represents the current number of
characters in the string int k=s.length( );
at(int) Returns the character at the specified index if within the
appropriate range; otherwise error char c=s.at(0);
put(int, char) Opposite of at; sets the character at the specified
index if within the appropriate range; not provided in <string>
library s.put(1,'x');
operator[ ](int) Returns a character reference (char &); can be used
instead of either at or put c=s[0]; s[1]='x';
find(STRING) Returns the first index where the given substring begins
within string; returns -1 if not found k=s.find(t);
find(STRING, int) Search starts at the specified position; returns the
next index where the given substring begins within string; returns -1
if not found k=s.find(t,2);
substr(int, int) Returns the substring with specified initial position
and length t=s.substr(2,3);
erase(int, int) Removes the substring with specified initial position
and length from the string s.erase(2,3);
insert(int, STRING) Adds the given substring at the specified initial
position within the string s.insert(2,"abcd");
replace(int, int, STRING) Changes the substring with specified initial
position and length to the given substring; equivalent to erase
followed by insert s.replace(2,3,"abcd");
Hints:
Note that friend functions can implicitly convert their parameter
types if the necessary constructor is provided. The specified STRING
constructors can convert either a char or a char* into a STRING.
Hence if operator STRING+STRING is provided as a friend function, then
each of the following should work automatically: char+STRING,
STRING+char, char*+STRING, STRING+char*. Similar statements can also
be made about many of the other functions and operators above.
Your STRING class should have an integer field that represents the
current length, and also a dynamically allocated character array
field. Allocate a new character array of the appropriate length
within each constructor, assignment, concatenation, and other
functions that create new STRING values. If you wish, you may also
provide a destructor for class STRING, but that is not required for
this project.
Write a C++ class called STRING. Place the declaration of the class
in the header file, and place the method definitions and friend
function definitions in the corresponding cpp file. Also download the
main function which is provided here, and place it in a separate file
from the STRING class. You must not change the main function itself,
although you will need to modify the first #include instruction so
that your program compiles and links.
The STRING class is similar to the string class provide in the
<string> library, and all operations of class STRING should work
identically to those of string, except where explicitly stated
otherwise below. However, your program is not permitted to include
<string>, nor any other libraries except <iostream>.
The STRING class must provide the following constructors, operations,
and functions.
Members and friends Meaning Example usage
STRING( ) Default constructor creates an empty STRING STRING s;
STRING(char) Constructor creates a STRING of length one with the given
character; not provided in <string> library STRING t('x');
STRING(char *) Constructor takes a NULL-terminated character array of
arbitrary size and creates a STRING with the same size and characters
STRING u("xyz");
STRING(STRING&) Copy constructor STRING v(u);
<<(ostream&, STRING&) Output operator cout << v << endl;characters; then reads input until reaching the following blank, tab,
or newline character cin >> s;
getline(istream&, STRING&) Reads input until reaching the next newline
character getline(cin, s);
STRING = STRING Assignment operator s=t;
assign(STRING) Assignment method; same as = s.assign(t);
STRING + STRING Concatenation returns a STRING s=t+u;
STRING += STRING Concatenation and assignment operator s+=v;
append(STRING) Concatenation and assignment method; same as +=
s.append(v);
STRING == STRING Equals comparison returns boolean if (s==t) ...
STRING != STRING Not-equals comparison returns boolean if (s!=t) ...
STRING < STRING Less-than comparison returns boolean if (s<t) ...
STRING <= STRING Less-or-equal comparison returns boolean if (s<=t)
....
STRING >= STRING Greater-or-equal comparison returns boolean if (s>=t)
....
STRING > STRING Greater-than comparison returns boolean if (s>t) ...
length( ) Returns an integer that represents the current number of
characters in the string int k=s.length( );
at(int) Returns the character at the specified index if within the
appropriate range; otherwise error char c=s.at(0);
put(int, char) Opposite of at; sets the character at the specified
index if within the appropriate range; not provided in <string>
library s.put(1,'x');
operator[ ](int) Returns a character reference (char &); can be used
instead of either at or put c=s[0]; s[1]='x';
find(STRING) Returns the first index where the given substring begins
within string; returns -1 if not found k=s.find(t);
find(STRING, int) Search starts at the specified position; returns the
next index where the given substring begins within string; returns -1
if not found k=s.find(t,2);
substr(int, int) Returns the substring with specified initial position
and length t=s.substr(2,3);
erase(int, int) Removes the substring with specified initial position
and length from the string s.erase(2,3);
insert(int, STRING) Adds the given substring at the specified initial
position within the string s.insert(2,"abcd");
replace(int, int, STRING) Changes the substring with specified initial
position and length to the given substring; equivalent to erase
followed by insert s.replace(2,3,"abcd");
Hints:
Note that friend functions can implicitly convert their parameter
types if the necessary constructor is provided. The specified STRING
constructors can convert either a char or a char* into a STRING.
Hence if operator STRING+STRING is provided as a friend function, then
each of the following should work automatically: char+STRING,
STRING+char, char*+STRING, STRING+char*. Similar statements can also
be made about many of the other functions and operators above.
Your STRING class should have an integer field that represents the
current length, and also a dynamically allocated character array
field. Allocate a new character array of the appropriate length
within each constructor, assignment, concatenation, and other
functions that create new STRING values. If you wish, you may also
provide a destructor for class STRING, but that is not required for
this project.