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.
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]; }
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 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;
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_typeoperatorthe_operator (right_hand_type);
//Implementation outside of the class
return_typeclassname::operatorthe_operator (right_hand_typeargument_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.
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.
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;
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.
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).