Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Classes, Objects, Pointers and Dynamic Memory

Program Description: This assignment you will need to create your own string class. For the name of the class, use your initials from your name. 

The MYString objects will hold a cstring and allow it to be used and changed. We will be changing this class over the next couple programs, to be adding more features to it (and correcting some problems that the program has in this simple version).

Your MYString class needs to be written using the .h and .cpp format.

Inside the class we will have the following data members:

Member Data
Description
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)

The class will store the string in dynamic memory that is pointed to with the pointer. When you first create an MYString object you should allocate 20 spaces of memory (using the new command). The string will be stored as a cstring in this memory.  

For this program, your MYString variables will never need to grow beyond length 20, in program 3 you will need to be allow your string that is held in the class to be able to be larger than 20 chars.  So you may want to start allowing your strings to be able to grow.....if you have extra time.  If you are optionally building in the ability to grow, it should start with a capacity of 20, but when needed it would grow in increments of 20.  The capacity should always be a multiple of 20.Dynamic array:

The MYString class will need to have the following member functions: 

Programming Note: Write and test one or two functions at a time
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

Main Program Requirements:

    • create a vector of MYStrings that is size 100
    • read each of the words from the file called "infile2.txt" (the file is out in Files section under Program Resources).  You can call the read function directly on the indexed vector<MYString>.  (do not use vector's push_back function.  See programming note below*).  Example:   while ( words[count].read(fin) )  {...}
    • as you are reading the words in, keep a count of how many words were read in.
    • After you have read in all the words from the file, resize your vector to the correct size based on your count of the number of words read in.
    • sort the MYStrings from smallest to largest (this will be based on the ASCII encoding....meaning capital letters will sort before lower case letters) using Bubble Sort
    • output the sorted words to outfile.txt file
      • 6 words per line ( use setw(13), which is part of <iomanip> library, to space them out....the setw command should not be in the write member function). 

 

*Programming Note:  Do not use the push_back() member function of vector, because this won't work for this program (it calls the copy constructor of our MYString class, which we haven't written).

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education