i need help using the following .h file to
Write a definition of a 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


This is the .h file..

#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

I thank you for any help or advice you may have.

1. Move all function definitions to a separate .cpp file (and never, ever include them in .h files, except class templates - but it's the other story).
2. Think about month[i] = !month[i]

Comments
thanks alot

the cpp file we have created is as follow...

#include <iostream>
#include "monthsSet.h"

using namespace std;

int main()
{
	cout << "**** Running testMonths.cpp ****\n\n";
	monthsSet A, B;
	A.insertMonth(1);
	A.insertMonth(5);
	A.insertMonth(10);
	A.insertMonth(11);
	A.insertMonth(12);
	cout << "Set A= ";
	A.print();
	B.insertMonth(1);
	B.insertMonth(2);
	B.insertMonth(5);
	B.insertMonth(6);
	B.insertMonth(9);
	cout << "Set B= ";
	B.print();

	cout << "Set A cardinality is = " << A.cardinality() << endl;
	cout << "Set B cardinality is = " << B.cardinality() << endl;

	A.unionSet(B);
	//A += B;
	cout << "After unioned B in A, set A is = ";
        A.print();

	A.differenceSet(B);
	//A -= B;
	cout << "After substracted B from A, set A is = ";
        A.print();
	
	A.intersectionSet(B);
	cout << "After intersected A with B, set A is = ";
        A.print();

	A.copySet(B);
	//A = B;
	cout << "After copying B in A, the set A is = ";
        A.print();
	
	if ( A.equals(B) )
	//if ( A == B )
		cout << "After comparing sets A and B, they are the same" << endl;
	else 
		cout << "After comparing sets A and B, they are different" << endl;

	return 1;
}

for the function defenition i was thinking around the lines of..

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

am i working along similar lines? or am i completly off.?

You have two threads on the same topic.

http://www.daniweb.com/forums/thread176040.html

Mark one solved so people know where to comment. Yes, you are way off. Read my post on the other thread for some of the reasons why.

I don't think this is what ArkM meant by the separate cpp file (what you have is a driver/tester cpp file with a main function. I assume the file he is referring to is a class member function implementation cpp file WITHOUT a main function). It's good advice, but it involves separate compilation. You might want to tackle that later and just keep it all in the .h file like you have it now, at least for now.

Beyond the problems I mentioned in the other thread, even after you fix them, your function won't work since you haven't written the - operator function yet, so do that first.

Comments
you are so helpful. thanks a bunch

thanks a whole lot!

You have two threads on the same topic.

http://www.daniweb.com/forums/thread176040.html

Mark one solved so people know where to comment. Yes, you are way off. Read my post on the other thread for some of the reasons why.

I don't think this is what ArkM meant by the separate cpp file (what you have is a driver/tester cpp file with a main function. I assume the file he is referring to is a class member function implementation cpp file WITHOUT a main function). It's good advice, but it involves separate compilation. You might want to tackle that later and just keep it all in the .h file like you have it now, at least for now.

Beyond the problems I mentioned in the other thread, even after you fix them, your function won't work since you haven't written the - operator function yet, so do that first.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.