Write a 'Set' template class.
NOTE: There's a "set" class in the C++ Standard Library, but don't use it.
HINT: Make use of the "Vector" class from the C++ Standard Library to help implement your Set class
A 'Set' is a collection of elements in which no two elements are equal. All elements in one set are of the same data-type. This data-type can be anything: a built-in data type, an object, or a pointer to a built-in datatype or object.
For a set whose elements are objects, the object class must have an overloaded "operator==" method, so that the Set can determine if two objects are equal.
Write the following public Set methods:
Set(); // Constructor: create empty set.
bool Add(T elmt); // add another element to the set. 'true' if added.
// T is the data-type of each element in the se.
bool Remove(T v); // remove from the set the element equal to 'v'.
// Return 'false' if no such element was in the set.
int Size(); // return number of elements in the Set.
static int NSets(); // return number of sets that are in existence
// that contain elements of this datatype.
// the following 3 Set methods can be used to sequentially
// retrieve each element in the set:
T getFirst(); // returns the "first" element in the set.
T getNext(); // returns the "next" element in the set (or the last elmt).
T getLast(); // returns the "last" element in the set.
If the Set is empty and getFirst, getNext, or getLast is called, there is no object these functions can return. So just print an error message and terminate the program.
Below is a main program to partially test Set class. You'll need to add some "cout" statements. The main program uses class Circle. You can download Circle.h and Circle.cpp.
int main() {
Set<float> fset; // a set of floats.
Set<Circle> pset; // a set of Circles.
Circle c(0.,0.,3.0); //a circle centered at (0,0) radius 3.0
Circle c2(0.,0.,3.0); //another circle centered at (0,0) radius 3.0
Circle c3(1.,0.,3.0); //a circle centered at (1,0) radius 3.0
bool ok;
ok=[login to view URL](c); // add circle c to the set
ok=[login to view URL](c2); // add circle c to the set. Make sure "ok" is false.
if (ok==false) cout<<"Can't add c2 to set"<<endl;
ok=[login to view URL](c3);
int n=[login to view URL](); // get size of the set, insure n is 2.
ok=[login to view URL](c); // remove circle c from the set. Insure ok is true.
int nset=Set<float>::NSets(); //insure nset is 1.
cout<<"Number of float sets = "<<nset<<endl;
nset=Set<Circle>::NSets(); // insure nset is 1.
cout<<"Number of Circle sets = "<<nset<<endl;
for (int i=0; i<6; i++) // add 6 floats to 'fset': 0,2,4,6,8,10
[login to view URL](i*2.0);
// retrieve each float in the set 'fset';
float elmt;
for (elmt = [login to view URL](); true; elmt=[login to view URL]()) {
// insure each different float value is printed.
cout<<"elmt="<<elmt<<endl;
if (elmt == [login to view URL]()) break; // end the loop
}
}
HINT2: Only use the vector 'begin()' method
Hello. We worked at previous time, there is quite easy assignment for me,. I am expert in C++, and I will not use set and I know how to use std::vector for this project
$15 USD in 1 day
4.9 (232 reviews)
6.1
6.1
5 freelancers are bidding on average $20 USD for this job
hi, i am goon in c++ , i can do this simple project in short time. i have done several console/desktop applications. i am confident with my sill that i can able to finnish this task easily. Thanks, Tristan
Hi
It is a small project.
I am a c/c++ expert, but a new freelancer.
If you award me, I can a standard c++ program.
I hope it is my first project, and just 5 stars.
Thank you.