Ok, how do I overload the * operator??
I can do the + and -, but not the *.
Here's code:
Code:
#include<iostream.h>
class set{
int *data;
int cap;
int top;
public:
set(){top=0;cap = 5; data = new int[cap];};
set(set &);
void insert(int);
void remove(int);
int isEmpty(){ return !top;};
int isElement(int);
int size(){return top;};
friend set operator+(set, set);
friend set operator-(set,set);
friend set operator*(set, set);
friend ostream& operator<<(ostream&, set);
friend int operator==(set, set);
};
int set::isElement(int target)
{
for(int i = 0; i < top; i++)
if(data[i] == target)
return 1;
return 0;
}
set::set(set &S)
{
cap = S.cap;
top = S.top;
data = new int[cap];
for(int i = 0; i < top; i++)
data[i] = S.data[i];
}
void set::insert(int target)
{
if(isElement(target))
return;
if(top == cap){
int *temp = new int[cap + 5];
for(int i = 0; i < top; i++) temp[i] = data[i];
cap += 5;
delete []data;
data = temp;
}
data[top++] = target;
}
void set::remove(int target)
{
int i;
if( !isElement(target))
return;
for(i = 0; i < cap; i++)
if(data[i] == target)
break;
while(i < cap - 1){
data[i] = data[i+1];
i++;
}
top--;
}
set operator+(set S1, set S2)
{
for(int i = 0; i < S2.top; i++)
S1.insert(S2.data[i]);
return S1;
}
set operator-(set S1, set S2)
{
for(int i = 0; i < S2.top; i++)
S1.remove(S2.data[i]);
return S1;
}
set operator*(set S1, set S2) //Way wrong!!! HELP!!!
{
for (int i=0; i<S2.top; i++)
for (int j = 0; j < S1.top; j++){
if(S1.data[i] == S2.data[j])
S1.insert(S2.data[i]);
if(S1.data[i] != S2.data[j])
S1.remove(S2.data[i]);
}
return S1;
}
ostream& operator<<(ostream &out, set S)
{
for(int i = 0; i < S.top; i++)
out << S.data[i] << " ";
return out;
}
int operator==(set S1, set S2)
{
if( ((S1-S2) + (S2 - S1)).size() == 0)
return 1;
return 0;
}
void main()
{
int in;
set S1, S2;
cout << "please insert integers for the first set, and -1 to stop\n";
cin >> in;
while(in != -1){
S1.insert(in);
cin >> in;
}
cout << "please insert integers for the seconmd set, and -1 to stop\n";
cin >> in;
while(in != -1){
S2.insert(in);
cin >> in;
}
cout << endl;
cout << S1 << endl;
cout << S2 << endl;
cout << S1+S2 << endl;
cout << S1 - S2 << endl;
cout << S1*S2<< endl;
if(S1 == S2)
cout << "identical\n";
else
cout << "not identical\n";
}
Thanks!!