Hi. was asked to add the definitions of the function to overload the operators from this:

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

Ive been trying for hours. Ive read my textbook and gone over my notes. Unfortunately i have been unsuccessful. any help would be greatly appreciated. Thank you in advance.

Recommended Answers

All 4 Replies

void monthsSet::unionSet (monthsSet &B, int idx, bool val)
{
    B.months[idx] = val;
}

umm..sorry..:D

operator=(Contoh &B)

so thats how i would write that function with it overloading opertator?

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. =)

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.