2021年1月18日星期一

Need Assistance With Program to Duplicate String Library

Currently in my Programming Class, we are working creating classes. In order to do so, we are making the String library using a class. To quote the assignment directly, "This assignment you will need to create your own string class. ... Your first job will be to create and test the MYString [this is the filler name used for the strong function] class. As you write each member function, you should then write some code in main to test that the member function works well. Notice in the above example I was purposely using indexes outside of the bounds of the string to check if this would cause problems. Your MYString class needs to be written using the .h and .cpp format."


We are told we need 3 data members:

char * str | pointer to dynamic memory for storing the string

int cap | size of the memory that is available to be used (start with 20 char's and then double it whenever this is not enough)

int end | index of the end of the string (the '\0' char)


In addition, we need the following member functions:

Note: This is formatted as Member Functions : return type | Description

MYString( ) | Default Constructor: creates an empty string

MYString (const char*) | creates a string that contains the information from the argument example: MYString greeting( "hello there wise one");

length( ) : int | the length of the string ( "cat" would return 3)

capacity( ) : int | the total amount of memory available for use (always 20 in this version, but in the next version, this will change)

at( int index) : char | returns the character at a certain location ( at( 0 ) for a "cat" would return 'c' ). If the index is not inside the string (negative or too large) then return '\0'

read( istream& istrm) : bool | read one string from the istream argument (could be from cin or an ifstream variable). This should work just like the >> operator. When reading in, you can assume that you will not read in a string longer than 99 characters. This function will return true if it was able to read (remember >> operator will return true if it is able to read from a file). For simplicity sake, you could create a local char array variable 100 that you first read into and then you could copy from this char array into your dynamic memory.

write( ostream& ostrm) : void | write the string out to the ostream argument, but do not add any end of line (could be cout or an ofstream variable) compareTo( const MYString& argStr) : int
compares the object string (objStr) to the argument string (argStr) by subtracting each element of argStr from objStr until a difference is found or until all elements have been compared objStr < argStr returns a negative number objStr > argStr returns a positive number objStr == argStr returns zero

c_str( ) : const char* | return a pointer to a constant cstring version of the MYString object. This can be nice for simple outputs: cout << objStr.c_str( ) << endl; // displays var contents to display

setEqualTo(const MYString& argStr): void | this does the assignment operation objStr.setEqualTo( argStr ) would change objStr so that it would contain the same information as argStr

I know some people come to this website looking for the exact code that they would need in order to finish to problem, but I just need to know where I should start on each of the member functions. This is only my second quarter of Computer Science, and unfortunatley I needed to take two quarters off in order to meet credit requirements, so I'm having trouble recalling all the information that I need in order to get this done. Thanks you in advance for any help you give.

https://stackoverflow.com/questions/65784577/need-assistance-with-program-to-duplicate-string-library January 19, 2021 at 10:05AM

没有评论:

发表评论