Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please i need help Here is a C++ class definition for an abstract data type Set of strings, representing the concept of a collection of

please i need help image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Here is a C++ class definition for an abstract data type Set of strings, representing the concept of a collection of strings, without duplicates. (A web server might record unique visitors to a website using a set of strings, for example.) To make things simpler for you, the case of letters in a string matters, so that the strings Rot 1 and rot I are not considered duplicates. (When we don't want a function to change a parameter representing a value of the type stored in the set, we pass that parameter by constant reference. Passing it by value would have been perfectly fine for this problem, but we chose the const reference alternative because that will be more suitable after we make some generalizations in a later problem.) Notice that the comment for the get function implies that for a 5 -item set ss, ss.get (0,x) will copy the smallest item in the set into x (because the smallest item is greater than 0 items in the set), and ss - get (4,x) will copy the largest item (because the largest item is greater than 4 items in the set). The words greater than, smallest, etc., are all interpreted in the context of what the > operator for string indicates about the relative order of two strings: Here's an example of the swap function: Notice that the empty string is just as good a string as any other; you should not treat it in any special way: Set 8s; ss. insert ("dosa"); assert (1ss. contains ())/3 ss.insert ("tortilla"); ss. insert(" = ) ; Bs.insert (" focaceia") assert (ss, contains ()j) 35. erase( "dosa"): assert(ss.size() =a 3 b. ss.contains( tocaceia ) b6 string vy ss. contains ()) i assert (ss, get (1,v) Gi v= "focaceia* ); assert(ss.get (0,v) it v=N= When comparing items for insert, erase, contains, and get, just use the =m,I=,>, etc. operators provided for the string type by the library. These do case-sensitive comparisons, and thar's fine. Here is what you are to do: 1. Determine which member functions of the Set class should be const member functions (because they do not modify the Set), and change the class declaration accordingly. 2. As defined above, the Set class allows the client to use a set that contains only stds a strings. Someone who wanted to modify the class to contain items of another type, such as only ints or only doubles, would have to make changes in many places. Modify the class definition you produced in the previous problem to use a type alias for all values wherever the original definition used a stdrustring. A type alias is a name that is a synomym for some type; here is an example: // The following line introduces the type alias Nt 1/ for the type int; anywhere the code uses the ni II the type int. using Number = int ; int main() f Number total =0; Number x; while (cinmx) total in i cout total endl; 3 The advantage of using the type alias Number is that if we later wish to modify this code to sum a sequence of longs or of doubles, we need make a change in only one place: the using statement introducing the type alias Number. (Aside: Prior to C++11 (and still usable now), the only way to introduce a type alias was to use a typedef statement, egg. typedef int Number: Appendix A.1.8 of the textbook describes typedef,) To make the grader's life easier, we'll require that everyone use the same synonym for their type alias: You must use the name I temType, with exactly that spelling and case. Now that you have defined an interface for a set class where the item type can be easily changed, implement the class and all its member functions in such a way that the items in a set are contained in a data member that is an array. (Notice we said an array, not a pointer. It's not until problem 5 of this homework that you'll deal with a dynamically allocated array.) A set must be able to hold a maximum of DEFAULT_MAX_ITEMS items, where const int DEFAULT_MAX_ITEMS =160; Test your class for a Set of std; : strings. Place your class definition and inline function implementations (if any) in a file named set. h, and your non-inline function implementations (if any) in a file named Set.cep. (If we haven't yet discussed inline, then if you haven't encountered the topic yourself, all your functions will be non-inline, which is fine.) Except to add a dump function (described below), you must not add public data or function members to, delete functions from, or change the public interface of the Set class. You may add whatever private data members and private member functions you like, and you may declare private structs/classes inside the Set class if you like. If you wish, you may add a public member function with the signature void dunp() const. The intent of this function is that for your own testing purposes, you can call it to print information about the set; we will never call it. You do not have to add this function if you don't want to, but if you do add it, it must not make any changes to the set; if we were to replace your implementation of this function with one that simply returned immediately, your code must still work correctly. The dump function must not write to cout, but it's allowed to write to cerr. Your implementation of the Set class must be such that the compilergenerated destructor, copy constructor, and assignment operator do the right things. Write a test program named testSet. epp to make sure your Set class implementation works properly. Here is one possible (incomplete) test program: Now change (only) the type alias in Set . h so that the Set will contain unsigned longs. Make no other changes to Set. h, and make no and works properly with this alternative main routine (which again, is not a complete test of correctness): You may need to flip back and forth a few times to fix your Set. h and Set. cpp code so that the only change to those files you'd need to make to change a set's item type is to the type alias in Set, h. (When you turn in the project, have the type alias in Set. h specify the item type to be std astring.) Except in a using statement in set. h introducing a type alias and in the context of whelude " class Cardset fublie: CardSet(); bool add(unsigned long cardNumber); // Add a card number to the Cardset. Return tru // card number was actually added. int size() const; // Return the number of card nu void print () const; // Write to cout every card number in the Cardse (/ per 1 ne. Write no other text. private: // Some of your eode goes here. Your CardSet implementation must employ a data member of type Set that uses the type alias Iteatype as a synonym for unsigned long. Your CardSet implementation must employ a data member of type Set that uses the type alias ItemType as a synonym for unsigned long. (Notice we said a member of type Set, not of type pointer to Set.) Except for the using statement introducing the type alias, you must not make any changes to the set. h and set. cpp files you produced for Problem 3, so you must not add any member functions or data members to the Set class. Each of the member functions add, size, and print must delegate as much of the work that they need to do as they can to Set member functions. (In other words, they must not do work themselves that they can have Set member functions do instead.) If the compiler-generated destructor, copy constructor, and assignment operator for CardSet don't do the right thing, declare and implement them. Write a program to test your CardSet class. Name your files Cardset.h, Cardset. opp, and testcardset.cpp. The words for and while must not appear in Cardset. h or Cardset.epp, except in the implementation of cardset: apr int if you wish. The characters [ (open square bracket) and * must not appear in cardset.h or cardset. epp, except in comments if you wish. You do not have to change unsigned long to ItemType in Cardset. h and cardset. epp if you don't want to (since unlike Set, which is designed for a wide variety of item types, CardSet is specifically designed to work with unsigned longs). In the code you tum in, Cardset's member functions must not call Set: : dump. Now that you've created a set type based on arrays whose size is fixed at compile time, let's change the implementation to use a dynamically allocated array of objects. Copy the three files you produced for problem 3 , naming the new files newset. h, newset. cpp, and testnewset. cpp. Update those files by cither adding another constructor or modifying your existing constructor so that a client can do the following: Since the compiler-generated destructor, copy constructor, and assignment operator no longer do the right thing, declare them (as public members) and implement them. Make no other changes to the public interface of your class. (You are free to make changes to the private members and to the implementations of the member functions, and you may add or remove private members.) Change the implementation of the swap function so that the number of statement executions when swapping two sets is the same no matter how many items are in the sets. (You would not satisfy this requirement if, for example, your swap function caused a loop to visit each item in the sets, since the number of statements executed by all the iterations of the loop would depend on the number of items in the sets.) The character [ (open square bracket) must not appear in newset.h (but is fine in newset. cpp). Test your new implementation of the Set class. (Notice that even though the file is named newset. h, the name of the class defined therein must still be set.) Verify that your CardSet class still works properly with this new version of Set (with Itemtype being a type alias for unsigned long). You should not need to change your CardSet class or its implementation in any way, other than to \#include "newset. . " instead of "Set. . . (For this test, be sure to link with new5et. cpp, not Set. cpp.) (Before you turn in cardset . h, be sure to restore any = ineludes to "Set. h " instead of "newset. h) Turn it in By Monday, January 23, there will be a link on the class webpage that will enable you to turn in this homework. Turn in one zip file that contains your solutions to the homework problems, (Since problem 3 builds on problems 1 and 2, you will not turn in separate code for problems 1 and 2.) If you solve every problem, the zip file you turn in will have nine files (three for each of problems 3,4 , and 5 ). The files must meet these requirements, or your score on this homework will be severely reduced: - Each of the header files Set. .h, Cardset.h, and nevset. h mast have an appropriate include guard. In the files you tum in, the using statements in Set, h and newset. h must introduce Iteniype as a type alias for stdinstring. - If we create a project consisting of Set.h, Set.cpp, and testset. cpp, it must build successfully under both g 32 and either Visual C++ or clang ++. (Note: To build an executable using g32 from some, but not all, of the ecpp files in a directory, you list the cpp files to use in the command. To build an executable named req1 for this requirement, for example, you'd say g32=0 req1 set.cpp testset.cpp.) - If in Set, h we change I temType to be a type alias for unsigned long, and then create a project consisting of Set. h, Set. cpp, CardSet. h, CardSet.cpp, and testcardset.cpp, it must build successfully under both g 32 and either Visual C++ or clangt+. - If we create a project consisting of newset, h, newset. epp, and testnewset. cepp, it must build successfully under both g32 and cither Visual C++ or clang +. - If we create a project consisting of nevset. h, newset. cpp, and testSet. cpp, where in testSet. cpp we change only the ginelude "Set. h " to ennelude "newSet. h ", the project must build successfully under both g 32 and cither Visual C++ or elang+t. (If you try this, be sure to change the \#include back to "Set. h " before you turn in testset.,epp.) - The sourve files you submit for this homework must not contain the word friend or pragma of vector, and must not contain any global variables whose values may be changed during execution. (Global constants are fine.) - No files other than those whose names begin with test may contain code that reads anything from cin or writes anything to cout, except that for problem 4, cardset: : print must write to cout, and for problem 5 , the implementation of the constructor that takes an integer parameter may write a message and exit the program if the integer is negative. Any file may write to cerr (perhaps for debugging purposes); we will ignore any output written to cerr. - You must have an implementation for every member function of Set and CardSet. If you can't get a function implemented correctly, its implementation must at least build successfully. For example, if you don't have time to correctly implement Set: ; serase or Set ; ; swap, say, here are implementations that meet this requirement in that they at least allow programs to build successfully even though they might execute incorrectly: bool Set; serase(const ItemTypes value) f return true; // not correct, but at least t \} void Setirswap(sets other) f // does nothing; not correct, but at least \} - Given Set, h with the type alias for the Set's item type specifying stdr rstring, if we make no change to your set. cpp, then if we compile your set.cpp and link it to a file containing the linking must succeed. When the resulting executable is run, it must write Passed all tests and nothing more to cout and terminate normally. - If we suecessfully do the above and then in Set. h change the type alias for the Set's item type to specify unsigned long as the item type without making any other changes, recompile set, cpp, and link it to at file containing \#include "Set, h " \#include using namespace std; file containing the linking must succeed. When the resulting executable is ran, it must write Passed al1 tests and nothing more to cout and terminate normally. - Given newset. n with the type alias for the Ser's item type specifying. std a s string, if we make no change to your newset. cpp, then if we compile your newset. cpp and link it to a file containing. the linking must suceeed. When the resalting executable is run, it must write Passed all tests and nothing more to cout and terminate normally. - If we successfully do the above and then in nesset. th change the type alias for the Set's item type to specify unsigned long as the item type without making any other changes, recompile nevset . epp, and link it to a file containing Hinelude "newset. h" Ainclude \#include using namespace std; void test() f int main() f test(): cout \&e "Passed all tests" \&e endl; h the linking must succeed. When the resulting executable is run, it must write Passed all tests and nothing more to cout and terminate normally. - If we successfully do the above and then in newSet. h change the type alias for the Set's item type to specify unsigned long as the item type without making any other changes, recompile newset. cpp, and link it to a file containing the linking must succeed. When the resulting exceutable is run, it must write Passed a11 tests and nothing more to cout and terminate normally. - During execution, your program must not perform any undefined actions, such as accessing an array element out of bounds, or dereferencing a null or uninitialized pointer. - In each source file you tum in, do not comment out your implementation: you want our test scripts to see it! (Some people do this when testing other files' code because they put all their code in one project instead of having a separate project for each of problems 3, 4. and 5 , of alternatively, having one project and removing (without deleting!) some files from the project and adding them back and removing others to test various combinations of files.) Notice that we are not requiring any particular content in testset.cpp, testcardset, cpp, and testnewset, cpp, as long as thcy mest the requirements above. Of course, the intention is that you'd use those files for the test code that you'd write to convince yourself that your implementations are correct. Although we will throughly evaluate your implementations for correctness, for homeworks, unlike for projects, we will not grade the thoroughness of your test cases. Incidentally, for homeworks, unlike for projects, we will also not grade any program commenting you might do

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What lessons in OD contracting does this case represent?

Answered: 1 week ago

Question

Does the code suggest how long data is kept and who has access?

Answered: 1 week ago