Concept explainers
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 |
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).
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 5 images
- int class Inventory { std::vector<Item*> m_Items; static unsigned int m_ItemsMade; void CreateItem() { std::string name = "Item " + std::to_string(m_ItemsMade); m_Items.push_back(new Item(name.c_str(), 100 * m_ItemsMade)); ++m_ItemsMade; } public: Inventory() { CreateItem(); CreateItem(); CreateItem(); } void Print() const { size_t* nSize = new size_t(m_Items.size()); std::cout << "_____INVENTORY_____\n"; for (unsigned int i = 0; i < *nSize; ++i) { m_Items[i]->Print(); } } }; GetValidatedInt(const char* strMessage, int nMinimumRange = 0, int nMaximumRange = 0); This function should first display the provided message to the screen then use cin to get an int from the user. Error check and validate to ensure a legal integer was entered. If not, clear the cin buffer using clear() and ignore() and try again (Note: the buffer still needs to be cleared even if this step was successful). If a legal integer was entered, check its value to see if it is within the…arrow_forwarddictionaries = [] dictionaries.append({"First":"Bob", "Last":"Jones"}) dictionaries.append({"First":"Harpreet", "Last":"Kaur"}) dictionaries.append({"First":"Mohamad", "Last":"Argani"}) for i in range(0, len(dictionaries)): # Condition that's print the full name when Condition is True if(dictionaries[i]['First']=='Bob')or(dictionaries[i]['Last']=='Kaur'): print(dictionaries[i]['First']+" "+dictionaries[i]['Last']) ********************************** please modify the code to use a while loop instead of a for looparrow_forwardExplain term encapsulation.arrow_forward
- You will design a program that manages the inventory of an electronics store. You will need to use a number of concepts that you learned in class including: use of classes, use of dictionaries and input and output of comma delimeted csv files. Input:a) ManufacturerList.csv -- contains items listed by row. Each row contains item ID,manufacturer name, item type, and optionally a damaged indicatorb) PriceList.csv -- contains items listed by row. Each row contains item ID and the item price.c) ServiceDatesList.csv – contains items listed by row. Each row contains item ID and servicedate. Required Output:1) Interactive Inventory Query Capabilitya. Query the user of an item by asking for manufacturer and item type with a single query. i. Print a message(“No such item in inventory”) if either the manufacturer or theitem type are not in the inventory, more that one of either type is submitted orthe combination is not in the inventory. Ignore any other words, so “nice Applecomputer” is treated…arrow_forward17. Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person's name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList.arrow_forwardWrite a C#program that uses a class called ClassRegistration as outlined below: The ClassRegistration class is responsible for keeping track of the student id numbers for students that register for a particular class. Each class has a maximum number of students that it can accommodate. Responsibilities of the ClassRegistration class: It is responsible for storing the student id numbers for a particular class (in an array of integers) It is responsible for adding new student id numbers to this list (returns boolean) It is responsible for checking if a student id is in the list (returns a boolean) It is responsible for getting a list of all students in the class (returns a string). It is responsible for returning the number of students registered for the class (returns an integer) It is responsible for returning the maximum number of students the class is allowed (returns an integer) It is responsible for returning the name of the class. (returns a string) ClassRegistration -…arrow_forward
- Given the previous Car class, the following members have been added for you: Private: string * parts; //string array of part names int num_parts; //number of parts Public: void setParts(int numpart, string newparts[]) //set the numparts and parts int getNumParts() string * getParts() string getPart(int index) //return the string in parts at index num Please implement the following for the Car class: Add the copy constructor. (deep copy!) Add the copy assignment operator. (deep copy!) Add the destructorarrow_forwardRules: Corner cases. By convention, the row and column indices are integers between 0 and n − 1, where (0, 0) is the upper-left site. Throw an IllegalArgumentException if any argument to open(), isOpen(), or isFull() is outside its prescribed range. Throw an IllegalArgumentException in the constructor if n ≤ 0. Unit testing. Your main() method must call each public constructor and method directly and help verify that they work as prescribed (e.g., by printing results to standard output). Performance requirements. The constructor must take Θ(n^2) time; all instance methods must take Θ(1)Θ(1) time plus Θ(1)Θ(1) calls to union() and find().arrow_forwardclass Artist{StringsArtist(const std::string& name="",int age=0):name_(name),age_(age){}std::string name()const {return name_;}void set_name(const std::string& name){name_=name;}int age()const {return age_;}void set_age(int age){age_=age;}virtual std::string perform(){return std::string("");}private:int age_;std::string name_;};class Singer : public Artist{public:Singer():Artist(){};Singer(const std::string& name,int age,int hits);int hits() const{return hits_;}void set_hits(int hit);std::string perform();int operator+(const Singer& rhs);int changeme(std::string name,int age,int hits);Singer combine(const Singer& rhs);private:int hits_=0;};int FindOldestandMostSuccess(std::vector<Singer> list); Task: Implement the function int Singer::changeme(std::string name,int age,int hits) : This function should check if the values passed by name, age and hits are different than those stored. And if this is the case it should change the values. This should be done by…arrow_forward
- Written in Python It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Docstrings for modules, functions, classes, and methodsarrow_forwardclass Artist{StringsArtist(const std::string& name="",int age=0):name_(name),age_(age){}std::string name()const {return name_;}void set_name(const std::string& name){name_=name;}int age()const {return age_;}void set_age(int age){age_=age;}virtual std::string perform(){return std::string("");}private:int age_;std::string name_;};class Singer : public Artist{public:Singer():Artist(){};Singer(const std::string& name,int age,int hits);int hits() const{return hits_;}void set_hits(int hit);std::string perform();int operator+(const Singer& rhs);int changeme(std::string name,int age,int hits);Singer combine(const Singer& rhs);private:int hits_=0;};int FindOldestandMostSuccess(std::vector<Singer> list); Implement the function Singer Singer::combine(const Singer& rhs):It should create a new Singer object, by calling the constructor with the following values: For name it should pass a combination of be the name of the calling object followed by a '+' and then…arrow_forwardWrite a C# program that uses a class called ClassRegistration as outlined below: The ClassRegistration class is responsible for keeping track of the student id numbers for students that register for a particular class. Each class has a maximum number of students that it can accommodate. Responsibilities of the ClassRegistration class: It is responsible for storing the student id numbers for a particular class (in an array of integers) It is responsible for adding new student id numbers to this list (returns boolean) It is responsible for checking if a student id is in the list (returns a boolean) It is responsible for getting a list of all students in the class (returns a string). It is responsible for returning the number of students registered for the class (returns an integer) It is responsible for returning the maximum number of students the class is allowed (returns an integer) It is responsible for returning the name of the class. (returns a string)arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education