Click to See Complete Forum and Search --> : Question about Bool and Operator ==


Arcane_Disciple
02-23-2003, 02:13 PM
*Note: I am not asking for exact code for the second part of my question. I want a plain english explaination.

Here is my header:

class intEntry
{
public:
intEntry(int v, int c = 1);
// initializes the integer value and its count
int getValue() const; //return value
void increment(); //increment count
friend bool operator< (const intEntry& lhs,const intEntry& rhs);
friend bool operator== (const intEntry& lhs,const intEntry& rhs);
//compare lhs and rhs using value
friend ostream& operator<< (ostream& ostr, const intEntry& obj);
//output obj in format
//"value value ... value" (count times)

private:
int value; //integer value
int count; //number of occurrences of value
};

intEntry::intEntry(int,int)
{};
//*************************
int intEntry::getValue() const
{
return value;
};
//*************************
void intEntry::increment()
{
count++;
};
//*************************


Question One: How do you add the the bool lines to my class structure? (This is the only part I ask for exact code. This is a very general thing.

Question Two: How does a bool setup print something? From what I have been taught, bool is used to test true or false.

Thanks.

bwkaz
02-23-2003, 06:16 PM
I don't really understand what you're asking... what "bool lines" are you talking about?

I can write the operator< and operator== functions if you want, but I don't think you want me to.

You are going to want to actually have code in your constructor, though, too...

Arcane_Disciple
02-23-2003, 08:18 PM
I have friend bool operator< and == up in the public area. How do I implement them?

is it something like bool intEntry::operator<(...) ?

If it would be easier to show it with code, then go for it. This isn't needed for any projects. It was just an assignment on to see if we could setup a generic header file. (Hence the empty constructor) We were just given the class and told to break it up into the functions. I asked the 2nd part becuase I figured it would come up again and its better to get it out of the way now.

bwkaz
02-23-2003, 09:19 PM
It'd be:

bool operator<(const intEntry& lhs, const intEntry& rhs)
{
return(lhs.value < rhs.value); // or something... I'm not sure what the count's are supposed to be
}

bool operator==(const intEntry& lhs, const intEntry& rhs)
{
return(lhs.value == rhs.value); // again, or something
} In part since you declared that the operator functions were friends. If you don't declare them as a friend, they'll be members of the intEntry class, the declarations could have a const after them, and the functions would look like:

bool intEntry::operator<(const intEntry& rhs)
{
return(value < rhs.value); // yet again, or something
} operator== would be similar.

Arcane_Disciple
02-24-2003, 12:02 AM
thank you much. i was sitting there trying to make them work as members when they were friends.

I almost forgot, this was the line i was talking about for printing:"

friend ostream& operator<< (ostream& ostr, const intEntry& obj);
//output obj in format
//"value value ... value" (count times)

bwkaz
02-24-2003, 10:28 AM
That function would just be a for loop that prints out obj.value, then a space, every time through it. You get to figure out the bounds on the loop. ;)

I'm not sure I see why the bool type has anything to do with it, though...

Arcane_Disciple
02-24-2003, 11:07 AM
It was my selective reading again :D

When I saw the operator I was also "seeing" a bool out there. Once again I manage to make a simple thing hard.