Creating Program with Friend Functions and Function Overloading
I am to create a program with a Class Set having the the following member functions:
• Set() • void AddElement(int v) • void RemoveElement(int v) • void Print() then write the following friend functions of class Set: • void Union(Set A, Set B), which calculates and prints the union of A and B • void Intersect(Set A, Set B), which calculates and prints the intersection of A and B • void Minus(Set A, Set B), which calculates and prints the minus of A and B Write the following function to overload the following operations: • = (Creates a new set from old set. For instance, A=B creates A from B) • + (Creates union of two sets. For instance, C=A+B creates union of A and B into C) • * (Creates intersection of two sets. For instance, C=A*B creates intersection of A and B into C) •  (Creates minus of two sets. For instance, C=AB creates minus of A and B into C) Write a program which will take the elements of first set until user enters 1. Similarly, the program will take the elements of second set until user enters 1. Then show the following outputs in the console: a. Elements of first set using Print() function of class b. Elements of second set using Print() function of class c. Union of the sets using friend function d. Union of the sets using operator overloading (+) e. Intersection of the sets using friend function f. Intersection of the sets using operator overloading (*) g. Minus of the sets using friend function h. Minus of the sets using operator overloading () Sample input: 1 3 5 6 8 10 12 1 2 4 5 6 7 11 13 1 Sample output: First set: 1 3 5 6 8 10 12 Second set: 2 4 5 6 7 11 13 Union (friend): 1 2 3 4 5 6 7 8 10 11 12 13 Union (operator overloading): 1 2 3 4 5 6 7 8 10 11 12 13 Intersection (friend): 5 6 Intersection (operator overloading): 5 6 Minus (friend): 1 3 8 10 12 Minus (operator overloading): 1 3 8 10 12 I am trying to create the first friend function and i am stuck. I am a new to programming language and i need help. Any help that can be provided is greatly appreciated. This is what i have so far, its not much, but as i said, i am stuck. PHP Code:

Re: Creating Program with Friend Functions and Function Overloading
I'd have thought you want the Set members to be an array of some kind, e.g. int[20], rather than two distinct ints.
If using int[20] (as opposed to, say, vector) then you'll also need a variable indicating the current size. Or you could store the entered 1 to indicate the end of the data. Code:
class Set Your cout << "Set A is "; line should be followed by setA.Print(), not the code to print the array directly. But this does mean you can add the code for Print() easily now, as you've already written it. To calculate the union, if we're assuming the data is inserted in ascending order that simplifies things. Create a pair if integers (p1 and p2) which will be used as indexes into setA.array and setB.array. Set them both to zero. In a loop, you'll need a threeway check: if setA.array[p1]>setB.array[p2] OR p1==setA.size, display setB.array[p2] and increment p2 else if setA.array[p1]<setB.array[p2] OR p2==setB.size, display setA.array[p1] and increment p1 else (if neither of the above is true then the members must be equal) display one or the other and increment BOTH p1 and p2, because we don't want to display duplicates. Terminate the loop when p1==setA.size and p2==setB.size. A similar approach should work for both Intersect and Minus, and then the operator overloaded versions are pretty much just a case of semantics. 
Re: Creating Program with Friend Functions and Function Overloading
thank you for your reply, i used protected because we had just begun talking about it and i was possibly thinking that using an inheritance would help make it easier.
the data is going to be random, it could be ascending or descending or just some random inputs, so will that still work? also, looking at the union explanation you gave me, checking to see which is bigger between setA and setB, am i to assume that p1 and p2 are going to contain all the numbers from each set? if s will it just check each number individually or all at once.. Thanks Jeff 
Re: Creating Program with Friend Functions and Function Overloading
Inheritance doesn't make anything easier. Use this only when you have to. It certainly isn't needed for this task and you shouldn't use it unless the teacher has said you should, for example if his next few exercises require inheritance.
The code I gave will only work if the sets are sorted in increasing value. If this isn't the case then one way to avoid checking each element against all elements in the other set is to define an array of flags where the size is equal to the maximum value in either set. Then loop through each set and set the corresponding flag to 1, and for the union just loop through the flags and display which ones are set. So if set1 contains 3,5,7,9 and set2 contains 1,2,3,4,5, you need 9 flags (10 if you count from zero) and parsing set1 would set flags 3,5,7,9, and parsing set2 would set flags 15. This would result in flags 1,2,3,4,5,7,9 being set, which is the union. To calculate the intersection, parse set1 as above, but when parsing set2, set the flag to 2 IF it's currently set to 1. (So the flags need to be counters really, not boolean.) The resulting flags would be 1=1, 2=1, 3=2, 4=1, 5=2, 6=0, 7=1, 8=0, 9=1 and the intersection is those flags with the value 2, i.e. 3,5. The difference can then be done by parsing set1 as above (3,5,7,9=1), then when parsing set2 set the flag to zero if it's already a 1. (1,2,4 no change; 3,5=0). set1set2 is then the flags that remain at 1, i.e. 7,9. This will work if the sets don't contain duplicates. If they do then you'll need to think about the definitions of union, intersection and difference. For example: would the union of {1,2,3,3} and {3,3,4,5} be {1,2,3,4,5} or {1,2,3,3,4,5}? 
Re: Creating Program with Friend Functions and Function Overloading
Hey,
as you see i used inputArraySetA[20] to take in my inputs, i tried using int a, but each time i used that i couldn't save a set of numbers in it only one number, is there a way in which i could just declare int A int B and have A and B hold the set of numbers for A and B instead of the arrays? 
Re: Creating Program with Friend Functions and Function Overloading
As I understand it the point of the exercise is that the Set class contains the sets of numbers. So yes of course you can have a separate array of ints if you want, but then you would have to copy all those numbers into the Set variable:
int num[20] Set A for () read numbers into num[] end loop for () copy each number into A end loop and it seems to me it would just be a whole load easier to take the numbers directly into A: Set A for() read numbers into A end loop. No, you cannot store multiple integers into a single integer variable. To store multiple ints you need an array of ints. My point is that apart from Set you don't need to store multiple ints. 
Re: Creating Program with Friend Functions and Function Overloading
Quote:
Set A,B; // in main. how do i bring the number directly into Set A, and Set B after defining it without having to store it in that array? Thanks Jeff 
Re: Creating Program with Friend Functions and Function Overloading
PHP Code:

Re: Creating Program with Friend Functions and Function Overloading
You would need to call the AddElement function for that.

All times are GMT +5.5. The time now is 11:48. 