USING OPERATION OVERLOADING AND C++, A Class Set is partially defined as below.
ID: 3863475 • Letter: U
Question
USING OPERATION OVERLOADING AND C++,
A Class Set is partially defined as below. This definition still missing a number of member functions and operators. class Set {public: Set(int[], int);//default constructor Set(const Set&)//copy constructor ~Set();//destructor int getSize() const;//return size private: int size;//set size int *ptr://pointer to first element of set}; Please provide full program implementation of the Class Set with the following operator overloaded. = means assignment operator == means checking equal or not != means checking not equal > means checking the sum of one Set is greater than the sum of another Set > means from input stream to read in element values to put in the Set;Explanation / Answer
#include<iostream>
#include<conio.h>
using namespace std;
const int SIZE=10;
class Set{
public:
int num1,num2;
Set();
Set(int [],int);//default constructor
Set(const Set&);//copy Constructor
~Set();//destructor
int getSize()const;//return size
void operator=(const Set& obj);
Set(){
register int i;
for(i=0;i<SIZE;i++){
arr[i]=i;
}
}
private:
int arr[SIZE];
int size;//set size
int *ptr;//pointer to first element of set
};
Set::Set (int size ){ //default constructor
size = (size > 0 ? size : 10);
ptr = new int [size]; // ADD THIS LINE
for (int i = 0; i < size; i++)
ptr[ i ] = 0; //initial values
}
void Set::operator=(const Set& obj)
{
(*this).num1= obj.num1;
(*this).num2 = obj.num2;
return;
}
bool Set::operator==(const Set& obj){
if(num1==obj.num1)
return true;
else
return false;
}
bool Set::operator!=(const Set& obj){
if(num1!=obj.num1)
return true;
else
return false;
}
bool Set::operator>(const Set& obj){
if(num1>obj.num1)
return true;
else
return false;
}
bool Set::operator<(const Set& obj){
if(num1<obj.num1)
return true;
else
return false;
}
Set& Set::operator++(){ //prefix
++num;
return *this;
}
Set Set::operator++(int){ //postfix
Set temp=*this;
++num;
return temp;
}
Set& Set::operator--(){ //prefix
--num;
return *this;
}
Set Set::operator--(int){ //postfix
Set temp=*this;
--num;
return temp;
}
ostream & operator << ( ostream & output, const Set & arr){
for (int i = 0; i < arr.size; i++)
output << arr.ptr[i] ;
return output;
}
istream & operator >> ( istream & in, const Set & arr){
for (int i = 0; i < arr.size; i++)
in >> arr.ptr[i] ;
return in;
}
int &operator[](int i){
if(i>SIZE){
cout<<"Index out of bounds"<<endl;
return arr[0];
}
return arr[i];
}
void Set::print(){
for(int j=0;j<10;j++){
cout<<arr[j];
}
}
int main(){
Set s1,s2,s3;
s1.num1=2;
s1.num2=3;
cout<<s1.num1<<" "<<s1.num2<<;
s2.num1=2;
s2.num2=3;
s3=s1+s2;
cout<<s3.num1<<" "<<s3.num2<<endl;
//for Equal
cout<<s1==s2<<endl;
//for Not Equal
cout<<s1!=s2<<endl;
//for greater
cout<<s1>s2<<endl;
//for smaller
cout<<s1<s2<<endl;
//for post-increment
s1++;
cout<<s1.num1<<endl;
//for pre increment
++s1;
cout<<s1.num1<<endl;
//for post decrement
s1--;
cout<<s1.num1<<endl;
//for pre decrement
--s1;
cout<<s1.num1<<endl;
for(int i=0;i<10;i++){
s1[i]=i;
}
s1.print();
//<<
ofstream fout("out.text");
fout<<s1;
//>>
ifstream fin("in.txt");
fin>>s1;
cout<<s1;
//for []
cout<<"Value of s1[3]: "<<s1[3]<<endl;
cout<<"Value of s1[5]: "<<s153]<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.