Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Creating Program with Friend Functions and Function Overloading (http://www.go4expert.com/forums/creating-program-friend-functions-t25351/)

boat_58 27Mar2011 23:27

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=A-B 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:

#include <iostream>
using namespace std;


int inputArraySetA[20];
int inputArraySetB[20];
//int inputArrayUnion[20];
int sizeSetA 0;
int sizeSetB 0;
//int sizeUnion = 0;
int temp 0;




class 
Set
{
    protected:
        
int x;
        
int y;
    public:
        
Set();
        
Set(int px,int py);
        
void AddElement(int v);
        
void RemoveElement(int v);
        
void Print();
        
friend void Union(int inputArraySetA[20], int inputArraySetB[20]);
        
friend void Intersect(int inputArraySetA[20], int inputArraySetB[20]);
        
friend void Minus(int inputArraySetA[20], int inputArraySetB[20]);
        
        
};


//--------------------------------------------------------------------------------------------------------------------    
void Union(int inputArraySetA[20], int inputArraySetB[20])
{


}


//--------------------------------------------------------------------------------------------------------------------

int main()
{
     
    
        
        
cout << "Enter First set of numbers, (-1) to quit: "<<endl// Takes the input of the first set of numbers
        
for (int i 020i++) 
        {
            
cin >> temp;
            if (
temp == -1
                {
                    break;
                } 
            else 
                {
                    
inputArraySetA[i] = temp;
                    
sizeSetA++;
                }
        }
        
    

        
cout << "Enter Second set of numbers, (-1) to quit: "<<endl// Takes the input of the second set of numbers
        
for (int i 020i++) 
        {
            
cin >> temp;
            if (
temp == -1
                {
                    break;
                } 
            else 
                {
                    
inputArraySetB[i] = temp;
                    
sizeSetB++;
                }
        }

//--------------------------------------------------------------------------------------------------------------------        
       
cout << "Set A is ";
        for (
int i 0sizeSetAi++) 
            {
                
cout << inputArraySetA[i] << " ";
            }
        
cout << "\nSet B is ";
        for (
int i 0sizeSetBi++) 
            {
                
cout << inputArraySetB[i] << " ";
            }


        return 
0;



xpi0t0s 28Mar2011 13:27

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
{
private: // use this instead of protected, unless you know why you should be using protected
  int array[20];
  int size;

public:
  Set() { size=0; }
  void AddElement(int v) { array[size++]=v; } // TODO: add check for overflow
  void RemoveElement(int v) {} // add this later
  void Print() {} // add this later

//lets write these according to the spec given
  friend void Union(Set A, Set B);
  friend void Intersect(Set A, Set B);
  friend void Minus(Set A, Set B);
};

In main, define Set setA, setB; then when inputting each set of numbers, after reading the number and checking for -1, just bung the number directly into setA or setB instead of into this inputArraySetA[20]; (which you weren't asked to do).

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 three-way 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.

boat_58 28Mar2011 19:14

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

xpi0t0s 29Mar2011 14:54

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 1-5. 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). set1-set2 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}?

boat_58 31Mar2011 18:53

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?

xpi0t0s 31Mar2011 20:27

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.

boat_58 3Apr2011 11:16

Re: Creating Program with Friend Functions and Function Overloading
 
Quote:

In main, define Set setA, setB; then when inputting each set of numbers, after reading the number and checking for -1, just bung the number directly into setA or setB instead of into this inputArraySetA[20]; (which you weren't asked to do).
hey real quick, can you explain what you mean by this for me. I defined
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

boat_58 3Apr2011 11:29

Re: Creating Program with Friend Functions and Function Overloading
 
PHP Code:


int main
()
{
    
        
set A,B;

        
cout << "Enter First set of numbers, (-1) to quit: "<<endl// Takes the input of the first set of numbers
        
for (int i 020i++) 
        {
            
cin >> temp;
            if (
temp == -1
                {
                    break;
                } 
            else 
                {
                    
SetA[i] = temp;
                    
sizeSetA++;
                }
        }
        
       
cout << "Set A is ";setA.Print(); 

I just changed the name of that array thinking that would do it but when the setA prints its empty so i did it wrong. how would i directly store it in setA

xpi0t0s 4Apr2011 21:02

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 02:06.