Operator Overloading for Dynamic Arrays

I need to overload operators for this integers sets program that uses dynamic arrays to store integers. The program must compare two integer sets and show their cardinality, intersections, unions, etc etc. Ive got the basic functions for completing these tasks but now i need to compare these member functions into assignment operator functions. Any ideas on how i would go about doing this? Any information would be appreciated.

Also i am planning to use two objects (each containing its own integer set) and then comparing them through operator overloading which really should do the same job these functions do below.

The basic program i have so far looks like this:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>


class Integer
{
private:
int *ptr1,*ptr2,size1,size2;
public:
Integer(int=0,int=0);
~Integer();
void inputValues(int,int);
void outputValues(int,int);
void showCardinality(int,int);
void showUnion(int,int);
void showIntersection(int,int);
void showCartesianProduct(int,int);
void showEquality(int,int);
void showDifference(int,int);
};

Integer::Integer(int size1,int size2)
{

ptr1=new int[size1];
ptr2=new int[size2];

for(int a=0; a<size1; a++)
{ ptr1[a]=0; }
for(int a=0; a<size2; a++)
{ ptr2[a]=0; }
}

Integer::~Integer()
{
delete[]ptr1;
delete[]ptr2;
}

void Integer::inputValues(int size1,int size2)
{

cout<<"Please enter the "<<size1<<" values in your first integer set."<<endl;
for(int a=0; a<size1; a++)
{ cin>>ptr1[a]; }
cout<<"Please enter the "<<size2<<" values in your second integer set."<<endl;
for(int a=0; a<size2; a++)
{ cin>>ptr2[a]; }

}

void Integer::outputValues(int size1,int size2)
{ cout<<endl;

cout<<"The values in your first integer set are:"<<endl;
for(int a=0; a<size1; a++)
{ cout<<ptr1[a]<<" "; } cout<<endl;
cout<<"The values in your second integer set are:"<<endl;
for(int a=0; a<size2; a++)
{ cout<<ptr2[a]<<" "; }
cout<<endl;
}

void Integer::showCardinality(int size1,int size2)
{ cout<<endl;
cout<<"The cardinality of your first set is "<<size1<<"."<<endl;
cout<<"The cardinality of your second set is "<<size2<<"."<<endl;
}

void Integer::showUnion(int size1,int size2)
{
int a; cout<<endl;
cout<<"The Union of Sets is: ";
for(int i=0; i<1000000; i++)
{
for(a=0; a<size1; a++)
{ if(ptr1[a]==i) { cout<<i<<" "; break; } }
if(a<size1) { continue; }
for(a=0; a<size2; a++)
{ if(ptr2[a]==i) { cout<<i<<" "; break; } }
} cout<<endl;
}

void Integer::showIntersection(int size1,int size2)
{ int a,b; cout<<endl;

cout<<"The Intersection of Sets is: ";
for(a=0; a<size1; a++)
{
for(b=0; b<size2; b++)
{
if(ptr1[a]==ptr2[b]) { cout<<ptr1[a]<<" "; break; }
}
} cout<<endl;
}


void Integer::showCartesianProduct(int size1,int size2)
{ int a,b; cout<<endl;

cout<<"The Cartesian Product of Set AxB= [";
for(a=0; a<size1; a++)
{
for(b=0; b<size2; b++)
{ cout<<"{"<<ptr1[a]<<","<<ptr2[b]<<"}"; }
} cout<<"]"<<endl;
cout<<endl;
cout<<"The Cartesian Product of Set BxA= [";
for(b=0; b<size2; b++)
{
for(a=0; a<size1; a++)
{ cout<<"{"<<ptr2[b]<<","<<ptr1[a]<<"}"; }
} cout<<"]"<<endl;
}


void Integer::showEquality(int size1,int size2)
{ int a,b,c; cout<<endl;
c=0;
for(a=0; a<size1; a++)
{
for(b=0; b<size2; b++)
{
if(ptr1[a]==ptr2[b]) { break; }
}
if(b>=size2) { c++; break; }
}
if(c==1) { cout<<"The two sets are not equal."<<endl; }

for(b=0; b<size2; b++)
{
for(a=0; a<size1; a++)
{
if(ptr2[b]==ptr1[a]) { break; }
}
if(a>=size1) { c++; break; }
}
if(c==1) { cout<<"The two sets are not equal."<<endl; }
else if (c==2) { cout<<""; }
else cout<<"The two sets are equal."<<endl;
}


void Integer::showDifference(int size1,int size2)
{ int a,b; cout<<endl;

cout<<"The difference of A-B: ";
for(a=0; a<size1; a++)
{
for(b=0; b<size2; b++)
{
if(ptr1[a]==ptr2[b]) { break; }
}
if(b>=size2) { cout<<ptr1[a]<<" "; }
} cout<<"-";

cout<<endl;
cout<<"The difference of B-A: ";
for(b=0; b<size2; b++)
{
for(a=0; a<size1; a++)
{
if(ptr2[b]==ptr1[a]) { break; }
}
if(a>=size1) { cout<<ptr2[b]<<" "; }
}
cout<<"-";
}


