-
Overloading * operator
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!!
-
i believe it's mistaking that operator as the pointer operator. i don't know more than that, unfortunately.
-
The operator* function should only print out that numbers that are duplicates/members of both sets.
IE.
First set: 1, 2, 3, 10
Second set: 2, 3, 12
Prints out: 2, 3 (without the comma's)
Any ideas??
I just can't figure this out!! argh!!!
-
Ah LouB, the things I do for you :D
It appeared to me that there weren't that many mistakes in what you did. here are a couple of things to remember.
1) Keep your operators functioning the same way they would for simple data types like int and char
when you do this:
Code:
int x, y;
int z = x + y;
x and y do not change, so when you make your operator+ function, neither of your inputs should change
2) when you deal with operators, make sure you use references where you can, and return copies of local variables. this reduces the amount of times that copy constructor is called.
here's your code, that i've altered slightly. seems to work fine.
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)
{
set tmp;
for(int i = 0; i < S1.top; i++)
tmp.insert(S1.data[i]);
for(i = 0; i < S2.top; i++)
tmp.insert(S2.data[i]);
return tmp;
}
set operator-(set& S1, set& S2)
{
set tmp(S1);
for(int i = 0; i < S2.top; i++)
tmp.remove(S2.data[i]);
return tmp;
}
set operator*(set& S1, set& S2){
set tmp;
for (int i=0; i<S2.top; i++)
for (int j = 0; j < S1.top; j++)
{
if(S1.data[i] == S2.data[j])
tmp.insert(S2.data[i]);
}
return tmp;
}
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)
{
set tmp = (S1 - S2) + (S2 - S1);
if( tmp.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";
}
hope that helps!
U.
-
I know...I know. But I do appreciate it.
Your version worked the way I needed when I changed
Set.insert(S2.data[i]); to
Set.insert(S1.data[i]);
Thanks much!!!
-
hehe.. no problem. Keep it up, you're doing well.
U.