Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Implement the Set class and write a main driver program to allow for the instantiation of mutliple set objects. The driver program should

In C++

Implement theSet classand write amain driver programto allow for the instantiation of mutliple set objects. The driver program should show correct use of each of the implemened set operations. Your program should:

Instantiate several objects of class Set.Note that objects should be constructed using each of the constructors implemented.

Apply each of the set operations {+,^,-,==,!=} on the objects created.

Show that new elements can be added to existing sets using the + operator.

Show that elements can be removed from existing sets using the - operator.

TheSetclass should be designed around the following characteristics:

1.The name of the class should beSet.

2.The class should be composed ofat leastthe following (data)members:

set, pointer to a dynamically allocated array of integers. This array will contain the elements that comprise the set.

pSize, data member to represent the physical size of the array.

numElements, data member to represent the number of elements in the set.

name, data member to represent the name of the set.Note that a set is typically named using an uppercase letter of the English alphabet.

DEFAULT_SIZE, (static constant) data member torepresent the default value used to dynamically allocate memory. Note that you can choose to set the default size to zero or some other value greater than zero.

Note that you may need to add additional data members to accomplish the objective of the driver program. Also, make sure that the data type of each member is appropriate to the type of data being stored.

3.The class should includeat leastthe followingmethods:

Set( int = DEFAULT_SIZE ) Default constructor to initialize the set to the empty set. In other words, a set that contains no elements.Note that this constructor should dynamically allocate as much memory as specified. If a specific size is not passed, the DEFAULT_SIZE should be used.

Set( int [], int, char ) Constructor to initialize the set from the array of integers passed to the constructor. The arguments to this constructor are the array of integers, size of the passed array, name of the set.

Set( Set ) Copy constructor to initialize the object from an existing object.

Destructor, clear out data members as necessary and deallocate all dynamically allocated memory as necessary.

A method to populate the set of integers from user input. This method can prompt the user for the number of elements they want to enter into the set. If the number of elements they want to enter exceeds the physical size of the array, any existing memory should be deallocated and the required amount reallocated.

A method to display the set. Note that the set should be displayed using proper set notation ( i.e. { 1, 5, 7 } ).

Operator overloaded methods for the input and display method respectively.

Mutator methods for each data member or logical groupings, as necessary.

Methods to perform each of the following set operations:

union( Set ) This method creates a new set that represents the union of the two sets (i.e. the set of the object this methods was invoked on with the set of the object passed to the method). This method returns the new set.

intersection( Set ) This method creates a new set that represents the intersection of the two sets and returns the new set.

difference( Set ) This method creates a new set that represetns the difference of the two sets and returns the new set.

equality( Set ) This method determines whether the two sets are equal and return true or false accordingly.

inequalty( Set ) This method determines whether the two sets are not equal and returns true or false accordingly.

Operator overloaded methods for each of the above set operations as follows:

+to represent the union of two sets.

^to represent the intersection of two sets.

-to represent the difference of two sets.

==to represent the equality of two sets.

!=to represent the inequality of two sets.

Note the set operations {+,^,-} each result in a new set being created. Think carefully how you can determine the memory requirements of the array of the new set.

element( int ) This method determines if the specified integer is an element of the set and returns true or false accordingly.

A method to add a new element to an existing set and return {true, false} accordingly.Note that by definition an element of a set cannot be repeated, therefore, this method must first ensure that the element being added is not already an element of the set. If the element cannot be added, the method should return false to denote this.Further note that this method should be able to add a new element to the set even if the physical array is at capacity (i.e. number of elements equals the physical size) when the method is invoked.

A method to remove an element from an existing set and return {true, false} accordingly.Note that for an element to be removed, it must be an element of the set.

Operator overloaded methods for theaddandremovemethod as follows:

+to represent addition of a new element to the set.

-to represent deletion of an element from the set.

Note you can choose to directly implement each of the specified operator overloaded methodsorimplement the methods {input, display, union, intersection, difference, equality, inequality, add, remove} asprivatemethods which are invoked by their respective overloaded method.

4.The class should allow for the instantiation of constant objects. In other words applications which use this class should be able to create sets as const.

5.Write a simple main function to instantiate several set objects for the purpose of testing out the various methods of the class. This main function can be defined in the class source file, but make sure it is commented (or removed) after the class has been tested.

The main driver program should look like this:

// FILE COMMENT BLOCK//// External references (i.e. include statements)// Using directives//// Class Definition////// Sample Driver Function//// Note that your program should re-display the// set each time the set is altered in any fashion!//main() { // create two arrays of integers // Instantiate two sets {A, B} using each of the integer arrays previously declared // Declare a set C using the default constructor // Input elements into this set from external user input // using the >> operator as // // cin >> C; // // Create a new set D from one of the above sets // Add new element(s) to one of the existing sets. // Example: // // A + element // // Remove element(s) from one the existing sets. // Example: // // C - element // Perform the set operations on any of the above objects // Note the operations must be invoked using the // appropriate operators. Example: // // Z = A + B // // where set Z is the union of sets A and B. }

Note that each time the set is altered in any way, the set should be displayed to show the operation was successful.

The defintion of the Set class should be in an individual header (.h) file and the implementation of the class's methods should be in an individual source (.cpp) file. You can write a simple main function within the .cpp file of the class for the purpose of testing your class, however, the main driver program should be in its' own individual source (.cpp) file.Note that if you write a test main in the class .cpp file, you will need to comment it out before you can link it with the main driver

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

Recommended Textbook for

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

More Books

Students also viewed these Programming questions