Overloading + (plus) operator

Started by
12 comments, last by Aspirer 13 years, 5 months ago
Hi everyone,

I'm doing some college work for a friend and I've come to problem I'm short to solve.
The trouble point is:
I'm supposed to overload + (plus) operator so it unites two sets. Both sets are string arrays.
That means if a.elements string array contains "1" and "x" and b.elements string array contains "2", "y" and "x", c = a + b should make c.elements "1", "x", "2", "y".
Both string arrays are created in heap.
I can copy the whole code if needed.
PS: I can't use containers.
PPS: I'm using string type, not char[][].

Thanks for any advices.
stejkenzie
Advertisement
We cannot give you an answer for homework questions. If you have an implementation that is nearly there, and you have a specific problem with it, then we can give you hints as to how to proceed.

Your "friend" would be better served by doing their own homework.
So, what specifically don't you understand?
2rip-off: That's what would I say to him if I wasn't newcomer to C++ on my own. I need as many experience I can get.

2nyxor: I need little chunk of code that would solve this problem. An explanatory one so I would understand it in first place, not just copy it.

I've never overloaded operator before and the examples I found are either with integers or with different operators.
You cannot overload an operator on a built-in type, such as arrays of strings.

You could create a set<> class, then overload the operator on that type:
class set {   // ...};set operator+(const set &a, const set &b){   // union implementation}

Note that operator overloading is not a great idea on types where there is no convention for what it means. I would use an explicit "set_union" function were I implementing this (I would use "union" but it is a keyword).

When you say you cannot use containers, does that mean you cannot use the existing set<> class in the Standard C++ Library?
I commented the code so you would (hopefully) understand.
It's a bit longer, but I think most of it makes sense.
I've got many ideas of how to overload the operator, but none of them seems to work and the program always crashes on the same line which is c = a + b;
Please, analyze this and gimme some hint or code for the operator.

#include <iostream>#include <sstream>using namespace std;class Mnozina{	//mnozina == setpublic:	Mnozina();	Mnozina(int countPrvku);	//takes parameter number of elemets	void vypis();				//print elements	int getVelikost();			//get size - not implemented yet	Mnozina operator+ (Mnozina param);private:	int pocetPrvku;				//number of elements	string* prvky;				//elements stored in the array of strings on the heap};Mnozina::Mnozina(){				//i'm not sure if this constructor shouldn't be doing something}Mnozina Mnozina::operator+ (Mnozina param){	//this is the main problem	Mnozina soucet;							//temp set	int iter;								//number of elements of the new string array	for(iter = 0; iter < (param.pocetPrvku); iter++){		//copies all elements of b set to tep set		for(int i = 0; i <= iter; i++){						//if the element is not there			if(soucet.prvky[iter] != param.prvky)		//yet				soucet.prvky = new string[iter];							soucet.prvky[iter] = param.prvky;		}	}	for(; iter < this->pocetPrvku; iter++){	//the same code as above, but adds it to the end (like vector pushack())		for(int i = 0; i <= iter; i++){			if(soucet.prvky[iter] != this->prvky)				soucet.prvky = new string[iter];				soucet.prvky[iter] = this->prvky;		}	}	return soucet;}Mnozina::Mnozina(int countPrvku){	//prompt for elements	pocetPrvku = countPrvku;	prvky = new string[pocetPrvku];	for(int i = 0; i < pocetPrvku; i++){		cout << "prvek " << i + 1 << "/" << pocetPrvku << " ";		cin >> prvky;	}}void Mnozina::vypis(){				//print elements	for(int i = 0; i < pocetPrvku; i++){		cout << prvky << " ";	}}int main(int argc, char **argv){	// main() is assigned, there can be no changes in here	if(argc != 3)	{		cerr << "Program prijima 2 short parametry" << endl;	//"program take 2 short parameters"		return 0;	}	cout << "Zadejte prvky pro mnoziny a a b:"<< endl;	//"enter elements for sets a and b"	Mnozina a(atoi(argv[1])); Mnozina b(atoi(argv[2]));	Mnozina c,d,e,f;	cout << "Prvky a: "; a.vypis(); cout << endl;		cout << "Prvky b: "; b.vypis(); cout << endl;	cout << "Sjednoceni c=a+b: ";	c=a+b; c.vypis();	// this is the point where the program fails}


Thanks, stejkenzie
"for(; iter < this->pocetPrvku; iter++){"

is this intentional?

The thing to remember about operator overloading is that is exactly writing any other function in C++. If you know the behavior you want and could write the function if it were just called add, then that is exactly what you would write for operator +.

if(soucet.prvky[iter] != param.prvky)]
are we sure this line is valid? you just instantiated soucet a few lines back and your constructor doesn't set prvky to point to anything so that pointer isn't valid.
2Kalten: it was, until you pointed it out. Now I can see it's not actually working, cuz of the condition.

2stonemetal: nice, I didn't notice that one. I'm creating first element after I check if it doesn't contain some value. Dammit!

Still, I kinda feel like this is the worst piece of code you could ever write for operation like this. Ain't there any way to use the built-in string + operator?

[Edited by - stejkenzie on November 15, 2010 3:03:11 AM]
Your if statements lack braces, the indentation does not match what the compiler sees.

Here is another problem:
soucet.prvky = new string[iter];soucet.prvky[iter] = this->prvky;

You aren't allocating enough objects to index prvky like this. Remember that if you have an array of size N you can only index from 0 to N - 1.

One thing you might consider is to separate the memory logic from the set logic. You could use std::vector<> as a placeholder until you have the set working, then replace this with a custom dynamic array class if that is part of your requirements.

Your algorithm is complex because your set class does not enforce uniqueness. If you write a memeber function too add new elements that will discard duplicate elements, this could be used in the constructor and in operator+ (remember: such an invariant must be enforced *everywhere* elements are added).

Your algorithm could then be:
Set operator+(const Set &a, const Set &b){    Set result = a;    for each(Element e in b)    {        result.add(e);    }    return result;}

Note that this is a free function, it does not need to access private data.

This topic is closed to new replies.

Advertisement