C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

comment on union operation function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
penny336
Guest





PostPosted: Fri Feb 27, 2004 6:12 am    Post subject: comment on union operation function Reply with quote



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"< 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 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 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 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;
}


Back to top
Sharad Kala
Guest





PostPosted: Fri Feb 27, 2004 6:42 am    Post subject: Re: comment on union operation function Reply with quote




"penny336" <penny336 (AT) hotmail (DOT) com> wrote

Quote:
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.

You should re-think about your copy-constructor implementation.
An implementation is free to elide the copy-constructor when dealing with
temporary objects.
In such cases it's not good to have copy-constructors with side-effects.

-Sharad



Back to top
John Harrison
Guest





PostPosted: Fri Feb 27, 2004 7:30 am    Post subject: Re: comment on union operation function Reply with quote




"penny336" <penny336 (AT) hotmail (DOT) com> wrote

Quote:
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.

Why on earth do you do that? A copy constructor should copy, nothing else.
Isn't that obvious?. Fix that before you do anything else, your code is not
going to work any other way.

john





Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.