Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Description A Class Set is partially defined as below. This definition s

ID: 3802016 • Letter: P

Question

Problem Description 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

};

1) Please provide full program implementation of the Class Set with the following operator overloaded. [4 parks each operator: totally 40 marks]

= 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 checking the sum of one Set is smaller than the sum of another Set.

++ means incrementing every element of Set by one; please consider both prefix and postfix situations;

-- means decreasing every element of Set by one; please consider both prefix and postfix;

>> means from input stream to read in element values to put in the Set;

<< means display every element of a Set to the output stream; every 5 elements per line separated by, between elements;

[ ] means accessing the index of the Set to either retrieve the value of an element or make a change to the value of an element.

2) Program full implementation of all the member functions; you should add necessary functions. [15 marks]

3) Program a driver to run and test every member function and the overloaded operators. [5 marks] Although the driver is only 5 marks if it was not provided, the requirement 1 and 2 are not going to be evaluated properly, may lead to mark deduction for program correctness and execution error free.

MY IMPLEMENTATION OF THE CODE BUT HAVING PROBLEMS WITH THESE TWO FUNCTIONS, HOW WOULD YOU IMPLEMENT THESE TWO PARTS CAUSE ARRAYS ARE INVOLVED the rest of the code should be fine PLEASE TRY TO ATTEMPT IT

> means checking the sum of one Set is greater than the sum of another Set

< means checking the sum of one Set is smaller than the sum of another Set

//SET.H

#ifndef SET_H
#define SET_H
using namespace std;

class Set {
public:
Set(int[], int); // default constructor
Set(const Set&);// copy constructor
~Set(); // destructor
int getSize() const; // return size
const Set& operator=(const Set& ); // assignment operator
bool operator==(const Set& )const;
bool operator !=(const Set& )const;
bool operator >(const Set& )const;
bool operator <(const Set& )const;
Set operator ++();//pre decrement
Set operator ++(int);//post decrement
Set operator --();//pre decrement
Set operator --(int);//post decrement

///input and output operators
friend ostream& operator<<(ostream &, const Set &);
friend istream& operator>>(istream &, Set &);

// subscript operator for non-const objects
int &operator[]( int );
// subscript operator for const objects
int operator[]( int ) const;


private:
   int size;
   int *ptr;
  

};
#endif

//SET.CPP

#include <iostream>
#include <iomanip>
using namespace std;
#include "Set.h" // Set class definition

// default constructor for class Set
// takes an array of integer and the size

Set::Set( int array[], int size1)
{
size = size1;
ptr = new int[ size ];
for ( int i = 0; i < size; i++ )
ptr[ i ] = array[i];
}
Set::Set(const Set& S){

   size = S.size;
   for(int i =0; i<size; ++i)
   {
       ptr[i]=S.ptr[i];
   }
}
// destructor for class Set
Set::~Set()
{
delete [] ptr;
}

// return size of set
int Set::getSize() const
{
return size;
}
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Set& Set::operator=(const Set& seT )
{
if ( &seT != this ) // check for self-assignment
{
if ( size != seT.getSize() ) {
delete [] ptr; // reclaim space
size = seT .getSize(); // resize this object
ptr = new int[ size ]; // create space for set copy

}
for ( int i = 0; i < size; i++ )
ptr[ i ] = seT [ i ]; // copy set into object
}
return *this;
}

// determine if two sets are equal and
// return true, otherwise return false
bool Set::operator==(const Set& seT ) const
{
if ( size != seT.getSize() )
return false;
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != seT [ i ] )
return false;
return true;
}
// inequality operator; returns opposite of == operator
bool Set::operator!=(const Set& seT ) const
{
return !( *this == seT ); // invokes Set::operator==
}

// overloaded subscript operator for const sets
int Set::operator[]( int subscript ) const
{
// check for subscript out of range error
if ( subscript < 0 || subscript >= size )
{
cout << "Error: Subscript " << subscript <<" out of range" << endl;
exit (1); // terminate program
}
return ptr[ subscript ];
}
// overloaded subscript operator for non-const sets
int &Set::operator[]( int subscript )
{
// check for subscript out of range error
if ( subscript < 0 || subscript >= size )
{
cout << "Error: Subscript " << subscript << " out of range" << endl;
exit(1); // exit program
}
return ptr[ subscript ];
}

