need help with strings and classes please!!!

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.
 
M

Mike Wahler

Mike Jones said:
Program description:

[snip class assignment]

OK you asked for help. As soon as you show us some
code you need help with, we'll help.

We will *not* do your work for you.

-Mike
 
A

Alf P. Steinbach

* (e-mail address removed) (Mike Jones) schriebt:
Program description:

Write a C++ class called STRING.

Right, your teacher needs a short intro course in C++. All uppercase
names should be and are conventionally reserved for macros. Print out
this reply and hand it to your teacher.
 
J

John Harrison

Mike Jones said:
Program description:

You need help, but what with? Which part of the assignment don't you
understand or not know how to do? Maybe you don't know where to start?

Just posting a whole assignment and saying 'help me please' isn't going to
get you much useful help. How can anyone help when they don't know the first
thing about your abilities.

Best advice is to pick a VERY SMALL part of the assignment and have a go at
that SMALL part, this will mean writing your own main function instead of
using the one you have been given (forget about that for now). When you get
stuck (which might be very quickly) post the code you have written here.
Seeing code you have written will tell the regulars here what ability you
have and what advice is appropriate.

john
 
O

osmium

Mike said:
Program description:
<snip>

Fondle the descriptions. See if you can see some order or hierarchy to what
needs to be done. See if you can factor something. For example, can += and
= share some code? (I don't know if those are even in the description but
that is the *kind* of thing to look for. How about == and !=? See if you
like the grouping they already have. Do that and then start coding, expect
to do a fair amount of backtracking.

Hint: store the datum as a C (null-terminated) string.
 
G

Gary

Mike Jones said:
Program description:
<<large snip>>

So. Where does this assignment come from? Looks like a complete description
of a string handling class. What have you done so far?
I'm interested as to the school, course, and length of time given for this
assignment.
 
D

David Harmon

Hint: store the datum as a C (null-terminated) string.

I disagree; there is nothing suggesting that a zero character is not a
valid element of the string, just as with std::string. If you use zero
as a termination rather than keeping a length or a pointer to the end,
you will have trouble when the actual data contains a zero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top