I was given this .h file to implement with a cpp file.
the directions say to add the definitions of the functions to overload the operators

+=, which implements the same operation as the unionSet(monthsSet &B) function;
-=, which implements the same operation as the differenceSet(monthsSet &B) function;
==, which implements the same operation as the equalsSet(monthsSet &B) function;
=, which implements the same operation as the copySet(monthsSet &B) function, in other words,
implements the assignment operator

the .h file is as follows..

#ifndef H_monthsSet
#define H_monthsSet

#include <iostream>
using namespace std;

class monthsSet
{
private:
    bool months[12];

public:
    monthsSet();
    int cardinality();
    void print();
    bool membership(int month);
    void differenceSet(monthsSet &B);
    void unionSet(monthsSet &B);
    void intersectionSet(monthsSet &B);
    bool equals(monthsSet &B);
    void copySet(monthsSet &B);
    bool emptySet();
    void clearSet();
    void insertMonth(int month);
    void deleteMonth(int month);
};

monthsSet::monthsSet()
{
    for (int i=0; i<12; i++)
        months[i] = false;
};

int monthsSet::cardinality()
{
    int count=0;
    for (int i=0; i<12; i++)
    {
        if (months[i])
            count++;
    }
    return count;
};

void monthsSet::print()
{
    for (int i=0; i<12; i++)
    {
        if (months[i])
            switch (i)
        {
            case 0:
                cout << "january "; break;
            case 1: cout << "february "; break;
            case 2: cout << "march "; break;
            case 3: cout << "april "; break;
            case 4: cout << "may "; break;
            case 5: cout << "june "; break;
            case 6: cout << "july "; break;
            case 7: cout << "august "; break;
            case 8: cout << "september "; break;
            case 9: cout << "october "; break;
            case 10: cout << "november "; break;
            default: cout << "december"; break;
        }
    }
    cout << endl;
};

bool monthsSet::membership(int month)
{
    return (months[month]);
};

void monthsSet::differenceSet(monthsSet &B)
{
    for (int i=0; i<12; i++)
        if (B.months[i])
            months[i] = false;
};

void monthsSet::unionSet (monthsSet &B)
{
    for (int i=0; i<12; i++)
        if (B.months[i])
            months[i] = true;
};

void monthsSet::intersectionSet (monthsSet &B)
{
    for (int i=0; i<12; i++)
        if (months[i] && !B.months[i])
            months[i] = false;
};

bool monthsSet::equals (monthsSet &B)
{
    for (int i=0; i<12; i++)
        if (months[i] != B.months[i])
            return false;
    return true;
};

void monthsSet::copySet (monthsSet &B)
{
    for (int i=0; i<12; i++)
        if (B.months[i])
            months[i] = true;
        else
            months[i] = false;
};

bool monthsSet::emptySet ()
{
    for (int i=0; i<12; i++)
        if (months[i])
            return false;
    return true;
};

void monthsSet::clearSet ()
{
    for (int i=0; i<12; i++)
        months[i] = false;
};

void monthsSet::insertMonth(int month)
{
    months[month-1] = true;
};

void monthsSet::deleteMonth(int month)
{
    months[month-1] = false;
};

#endif

The problem is i dont quite understand the directions. i just need an example or two to by better informed. Again any help is greatly appreciated and i thank you in advance. =)

Recommended Answers

All 11 Replies

You may want to simplify the question to a single case. Once you get an answer you should be able to apply it to the rest of the cases. It just is overwhelming to people reading to look at 1000 lines of code that you post. Also, please wrap the code in CODE tags.

do the overloaded operators need to be implemented as members of the class, or could they be implemented 'outside' the class?

(I think in class may be required or recommended)

Do you need to have the operators return a value?

Code tags please:

[code=cplusplus] // paste code here

[/code]

The object is to be able to do this in main:

monthsSet a;
monthsSet b;
a += b;

so it compiles and runs.

Currently you have to do this:

monthsSet a;
monthsSet b;
a.unionSet (b);

There are numerous links on the web on how to implement operator overloading. Here's one.

http://en.wikibooks.org/wiki/C++_Programming/Operator_Overloading

was given this .h file to implement with a cpp file.
the directions say to add the definitions of the functions to overload the operators

+=, which implements the same operation as the unionSet(monthsSet &B) function;
-=, which implements the same operation as the differenceSet(monthsSet &B) function;
==, which implements the same operation as the equalsSet(monthsSet &B) function;
=, which implements the same operation as the copySet(monthsSet &B) function, in other words,
implements the assignment operator

the .h file is as follows..

#ifndef H_monthsSet
#define H_monthsSet

#include <iostream>
using namespace std;

class monthsSet
{
private:
bool months[12];

public:
monthsSet();
int cardinality();
void print(); 
bool membership(int month);
void differenceSet(monthsSet &B);
void unionSet(monthsSet &B);
void intersectionSet(monthsSet &B);
bool equals(monthsSet &B);
void copySet(monthsSet &B);
bool emptySet();
void clearSet();
void insertMonth(int month);
void deleteMonth(int month);
};

monthsSet::monthsSet()
{
for (int i=0; i<12; i++)
months[i] = false;
};

int monthsSet::cardinality()
{
int count=0;
for (int i=0; i<12; i++)
{
if (months[i])
count++;
}
return count;
};

void monthsSet::print()
{
for (int i=0; i<12; i++)
{ 
if (months[i])
switch (i)
{
case 0: 
cout << "january "; break;
case 1: cout << "february "; break;
case 2: cout << "march "; break;
case 3: cout << "april "; break;
case 4: cout << "may "; break;
case 5: cout << "june "; break;
case 6: cout << "july "; break;
case 7: cout << "august "; break;
case 8: cout << "september "; break;
case 9: cout << "october "; break;
case 10: cout << "november "; break;
default: cout << "december"; break;
}
}
cout << endl;
};

