Objective: Implement operator overloading and use a friend function. Your submis
ID: 3777278 • Letter: O
Question
Objective: Implement operator overloading and use a friend function. Your submission must be comprised of two files: p9.cpp Contains the main() function and fully exercises the class SetClass. SetClass.h Contains the declaration and complete implementation of the class, Which implements the abstract data type Set. You are required to design and implement a C++ class, named SetClass, to implement the abstract data type Set. The universal set is the integers 0 through 50 (inclusive). You must implement the set operations “union”, “intersection”, and “complement” as overloaded operators on operands of class SetClass. You MUST NOT implement these as member methods of the class. You must implement a function print as a friend of the class SetClass. Implement a main() procedure to test the class implementation. The requirements are given below. (Note: Penalty points exceed points possible, but no score can be lower than 0.) 1) (6 point penalty) All code required to fulfill parts 2, 3 and 4 of these requirements must be defined in a header file SetClass.h, while all code required to fulfill all other requirements must be in a file named p9.cpp, which must reference SetClass.h in a preprocessor #include statement. Both of these files must be submitted using the handin facility on the csx server. 2) (6 point penalty) Implement the following public member functions for SetClass: a. isEmpty() returns true if the Set is empty, false otherwise b. isElement(x) returns true if x is a member of the set, false otherwise c. add(x) adds x to the set. If x is already a member or out of range, return false, otherwise return true. d. remove(x) removes x from the set. If x was in the set (before removal), return true indicating the removal is successful, otherwise return false There should be no public members other than the ones listed above and constructors. The storage for set should be implemented as a protected member “set” (an array of bool, bool set[51];). The constructors should initialize the Set object to be empty on creation. An element i is in the set if set[i] is true. In an empty set all elements of the array are false. 3) (6 points penalty) Implement a function “print” as a friend of the class SetClass. The function print takes an object of type SetClass and lists the elements of the set. 4) (4 points penalty) Implement the following functions on Sets (these are not members of any class): a. union as overloaded “+”. b. intersection as overloaded “*”. c. complement as overloaded “-“. 5) (8 points penalty) Implement a main program that tests all operations by performing at least the following operations: a. Create three empty sets A, B, C (declare A, B, C); verify the sets are empty by printing their members b. Read a set of integers and build set A; read a second set of integers and build set B; print the sets A and B with proper identifications. You need to use the member function add to add elements to the set. If addition of any integer to the set returns false, issue an error message c. Read a set of numbers and remove them from B if it is present (you need to use the member function remove), issue appropriate message if any number is not in the set; print B d. Compute: C=A+B; print C e. Compute: C=A*B; print C f. Compute: C=-B, print C; g. Read an integer and check if it is in the sets A, B, and C. Issue appropriate message in each case. (i.e x is not in A) 6) (2 points penalty) All code must be well documented. 7) The program must compile and run on the csx server to get any credit. CAUTION: This is an individual programming assignment. All work must be done individually. Sharing (or copying from any source) of code segment will be treated as plagiarism and dealt with accordingly. If any requirement is found to be ambiguous, bring it to the attention of the instructor for clarification. Submit the solutions using the “handin” command. Example submission command is “handin cs2433 program9 p9.cpp”, where p9.cpp is the file being submitted.
Explanation / Answer
p99.cpp
#include<iostream>
#include "setclass.h"
using namespace std;
void check(SetClass A);
void enterSet(SetClass& Temp)
{
int i=0;
print(Temp);
while(true)
{
cin>>i;
if(i>50)
break;
if(!Temp.isElement(i))
Temp.add(i);
else
cout<<"already present in set ";
}
}
void check(SetClass& A,char name)
{
int i;
cin>>i;
if(A.isElement(i))
cout<<i<<" present in "<<name<<endl;
else
cout<<i<<" not in "<<name<<endl;
}
int main()
{
int i;
SetClass A,B,C;
cout<<"elements of class A: ";
print(A);
cout<<"elements of class B: ";
print(B);
cout<<"elements of class C: ";
print(C);
cout<<"enter elements to store in A (>50 to stop) ";
enterSet(A);
cout<<"enter elements to store in B (>50 to stop) ";
enterSet(B);
cout<<"A: ";
print(A);
cout<<"B: ";
print(B);
cout<<"enter elements to be removed in B (>50 to stop) ";
do
{
cin>>i;
if(B.isElement(i))
B.remove(i);
else
cout<<"not present in set ";
}while(i<51);
cout<<"B: ";
print(B);
cout<<"union of A & B"<<endl;
C=A+B;
print(C);
cout<<"intersettion of A & B"<<endl;
C=A*B;
print(C);
cout<<"not of B"<<endl;
C=-B;
print(C);
cout<<"enter elements to for presence in A: ";
check(A,'A');
cout<<"enter elements to for presence in B: ";
check(B,'B');
cout<<"enter elements to for presence in C: ";
check(C,'C');
return 0;
}
//"setclass.h"
#include<iostream>
using namespace std;
class SetClass
{
private:
bool Set[51];
public:
SetClass()
{
int i;
for(i=0;i<51;i++)
{
remove(i);
}
}
bool isEmpty()
{
int i;
for(i=0;i<51;i++)
{
if(Set[i])
return false;
}
return true;
}
bool isElement(int x)
{
if(Set[x])
return true;
else
return false;
}
bool add(int x)
{
Set[x]=true;
return true;
}
bool remove(int x)
{
if(isElement(x))
{
Set[x]=false;
return true;
}
return false;
}
friend void print(SetClass buffer);
friend SetClass operator+(SetClass,SetClass);
friend SetClass operator*(SetClass,SetClass);
friend SetClass operator-(SetClass);
};
SetClass operator+(SetClass A,SetClass B)
{
SetClass Temp;
int i;
for(i=0;i<51;i++)
{
if(A.isElement(i)||B.isElement(i))
Temp.add(i);
}
return Temp;
}
SetClass operator*(SetClass A,SetClass B)
{
SetClass Temp;
int i;
for(i=0;i<51;i++)
{
if(A.isElement(i) && B.isElement(i))
Temp.add(i);
}
return Temp;
}
SetClass operator-(SetClass A)
{
SetClass Temp;
int i;
for(i=0;i<51;i++)
{
if(! A.isElement(i))
Temp.add(i);
}
return Temp;
}
void print(SetClass buffer)
{
int i;
for(i=0;i<51;i++)
{
if(buffer.isElement(i))
cout<<i<<" ";
}
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.