void main()
{
int size1=0,size2=0;
cout<<"Enter the size of your first Integer set."<<endl;
cin>>size1;
cout<<"Enter the size of your second Integer set."<<endl;
cin>>size2;

Integer sets;


sets.inputValues(size1,size2);
sets.outputValues(size1,size2);
sets.showCardinality(size1,size2);
sets.showUnion(size1,size2);
sets.showIntersection(size1,size2);
sets.showCartesianProduct(size1,size2);
sets.showEquality(size1,size2);
sets.showDifference(size1,size2);

cin.ignore();
cin.get();

}
Well, first things first, do you know how to overload an operator inside a class in C++? If not, the basic syntax (for most, but not all operators) is:

1
2
3
4
5
6
7
//Declaration
return_type operator the_operator (right_hand_type);

//Implementation outside of the class
return_type classname::operator the_operator (right_hand_type argument_name) {
   //Code here.
}


The operator will expect an object of type of classname on the left hand side, and just like all the member functions, if you declare it inside your class, you'll be able to access all the members. :)

Out of curiousty, which operators are you going to overload?

Oh, one last thing... void main(), I'm sorry to say, isn't proper C++. main() should always return an int.

Good luck!

-Albatross
Last edited on
Im looking to overload the difference(-), cartesian product(*), Equality(==), Intersection(+). Ive already attempted the equality operator but its really not working as well as id hoped. There are no errors but its still does not work right. The program runs and gets as far as the line ive underlined. I dont think the program runs after that line because its highlighted in blue after the run window closes. Why isnt the programming running after that? Im pretty sure im making a very blatant error. Thanks for your help.


So far it looks like this:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>


class Integer
{
private:
int *ptr,size;
public:
Integer(int=0);
~Integer();
int operator == (Integer &);
void inputValues(int,int);
void outputValues(int,int);
void showCardinality(int,int);
void showUnion(int,int);
void showIntersection(int,int);
void showCartesianProduct(int,int);
void showEquality(int,int);
void showDifference(int,int);
};

Integer::Integer(int size)
{

ptr=new int[size];

for(int a=0; a<size; a++)
{ ptr[a]=0; }
}

Integer::~Integer()
{
delete[]ptr;
}

void Integer::inputValues(int size,int b)
{

cout<<"Please enter the "<<size<<" values in your "<<b<<" integer set."<<endl;
for(int a=0; a<size; a++)
{ cin>>ptr[a]; } cout<<endl;
}

void Integer::outputValues(int size,int b)
{ cout<<endl;

cout<<"The values in your "<<b<<" integer set are:"<<endl;
for(int a=0; a<size; a++)
{ cout<<ptr[a]<<" "; } cout<<endl;
}

void Integer::showCardinality(int size,int a)
{ cout<<endl;
cout<<"The cardinality of your "<<a<<" first set is "<<size<<"."<<endl;
}

int Integer::operator == (Integer &t)
{
int a,b,c; cout<<endl;
c=0;
for(a=0; a<size; a++)
{
for(b=0; b<t.size; b++)
{
if(ptr[a]==t.ptr[b] }[/u]) { break; }[/u]
}
if(b>=t.size) { c++; break; }
}
if(c==1) { return 0; }

for(b=0; b<t.size; b++)
{
for(a=0; a<size; a++)
{
if(t.ptr[b]==ptr[a]) { break; }
}
if(a>=size) { c++; break; }
}
if(c==1) { return 0; }
else if (c==2) { return 0; }
else return 1;
}


int main()
{
int size1=0,size2=0;
cout<<"Enter the size of your first Integer set."<<endl;
cin>>size1;
cout<<"Enter the size of your second Integer set."<<endl;
cin>>size2;

Integer set1(size1),set2(size2);


set1.inputValues(size1,1);
set2.inputValues(size2,2);
set1.outputValues(size1,1);
set2.outputValues(size2,2);
set1.showCardinality(size1,1);
set2.showCardinality(size2,2);

if(set1==set2) { cout<<"Both sets are equal."<<endl; }
else cout<<"Both sets are not equal."<<endl;



cin.ignore();
cin.get();



}
That operator == looks a bit... complicated. I haven't tested it, though, because I noticed something about your constructor.

You're never setting the member size of your class to anything sane. In fact, you have a variable in your constructor of the same name which conflicts with the member variable. While it'll compile, it makes getting to the member variable a bit trickier (though it's still possible and not that difficult).

You may want to rename one of those variables so that you can set Integer's member variable properly. :)

EDIT: OH! What compiler are you using? It seems to be supporting some... pre-standard features.

-Albatross
Last edited on
Dude it worked! I changed the variable in the constructor amazingly it just worked glitch free. Thanks for all your help. If i need anymore help, i know who im coming to (sorry in advance).

im using borland c++ 5.02
Last edited on
Topic archived. No new replies allowed.