dear all,
i wrote a class integerSet which contain element as integer
here is the driver program main
IntegerSet a( n );
a.inputSet();
IntegerSet b (a);
IntegerSet c(2*n);
c = a.unionOfIntegerSets( b );
the problem is in union operation, how to avoid calling copy constructor at
the assignment,
c = a.unionOfIntegerSets( b );
since in copy constructor, i multiple the elements by 2.
here is my class implementation
using namespace std;
// constructor
IntegerSet::IntegerSet( int s )
{
size = s;
set = new int[ size ];
emptySet();
} // end class IntegerSet constructor
// copy constructor
IntegerSet::IntegerSet( const IntegerSet &init )
{
cout<<"copy constructor is called"<<endl;
size = init.size;
set = new int[ size ];
emptySet();
for ( int i = 0; i < size; i++ )
set[ i ] = 2*init.set[ i ];
} // end copy constructor
// function emptySet definition
void IntegerSet::emptySet()
{
for ( int y = 0; y < size; ++y )
set[ y ] = 0;
} // end function emptySet
// function inputSet definition
void IntegerSet::inputSet()
{
int i=0;
srand (time(0));
// input set information
while (i<size) {
set[i] = 1+ rand()%99;
i++;
}
} // end function inputSet
// print the set
void IntegerSet::setPrint() const
{
cout << '{';
for ( int u = 0; u < size; ++u )
cout << setw( 4 ) << set[u] << " " ;
cout << setw( 4 ) << "}" << '\n'<<'\n';
} // end function setPrint
// function unionOfIntegerSets definition
IntegerSet IntegerSet::unionOfIntegerSets(
const IntegerSet &r )
{
IntegerSet temp( size + r.size );
temp.emptySet();
for ( int i = 0; i < size; i++ )
temp.set[ i ] =set[i];
for (int j=0; j< r.size; j++)
temp.set[size+j]=r.set[j];
return temp;
} // end function unionOfIntegerSets
// insert element into set
void IntegerSet::insertElement( int k, int p )
{
int *ptr;
ptr=set;
size++;
set = new int [size];
for (int i=0; i<size-1; i++)
set[i]=ptr[i];
delete ptr;
for (i=size; i>p; i--)
set[i]=set[i-1];
set[p]=k;
return ;
} // end function insertElement
// delete an element from the set
void IntegerSet::deleteElement( int p )
{
for (int i=p; i<size; i++)
set[i]=set[i+1];
size--;
return ;
} // end function deleteElement
// function isEqualTo definition
bool IntegerSet::isEqualTo( const IntegerSet &r ) const
{
for ( int v = 0; v < ( size < r.size ? size : r.size ); ++v )
if ( set[ v ] != r.set[ v ] )
return false; // sets are not-equal
return true; // sets are equal
} // end function isEqualTo
IntegerSet::~IntegerSet()
{
delete set;
}