SuperTutor

(15)

$15/per page/Negotiable

About SuperTutor

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Business & Finance See all
Accounting,Business & Finance,Economics,Engineering,HR Management,Math Hide all
Teaching Since: Apr 2017
Last Sign in: 235 Weeks Ago, 2 Days Ago
Questions Answered: 12843
Tutorials Posted: 12834

Education

  • MBA, Ph.D in Management
    Harvard university
    Feb-1997 - Aug-2003

Experience

  • Professor
    Strayer University
    Jan-2007 - Present

Category > Programming Posted 06 May 2017 My Price 15.00

C++ Programming Homework 3

C++ Programming
Homework 3
Write a String class which will be a wrapper class to the C style strings. The strings will be of varying
logical lengths, but will have a fixed physical (maximum) length of MAXLEN (defined to be 128
characters).
● (40 points) Your String class must implement all the appropriate methods including constructors,
assignment, equality operators, the index operator , reverse, indexOf (find), print, and read. These
public methods should check MAXLEN to see if the physical capacity has been exceeded. If so, print
an error message and do something reasonable to recover so the program can continue running.
● (40 points) Do not use any of the C ​ str​ functions (e.g. ​strcmp​, ​strlen​, or ​strcpy​), however write them
yourself, as static methods, then use your static methods. REMEMBER: never copy/paste code from
any source - you must write everything yourself. ​ These str functions should not check MAXLEN​.
● File organization​: Put the full definition of class String in a file named String.h. Put your main program
and testing functions in a file named string_test.cpp. string_test.cpp must #include String.h. You can
compile your program with the comand g++ string_test.cpp -o string_test . You can run it by calling
string_test
● Class String declaration:
#define MAXLEN 128
class String
{
public:
/// Both constructors should construct
/// this String from the parameter s
explicit String( const char * s = "");
String( const String & s );
String operator = ( const String & s );
char & operator ( int index );
int size();
String reverse(); // does not modify this String
int indexOf( const char c );
int indexOf( const String pattern );
bool operator == ( const String s );
bool operator != ( const String s );
bool operator > ( const String s );
bool operator < ( const String s )
bool operator <= ( const String s );
bool operator >= ( const String s );
/// concatenates this and s to return result
String operator + ( const String s );
/// concatenates s onto end of this string
String operator += ( const String s );
void print( ostream & out );
void read( istream & in );
~String();
private:
● bool inBounds( int i )
{
return i >= 0 && i < strlen(buf);
}// HINT: some C string primitives you should define and use
static int ​strlen​( const char *s );
static char * ​strcpy​( char *dest, const char *src );
static char * ​strcat​(char *dest, const char *src);
static int ​strcmp​( const char *left, const char *right );
static int ​strncmp​( const char *left, const char *right, int n );
static char * strchr( char *str, int c );
/* haystack “The quick brown fox ran up the lazy log”
needle “ran” */
static const char * strstr( const char *haystack, const char *needle );
static char * strstr( char *haystack, const char *needle );
​char buf[MAXLEN];​ // array for the characters in this string
// DO NOT store the ‘logical’ length of this string
// use the null ‘\0’ terminator to mark the end
};
ostream & operator << ( ostream & out, String str );
istream & operator >> ( istream & in, String & str );
(20 points) Write a main function which tests each public method defined in your class String. Give at
least two and at most 4 tests for each method. Good organization would be something like the following:
void test_constructor_and_print()
{
String s(“Hello World”);
cout << s << endl;
}
int main()
{
test_constructor_and_print();
test_assignment();
// ...
}
● First, read this carefully. If you don’t understand something, read it again, but change your focus so you are paying attention.
Follow each link and read the relevant part of what is linked.
You should think how you are going to get from an empty class definition to one that is fully functional. The foolish approach
is to code it all up, then start debugging. You don’t have a working program until it is nearly complete.
The best approach is a combination of “​test driven development​” and "stepwise refinement" AKA “​top-down programming​”
where you identify the minimum functionality you need to test, then write a test case for that, then implement the functionality
until the test performs correctly. Then you identify the next feature to add, write the test, implement the feature, test, etc.
Here is an abstract example​. Note, in practice, we often use a combination of bottomup and top-down programming. In this
assignment, we start bottom-up by identifying some useful operations: strcpy(), strlen(), strcmp(), etc. Then we move to top-down and implement each String method, but keep in mind the operations we defined and wrote bottom-up. E.g., use
strcpy() to write the constructors and use strcmp() to write the relational operators..
For example, if you are writing class String, to write the simlest test program, you need functioning constructors and a
functioning print method. First write a simple main function that declares a String and initializes it via the constructor, then
print out the string to see if it was constructed correctly. Now go write both constructors and print and operator <<.
To write the constructors, you need the utility function, strcpy(), which is defined as a static method (static means it has no
this parameter, but is still a member function so it can access private parts of strings). Here is a description of how strcpy
should behave with examples of use ​http://www.cplusplus.com/reference/cstring/strcpy/
Test your program until it is functioning correctly. Note, you may also need the copy constructor if any strings are passed as
parameter by copy or returned by copy (as opposed to by reference).
The copy constructor for a class that takes one parameter that is a reference to an instance of the class. It may or may not
be const as well. Constructors are responsible for building this string to look just like the c-string or string object they are
being constructed from. strcpy() should do the work. Both constructors should call strcpy to copy the characters from the
parameter into this buf.
Next, you can think about writing operator = or operator ==, but whatever you decide to write next, first add the test case to
your main function, then implement the new function and test it. The relationals and equality operators can use strcmp() to do
the hard work. You can read about any of these functions via google because they are standard C functions, but we are
writing them ourselves to use in implementing our string class.
One more major suggestion, do not call strlen() unless absolutely necessary. It is expensive, O(N), and is not the way to
iterate through a c-string with for loops. I showed you how to iterate through c-strings using the test for the null terminating
character. That is the correct and efficient way to do it. Note it is difficult to write reverse() without first calling strlen().
Another thing to avoid is calling strlen in the terminating condition of a loop, e.g.,
void f(char *s)
{
for (int i=0; i < strlen(s); ++i)
do something with(s[i]);
}
Note if N is the number of charaacters in the string s, this has just turned this functin f into an O(N^2) function! You can easily
convert it back to O(N) by factoring out the call to strlen() as follows:
void f(char *s)
{
int len = strlen(s);
for (int i=0; i < len; ++i)
do something with(s[i]);
}
but this is still making two passes down the string s. An even better solution is to not call strlen() at all and use the null string
terminator to end the loop.
void f(char *s)
{
for (int i=0; s[i] != ‘\0’; ++i)
do something with(s[i]);
}

 

Answers

(15)
Status NEW Posted 06 May 2017 03:05 AM My Price 15.00

-----------

Attachments

file 1494040344-Solutions file.docx preview (51 words )
S-----------olu-----------tio-----------ns -----------fil-----------e -----------Hel-----------lo -----------Sir-----------/Ma-----------dam----------- T-----------han-----------k y-----------ou -----------for----------- yo-----------ur -----------int-----------ere-----------st -----------and----------- bu-----------yin-----------g m-----------y p-----------ost-----------ed -----------sol-----------uti-----------on.----------- Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m o-----------nli-----------ne -----------or -----------inb-----------ox -----------me -----------a m-----------ess-----------age----------- I -----------wil-----------l b-----------e q-----------uic-----------kly----------- on-----------lin-----------e a-----------nd -----------giv-----------e y-----------ou -----------exa-----------ct -----------fil-----------e a-----------nd -----------the----------- sa-----------me -----------fil-----------e i-----------s a-----------lso----------- se-----------nt -----------to -----------you-----------r e-----------mai-----------l t-----------hat----------- is----------- re-----------gis-----------ter-----------ed -----------on-----------th-----------is -----------web-----------sit-----------e -----------Tha-----------nk -----------you----------- -----------
Not Rated(0)