473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

comment on union operation function

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;
}
Jul 22 '05 #1
2 2095

"penny336" <pe******@hotmail.com> wrote in message
news:c1**********@www.csis.hku.hk...
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
Jul 22 '05 #2

"penny336" <pe******@hotmail.com> wrote in message
news:c1**********@www.csis.hku.hk...
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


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Rasmussen | last post by:
I have some data packed in by bitfields: struct Data { unsigned long a : 8; unsigned long b : 14; unsigned long c : 7; unsigned long d : 3; };
3
by: Matias Silva | last post by:
Hi Everyone, I wrote a for loop to build several select statements that are combined with a UNION. When I execute one of the queries separately, it works, but when I execute the query with a UNION...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
5
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
3
by: Martin P. Hellwig | last post by:
Hi all, I've been toying with python for about two years now. Not every day, just when I encounter something in my job (sysadmin) repetitively dull. The amazing thing is that like any other...
4
by: janko.klemensek | last post by:
Can I Group by the Union in one query or must I write two queries for this (one for union and second for group by)?
8
by: zacariaz | last post by:
typedef unsigned long long uint64 // just so we dont get confused ;) ok, the problem is this. I have 1 + n number of uint64 variables and the 1 should at all times contain a value equal to the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.