473,387 Members | 1,540 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,387 software developers and data experts.

printing the union (help)

Hi, I am doing a program of overloaded methods.
I want to get what is the union of the user set and S set and the user
set and S1 set.
I try it but it is not working.
Can some one help me please?

Here is part of my code:

class IntegerSet
{
private:
bool set[26];
int element;

public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //Method
union

};

void IntegerSet::setString()
{
cout<<"{";
for(element=0; element<26; element++)
{
if(set[element]==true)
{
cout<<" "<<element<<" ";
}
}
cout<<"}";
}

main()
{
for(int i=2; i<21; i++)
{
if(i%2==0)
S1set.insertElement(i);
}
for(int z=0; z<26; z++)
{
Sset.insertElement(z);
}

int temp, ele;
int newSet[26];

cout<<"Enter how many elements you want in the set: ";
cin>>ele;
cout<<"Enter the values: \n";
for(int i=0; i<ele; i++)
{
cout<<i+1<<": ";
cin>>temp;
newSet[i]=temp;
}
IntegerSet S4set(newSet, ele);
cout<<"Your set is: ";
S4set.setString();

cout<<"\n\nSelect one of these choices:\n";
cout<<" 1. Find Union \n";
cout<<"Choice: ";
cin>>choice;

if(choice==1)
{
char a, b, option;

cout<<"\nYour set is: ";
S4set.setString();
cout<<"\nSet S: ";
Sset.setString();
cout<<"\nSet S1: ";
S1set.setString();

cout<<"\n\nSelect one of this choices to find the union of:\n";
cout<<" a. Your set and set 'S'\n";
cout<<" b. Your set and set 'S1'\n";
cout<<"Option: ";
cin>>option;

IntegerSet unio();

if(option=='a'||option=='A')
{
unio = S4set.operator+(Sset);
}
else if(option=='b'||option=='B')
{
unio = S4set.operator+(S1set);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
}

Thanks

Nov 13 '07 #1
8 1928
Latina wrote:
Hi, I am doing a program of overloaded methods.
I want to get what is the union of the user set and S set and the user
set and S1 set.
I try it but it is not working.
Can some one help me please?
Define "Not working". What are you inputting? What are you getting
out? What are you expecting?

See FAQ 5.8 http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Post a minimal *COMPILABLE* example (see my comment below) that exhibits
the behavior in question. Tell us what you did, what you expected, and
what you got.
>
Here is part of my code:

class IntegerSet
{
private:
bool set[26];
int element;

public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //Method
union

};
This following method won't compile -- no prototype
>
void IntegerSet::setString()
{
cout<<"{";
for(element=0; element<26; element++)
{
if(set[element]==true)
{
cout<<" "<<element<<" ";
}
}
cout<<"}";
}
Error. Implicit int not allowed.
main()
{
for(int i=2; i<21; i++)
{
if(i%2==0)
S1set.insertElement(i);
}
for(int z=0; z<26; z++)
{
Sset.insertElement(z);
}

int temp, ele;
int newSet[26];

cout<<"Enter how many elements you want in the set: ";
cin>>ele;
cout<<"Enter the values: \n";
for(int i=0; i<ele; i++)
{
cout<<i+1<<": ";
cin>>temp;
newSet[i]=temp;
}
IntegerSet S4set(newSet, ele);
cout<<"Your set is: ";
S4set.setString();

cout<<"\n\nSelect one of these choices:\n";
cout<<" 1. Find Union \n";
cout<<"Choice: ";
cin>>choice;

if(choice==1)
{
char a, b, option;

cout<<"\nYour set is: ";
S4set.setString();
cout<<"\nSet S: ";
Sset.setString();
cout<<"\nSet S1: ";
S1set.setString();

cout<<"\n\nSelect one of this choices to find the union of:\n";
cout<<" a. Your set and set 'S'\n";
cout<<" b. Your set and set 'S1'\n";
cout<<"Option: ";
cin>>option;

IntegerSet unio();

if(option=='a'||option=='A')
{
Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.
unio = S4set.operator+(Sset);
}
else if(option=='b'||option=='B')
{
See above.
unio = S4set.operator+(S1set);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
}
Nov 13 '07 #2
Hi, I am doing a program of overloaded methods. I am using Dev-C++
compiler.
The program first asks the user to enter a set of elements that are
stored in the IntegerSet S4set.
Then asks the user to enter 'a' to find the union of his/her set and
set S or enter 'b' find the union of his/her set and set S1.
Then should print out the union of the two sets. Which it is not doing
it, it only printing this: { }

Here is my code again:

class IntegerSet
{
private:
bool set[26];
int element;

public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //method union

};

IntegerSet set();

IntegerSet::IntegerSet()
{
for(element=0; element<=25; element++)
set[element]= false;
}

IntegerSet::IntegerSet(int x[], int k)
{
for(element=0; element<=25; element++)
set[element]= false;
for(int j=0; j<k; j++)
{
element=x[j];
set[element]= true;
}
}

void IntegerSet::insertElement(int element)
{
set[element]=true;
}

void IntegerSet::setString()
{
cout<<"{";
for(element=0; element<26; element++)
{
if(set[element]==true)
{
cout<<" "<<element<<" ";
}
}
cout<<"}";
}

main()
{
for(int i=2; i<21; i++)
{
if(i%2==0)
S1set.insertElement(i);
}
for(int z=0; z<26; z++)
{
Sset.insertElement(z);
}

int temp, ele;
int newSet[26];

cout<<"Enter how many elements you want in the set: ";
cin>>ele;
cout<<"Enter the values: \n";
for(int i=0; i<ele; i++)
{
cout<<i+1<<": ";
cin>>temp;
newSet[i]=temp;
}
IntegerSet S4set(newSet, ele);

char a, b, option;

cout<<"\nYour set is: ";
S4set.setString();
cout<<"\nSet S: ";
Sset.setString();
cout<<"\nSet S1: ";
S1set.setString();

cout<<"\n\nSelect one of this choices to find the union of:\n";
cout<<" a. Your set and set 'S'\n";
cout<<" b. Your set and set 'S1'\n";
cout<<"Option: ";
cin>>option;

IntegerSet unio();

if(option=='a' || option=='A')
{
unio = S4set.operator+(Sset);
}
else if(option=='b' || option=='B')
{
unio = S4set.operator+(S1set);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
Thanks

Nov 13 '07 #3
IntegerSet unio();
>
if(option=='a'||option=='A')
{

Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.
unio = S4set.operator+(Sset);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
because it cannot convert `IntegerSet' to `IntegerSet ()()' in
assignment
but how I can correct that?

Nov 13 '07 #4
Latina wrote:
Hi, I am doing a program of overloaded methods. I am using Dev-C++
compiler.
The program first asks the user to enter a set of elements that are
stored in the IntegerSet S4set.
Then asks the user to enter 'a' to find the union of his/her set and
set S or enter 'b' find the union of his/her set and set S1.
Then should print out the union of the two sets. Which it is not doing
it, it only printing this: { }

Here is my code again:
And try yet again.

The followng methods are not defined in the class:

IntegerSet::IntegerSet(int[], int)
IntegerSet::IntegerSet();
void IntegerSet::insertElement(int);
void IntegerSet::setString();

Implicit int for main() is illegal. Main returns int. Declare it as:

int main()
Nov 13 '07 #5
Latina wrote:
>> IntegerSet unio();
if(option=='a'||option=='A')
{
Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.
>> unio = S4set.operator+(Sset);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}

because it cannot convert `IntegerSet' to `IntegerSet ()()' in
assignment
but how I can correct that?
What do you mean? Show the code that generated this error.
Nov 13 '07 #6
On Nov 13, 1:42 pm, red floyd <no.s...@here.dudewrote:
Latina wrote:
> IntegerSet unio();
if(option=='a'||option=='A')
{
Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.
> unio = S4set.operator+(Sset);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
because it cannot convert `IntegerSet' to `IntegerSet ()()' in
assignment
but how I can correct that?

What do you mean? Show the code that generated this error.- Hide quoted text -

- Show quoted text -
if(option=='a'||option=='A')
{
unio.S4set.operator+(Sset); <--Error here
}
else if(option=='b'||option=='B')
{
unio=S4set.operator+(S1set); <--Error here
}

Nov 13 '07 #7
Latina wrote:
On Nov 13, 1:42 pm, red floyd <no.s...@here.dudewrote:
>Latina wrote:
>>>> IntegerSet unio();
This line does not do what you think it does. You think it declares and
default initializes an IntegerSet named unio.

Wrong. It declares unio as a function returning an IntegerSet.
Kill the parens on this declaration.
>>>> if(option=='a'||option=='A')
{
Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.
unio = S4set.operator+(Sset);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
because it cannot convert `IntegerSet' to `IntegerSet ()()' in
assignment
but how I can correct that?
What do you mean? Show the code that generated this error.- Hide quoted text -

- Show quoted text -

if(option=='a'||option=='A')
{
unio.S4set.operator+(Sset); <--Error here
}
else if(option=='b'||option=='B')
{
unio=S4set.operator+(S1set); <--Error here
}
Nov 13 '07 #8
On Nov 13, 8:01 pm, red floyd <no.s...@here.dudewrote:

[...]
IntegerSet unio();
if(option=='a'||option=='A')
{
Why not: unio = S4set + Sset; ? That's the whole point of
operator overloading.
On the other hand, unio isn't a variable, and so can't appear on
the right side of an assignment, regardless of what he puts on
the left.

Just a guess, but he probably meant to define unio as a local
variable, rather than as an external function. In which case,
the definition should have been:

IntegerSet unio ;
unio = S4set.operator+(Sset);
If he wants help, I think he really needs to start posting code
which compiles. Or doesn't, of course, if he's asking about a
compiler error.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 14 '07 #9

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

Similar topics

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...
3
by: Dalan | last post by:
From reading Access 97 help text, it seems that to do what I need to do will require a Union Query. As this would be my first, I think I might require a little guidance. I have two tables with...
4
by: shaun palmer | last post by:
when or Where do you use a union query ? how can I wright sql, tblPopulation,* tblcustomer,* one to one with all the appropriate attributes(field). Your help would be greatly...
12
by: Susan Bricker | last post by:
For those of you who have been following my posts - they all pertain to a Dog Competition Organization's Database. There are three classes that the dogs can participate: NOVICE, OPEN, and...
14
by: rahul8143 | last post by:
hello, what will be output of following program on INTEL machine #include <stdio.h> void main() { union s { int i; char ch;
4
by: sebastien NO Maraux SPAM | last post by:
I am using Ghost Lib 4.0, which is SDK for Phantom haptic device. this lib does not compile under .net, seemingly because of a union of this type : union A { union A* aList; char b; };
7
by: jason.langdale | last post by:
I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5 and table B has field 1,2,3,4,5. I want to do a union on these. (I have done so successfully if I stop here) I also want to join...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
10
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.