bool monthsSet::membership(int month)
{
return (months[month]); 
};

void monthsSet::differenceSet(monthsSet &B)
{
for (int i=0; i<12; i++)
if (B.months[i])
months[i] = false;
};

void monthsSet::unionSet (monthsSet &B)
{
for (int i=0; i<12; i++)
if (B.months[i])
months[i] = true;
};

void monthsSet::intersectionSet (monthsSet &B)
{
for (int i=0; i<12; i++)
if (months[i] && !B.months[i])
months[i] = false;
};

bool monthsSet::equals (monthsSet &B)
{
for (int i=0; i<12; i++)
if (months[i] != B.months[i])
return false;
return true;
};

void monthsSet::copySet (monthsSet &B)
{
for (int i=0; i<12; i++)
if (B.months[i])
months[i] = true;
else
months[i] = false;
};

bool monthsSet::emptySet ()
{
for (int i=0; i<12; i++)
if (months[i])
return false;
return true;
};

void monthsSet::clearSet ()
{
for (int i=0; i<12; i++)
months[i] = false;
};

void monthsSet::insertMonth(int month)
{
months[month-1] = true;
};

void monthsSet::deleteMonth(int month)
{
months[month-1] = false;
};

#endif

The problem is i dont quite understand the directions. i just need an example or two to by better informed. Again any help is greatly appreciated and i thank you in advance. =)

VernonDozier has already given you the directions, if you have taken a look at them you would understand it on your own.
To tell you in brief about Operator Overloading : It is to attach a new/different meaning to an already existing operator in the language. This is called overloading because doing this seems a bit like putting extra meaning to the same operator in a way making excess use of it, in a way "overloading" it with functions that it is supposed to do.

>+=, which implements the same operation as the unionSet(monthsSet &B) function;

So now in the assignment given by you, you are supposed to add more meaning to the += binary operator in C++ and attach one more meaning/function to it of creating an unionSet. Note I say here "one more" because of the reason that the operator is already supposed to do one function already assigned to it by the C++ language that of adding the operand on the LHS of it to the operand on the RHS of it and then assigning the sum to the operand on the LHS, as in :

a+=b;

So you now have to create a function similar in signature and operation to the function unionSet and name it is "+=" so when you call it on objects it has the same effect as that of :

a.unionSet(b);

This is exactly what VernonDozier mentions in his post. I hope you can work your way with the others the same way.

Thank you all for your help. Taking your advice into consideration, would the second case be as follows.

monthsSet a;
monthsSet b;
a.differenceSet (b);

I think i finally got it. :)

umm if i could just ask for a bit more help.
another assignment using the same .h file asks for me to
Write the definition of the member function void complementSet(); which for a set A, the complement of A is the
difference of U-A, where U is the universe set.
Would the universe set be the monthsSet istself??
Thank you all very much for all your help

Thank you all for your help. Taking your advice into consideration, would the second case be as follows.

monthsSet a;
monthsSet b;
a.differenceSet (b);

I would think so.

I think i finally got it. :)

umm if i could just ask for a bit more help.
another assignment using the same .h file asks for me to
Write the definition of the member function void complementSet(); which for a set A, the complement of A is the
difference of U-A, where U is the universe set.
Would the universe set be the monthsSet istself??
Thank you all very much for all your help

I imagine the universe set is when months[i] = true for all i.

To explain you the concept of universe in set theory, will have to give you an example.
Say, you have a set of men who play football, a set of men who play cricket, and set of men who play baseball. Here the universe ia set of all men, some of which play football, some of which play cricket, some of which play baseball etc, etc.
Now let us apply this example to a few men
Universe = {A, B, C, D, E, F} // set of all men (universe set)
Football = {F} // men who play football
Cricket = {C} // men who play cricket
Baseball = {B} // men who play baseball

Now as mentioned by you in your post the complement of a set is U- (that set), so in our example the complement of set Football is
Universe - Football = {A, B, C, D, E, F} - {F} = {A, B, C, D, E}

A universe set is in the context of the example. For a different example which also includes men and women players, your universe set would be the set of all human beings (Men + Women)

I hope you are able to figure out what a universal or universe set is, and also the complement operation.

Would my assignment by like the following.

void complementSet(&b);
{
monthsSet a==months[i]-b
}

when months= is true for all months?
Im sorry if i sound so mixed up. C++ has proven to
be something very difficult for me to understand. I cant thank you
enough for all of you help.

Would my assignment by like the following.

void complementSet(&b);
{
monthsSet a==months[i]-b
}

when months= is true for all months?
Im sorry if i sound so mixed up. C++ has proven to
be something very difficult for me to understand. I cant thank you
enough for all of you help.

void complementSet(&b);
{
monthsSet a==months[i]-b
}

Error 1 - semicolon in line 1. Is this a prototype or the function itself? Get rid of the semicolon.

Error 2 - a is a local variable. It goes out of scope immediately when the function goes out of scope. Nothing is returned, so this function will likely do nothing.

Error 3 - What is b's type?

Error 4 - What is i?

Error 5 - Do you want == or = here, and have you written either of them?

Error 6 - (only an error if this code is outside of the class) - Is this a class member function? Look at your other class member functions (see red):

void monthsSet::differenceSet(monthsSet &B)

Error 7 - You are subtracting (I assume) a monthsSet object from a bool , and I assume you don't intend to do that. Subtract a monthsSet object from a monthsSet object.


Do one thing at a time. Implement ONE of the operators so it runs and compiles perfectly, then do the others. Post your updated code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.