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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#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 = 0; i < 20; i++) 
		{
			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 = 0; i < 20; i++) 
		{
			cin >> temp;
			if (temp == -1) 
				{
					break;
				} 
			else 
				{
					inputArraySetB[i] = temp;
					sizeSetB++;
				}
		}

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


        return 0;
}

maybe i dont get this right, but why are you working with arrays? arent you supposed to use your set class to store the users input?
in your set class you can use std::set to save the data. http://cplusplus.com/reference/stl/set/
its feature is quite helpful for the union operation
Last edited on
i read up on how to accept the inputs and be able to store them online and everything that i had tried failed except when i used the array. As i said, i am very new to this..

I tried declaring
int a, b;
then when i used them to store, i couldn't find out how to store a set of numbers, i could only store one number in it at a time. If you could show me how to that would be greatly appreciated.

Thanks

Topic archived. No new replies allowed.