Union of Sets, Dynamic Memory, operator overloading

So, I have to create methods that takes the union and intersection of sets. I must also create a method that removes an element from the set. The union works fine, however, when I call the intersection method in the main, it returns all the elements as 0. For example, I typed in a set {1,2} and another {2,3,4}, so the intersection would be {2}. But, this displays 0. and displays the correct number of elements. So I am unsure what I am doing wrong. Also, the method that removes that set, has a problem with the delete that I have written, so I was wondering how to fix it. Any suggestions on how to fix it?
**side note: I am fully aware of my variable names, I was creating these names to get a better understanding of what I was doing.
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
97
98
99
100
101
 //default constructor
Set::Set ( int s ){

        if ( s > 0 )
                psize = s;
        else
                psize = DEFAULTSIZE;
        //allocate an array of specified size
        set = new int[ psize ];

        if(!set) {
                //send an error is system cannot allocate memory
                cout << "Cannot Allocate Memory, exiting program... " << endl;
                exit (1);
        }

        for ( int i = 0; i < psize; i++){
                set[i] = 0;
                numOfElements = 0;
        }
}
bool Set::element ( int n ){
                 for ( int i = 0; i < psize; i++){
                            if ( set[i] == n )
                                    return true;
                    }
                    return false;
            }

         Set Set::Union( Set &B ){
                int newsize = B.numOfElements + numOfElements;

                Set C(newsize);

                for (int i = 0; i < numOfElements; i++){
                        C.set[i] = set[i];
                }

                int indx = 0;
                for(int i = 0; i < B.numOfElements; i ++){
                        if(C.element(B.set[i])){
                                newsize--;
                                continue;
                        }
                        else
                        {
                                C.set[indx + numOfElements] = B.set[i];
                                indx++;
                        }
                }

                C.numOfElements = newsize;
                C.display();
                return (C);
        }
        Set Set::Intersection( Set &B ) {

                int newsize = numOfElements;

                Set C(newsize);
                        for ( int i = 0; i < numOfElements; i++ ){
                                if( element(B.set[i]))
                                        C.set[i] = B.set[i];
                                else{
                                        newsize--;
                                        continue;
                                }
                        }
                return (C);
        }
   Set Set::operator-( int n ){
            for ( int i = 0; i < numOfElements; i++){
                    if(element(n)){
                            delete set[i];
                            numOfElements--;
                    }
            }
            psize = numOfElements;

            return (*this);
    }


            main (){

                    Set A, B, C;
                    A.input();
                    A.display();
                    B.input();
                    B.display();
                    C = A.Union(B);
                    C.display();
            }







.
Topic archived. No new replies allowed.