bool Set::operator>(const Set& s1 ) const{
  
   int sum;
  
  
   for(int i=0;i <size;++i)
   sum+=s1.ptr[i];
   //s1.sum=this->ptr[i];
   if (sum>s1.size)
   { return true;
   }
   else
       return false;
  

   //int sum1;
   //int sum2;
//   for(int i=0;i <getSize;++i)
//set=+size[i];
//sum2=+seT.size[i];
   //if ( sum1>sum2)
   // return true;
       //else
   // return false;
   //this->ptr[4]

     
//return(seT.getSize() >seT.getSize() );

   //if (size>seT.getSize())
   // return true;
   //else
       //return false;

}

bool Set::operator<(const Set& seT ) const{

//return(seT.getSize() <seT.getSize() );
   if (size<seT.getSize())
   return true;
   else
       return false;

}
/// pre decrement
Set Set::operator--(){
   for(int i = 0; i < size; i++)
ptr[i]--;
return *this;

//Set u=*this;

// if (-- u.size>0)
// {
//--size;
// }
// else

//cout<<"cannot be performed cause its negative"<< endl;


///return*this;//ask

}
/// post decrement
Set Set::operator--(int inc){

   for(int i = 0; i < size; i++)
ptr[i]--;
return *this;

   //Set temp=*this;
   //Set u=*this;
//if (-- u.size>0)
// {
//size--;
//}
// else

//cout<<"cannot be performed cause its negative"<< endl;


//return temp;//ask

}

Set Set::operator++(){

//++size;

for (int i=0; i<size;i++)
   ptr[i]++;
return*this;
//return*this;//ask

}

Set Set::operator++(int inc){/// increment
   //Set temp=*this;
       //size++
//return temp; //ask

for (int i=0; i<size;i++)
   ptr[i]++;
return*this;


}
///overloading stream to allow cout
ostream& operator<<(ostream& output, const Set & s1){
for (int i=0;i< s1.getSize();i++)
output <<s1[i]<<setw(5);
//output <<setw(5)<<s1.size;
return output;

}

//// overloading stream to allow cin
istream& operator>>(istream& input, Set & s1){

input >> s1.size;

//input.ignore(1);//skip dash(-)
return input;

}

// TEST.CPP

#include <iostream>
#include "set.h"
using namespace std;
int main()
{
int a1[] = {1, 5, 8};
Set s1(a1, 3); // call Set constructor
cout << s1[0] << ' '; //corresponds to s1.operator[](0)
cout << s1[1] << ' '; //corresponds to s1.operator[](1)
cout <<s1[2] << ' '; //corresponds to s1.operator[](2)
int a2[] = {4, 6, 8};
Set s2(a2, 3); // call Set constructor
if (s1 == s2) //corresponds to s1.operator==(&s2)
cout <<s1[0]<<" "<<s1[1]<<" "<<s1[2]<< "Equals to "<<s2[0]<<" "<<s2[1]<<" "<<s2[2]<<endl;
else
cout <<s1[0]<<" "<<s1[1]<<" "<<s1[2]<< "not equals to "<<s2[0]<<" "<<s2[1]<<" "<<s2[2]<<endl;
s2 = s1; //corresponds to s1.operator=(&s1)
//if (s1 == s2) //corresponds to s1.operator==(&s2)
//cout << "Equals ";
//else
//cout << "Not Equals ";

if (s1>s2)
   cout<<s1[0]<<" "<<s1[1]<<" "<<s1[2]<<"is greater "<<s2[0]<<" "<<s2[1]<<" "<<s2[2]<<endl;
else
   s2>s1;
   cout<<s1[0]<<" "<<s1[1]<<" "<<s1[2] <<"is less than "<<s2[0]<<" "<<s2[1]<<" "<<s2[2]<<endl;;
  
++s1;
s2++;
cout<<s1<<endl;
cout<<s2<<endl;

--s1;
s2--;


system("pause");


return 0;
} // end main

Explanation / Answer

Please add this code for operator >

bool Set::operator>(const Set& s1 ) const{

int sum1=0;
int sum2=0;

for(int i=0;i <s1.size;++i)
sum1+=s1.ptr[i];

for(int i=0;i <this->size;++i)
sum2+=this->ptr[i];

//s1.sum=this->ptr[i];
if (sum1>sum2)
{ return false;
}
else
return true;

}

for operator <

bool Set::operator<(const Set& s1 ) const{

int sum1=0;
int sum2=0;

for(int i=0;i <s1.size;++i)
sum1+=s1.ptr[i];

for(int i=0;i <this->size;++i)
sum2+=this->ptr[i];

//s1.sum=this->ptr[i];
if (sum1<sum2)
{ return false;
}
else
return true;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote