Question: Now that you have seen what we can do by creating a relatively thin object wrapper around a raw C++ array, apply that to create

Now that you have seen what we can do by creating a relatively thin object wrapper around a raw C++ array, apply that to create a safer (if not friendlier) version of a character string.

Create the header file StringWrapper.h and implementation file StringWrapper.cpp. You will be defining a class StringWrapper using the techniques youve just applied, but with a raw character array (C-Style string) as the underlying storage unit instead of an integer array. Your library should meet the following requirements:

  • Add code to your main program to test the functionality of StringWrapper as you go. Remember: Compile and test often!
  • Allow the underlying maximum capacity to be 1048576 bytes (1MiB in bytes = 10242 bytes). Since 1 char is 1 byte, the maximum capacity is exactly 1048576 characters. Use a const static attribute for this as you did for the ArrayWrapper earlier.
  • There is no need to also store a logical size attribute like we did with the integer array. The storage will be a valid C-Style string (null-terminated), so it we can use functions intended for C-Strings with it.
  • The constructor should take a const-qualified C-Style string as its argument. Use the strncpy() function from the library to copy it into the underlying storage. Be sure to manually null-terminate the attribute after you copy to assure that it is a valid C-String (in case the parameter contained a much larger string).
  • The get() and set() methods work similarly to the ones you wrote above they will allow the user to read/write individual characters in the string. Use the strlen() function to verify that the index they supply is not outside the logical bounds of the underlying string. Throw a std::out_of_range exactly as we did in the ArrayWrapper class if the index is invalid.
  • Add a write() method matching the prototype shown below. The write method will take an output stream as an (optional) parameter and write the string into that stream. If no argument is given, the stream parameter will default to std::cout.
  • Add a readline() method matching the prototype shown below. The first parameter is an input stream (defaulting to std::cin) and the second parameter is a delimiter (defaulting to the newline). The method will use the .getline() method belonging to the input stream to read from that stream and store the resulting input in the underlying C-string attribute. Be sure to control the maximum size correctly to prevent overflowing the underlying storage.

Prototypes for write() and readline():

write():

void write(std::ostream& strm=std::cout) const;

readline():

void readline(std::istream& strm=std::cin, char delim=' ');

*Especially for write(), readline() methods is confusing for me.

Please someone answer the question as soon as possible.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!