Thread: Overloading * operator

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    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!!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i believe it's mistaking that operator as the pointer operator. i don't know more than that, unfortunately.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    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!!!

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Ah LouB, the things I do for you

    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    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!!!

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    hehe.. no problem. Keep it up, you're doing well.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM