KL
Guest
Posts: n/a

 07-02-2005
I am working on a school assignment, so please don't tell me the
solution. I just want some direction.

I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having a bit of a problem in seeing what needs to happen
here. I am just not sure how I do this. Will the overloading function
recognize a < and a usual <? Do I do an
IF (a.letters < b.letters){
return true
}
else {
return false
}

?? Is that the logic I need?

KL

Rolf Magnus
Guest
Posts: n/a

 07-02-2005
KL wrote:

> I am working on a school assignment, so please don't tell me the
> solution. I just want some direction.

You are not posting here very often, right?

> I am supposed to overload the >, <, ==, !=, >=, and <= operators using
> bool. I am having a bit of a problem in seeing what needs to happen
> here.

Well, that depends.

> I am just not sure how I do this. Will the overloading function
> recognize a < and a usual <?

Not sure what you mean. Your operator is just a function, and you can use
everything in it that you can use in any other function.

> Do I do an
> IF (a.letters < b.letters){
> return true
> }
> else {
> return false
> }
>
> ?? Is that the logic I need?

Yes, basically. However, you can leave out the if and just write:

return a.letters < b.letters;

since the result of 'a.letters < b.letters' is just a boolean that is true
or false, depending on the values.

KL
Guest
Posts: n/a

 07-03-2005
Rolf Magnus wrote:
> KL wrote:
>
>> I am working on a school assignment, so please don't tell me the
>> solution. I just want some direction.

>
> You are not posting here very often, right?

That is right. I just post when I get stuck. So basically, not very often.

>> I am supposed to overload the >, <, ==, !=, >=, and <= operators
>> using bool. I am having a bit of a problem in seeing what needs to
>> happen here.

>
> Well, that depends.
>
>> I am just not sure how I do this. Will the overloading function
>> recognize a < and a usual <?

>
> Not sure what you mean. Your operator is just a function, and you can
> use everything in it that you can use in any other function.
>
>> Do I do an
>> IF (a.letters < b.letters){
>> return true
>> }
>> else {
>> return false
>> }
>>
>> ?? Is that the logic I need?

>
> Yes, basically. However, you can leave out the if and just write:
>
> return a.letters < b.letters;
>
> since the result of 'a.letters < b.letters' is just a boolean that is
> true or false, depending on the values.

OK, so when it is called, for example a<b, my parameters (which were already
given) only passes the b. So my question is how to reference the a part.
It could be called anything. How do I reference it in my overloading
function?

Sorry if this seems like a basic question, but just when I think I have that
figured out, it jumps out of my head again.

--

KL

Your argument is sound, nothing but sound. -Benjamin Franklin

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

hbsk
Guest
Posts: n/a

 07-03-2005
when an statement like (a < b) is invoked, it expands to the following:
a.operator<(b); ie, operator < is invoked for object a passing object b
as the parameter. hence you don't have to worry about object a (
provided a and b are objects of the same type/class/struct/union).that
is also the reason you have only one parameter in the overloaded
function.

John Carson
Guest
Posts: n/a

 07-03-2005
"KL" <(E-Mail Removed)> wrote in message
news:42c76c6f\$(E-Mail Removed)
>
> OK, so when it is called, for example a<b, my parameters (which were
> already given) only passes the b. So my question is how to reference
> the a part. It could be called anything. How do I reference it in my
>
> Sorry if this seems like a basic question, but just when I think I
> have that figured out, it jumps out of my head again.

There are two forms for overloaded operators: member operators and
non-member. Suppose we have an Int class. Using member operators, we might
write it as follows:

class Int
{
int x;
public:
Int(int x_) : x(x_)
{}
bool operator<(const Int& rhs)
{
return x < rhs.x;
}
};

Here we see that the member operator gets the left-hand side of the
inequality expression from its own int member variable. The right-hand side
of the inequality is obtained by accessing the int member from the reference
to Int supplied as an argument. We might use this as follows:

#include <iostream>
int main()
{
Int i1(5);
Int i2(7);
if (i1 < i2)
std::cout << "lhs < rhs is true\n";
else
std::cout << "lhs < rhs is false\n";
}

The disadvantage of member operators is that you can only use them when an
object of the relevant class is on the left-hand side of the operator.
Suppose that you wished to compare Int objects with built-in ints.

int main()
{
Int i1(5);
if (9 < i2)
std::cout << "lhs < rhs is true\n";
else
std::cout << "lhs < rhs is false\n";
}

This won't work because the 9 on the left-hand side is not an Int object and
hence operator< from the Int class cannot be called. You can get around this
by using non-member operators and making them friends. In that case, both
the left-hand side and the right-hand side must be arguments of the
operator.

class Int
{
int x;
public:
Int(int x_) : x(x_)
{}
// Member operator when Int is lhs
// Int-Int comparisons
bool operator<(const Int& rhs)
{
return x < rhs.x;
}
// Int-int comparisons
bool operator<(int rhs)
{
return x < rhs;
}
// Use non-member operator when int is lhs. Make it a friend.
// so it can access private members. Note that it has two parameters.
friend bool operator<(int lhs, const Int& rhs);
};

// define non-member operator
bool operator<(int lhs, const Int &rhs)
{
return lhs < rhs.x;
}

Note that, rather than have a mix of member and non-member operators, many
people prefer to only have non-member operators. In any case, we can now
make all possible comparisons:

int main()
{
Int i1(5);
Int i2(7);
std::cout << "Int-Int comparison\n";
if (i1 < i2)
std::cout << "lhs < rhs is true\n";
else
std::cout << "lhs < rhs is false\n";

std::cout << "int-Int comparison\n";
if (9 < i2)
std::cout << "lhs < rhs is true\n";
else
std::cout << "lhs < rhs is false\n";

std::cout << "Int-int comparison\n";
if (i1 < 9)
std::cout << "lhs < rhs is true\n";
else
std::cout << "lhs < rhs is false\n";
}

--
John Carson

KL
Guest
Posts: n/a

 07-03-2005
hbsk wrote:
> when an statement like (a < b) is invoked, it expands to the
> following: a.operator<(b); ie, operator < is invoked for object a
> passing object b as the parameter. hence you don't have to worry
> about object a ( provided a and b are objects of the same
> type/class/struct/union).that is also the reason you have only one
> parameter in the overloaded function.

I am still a bit confused. I don't know why I can't grasp this. Here is
what I have so far:
STRING(string s){

int a;

for (a=0; a<100; a++)

letters[a] = ' ';

a = 0;

length = s.length();

while (a < length) {

letters[a] = s.at(a);

a++;}

};

bool operator<(const STRING&) const;

};

bool operator<(const STRING& b) const {

for (int a=0;a<100;a++){

return (s.letters[a]<b.letters[a];

}

}

I get the feeling that this is not completely correct. Any insights?
--

KL

Your argument is sound, nothing but sound. -Benjamin Franklin

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark P
Guest
Posts: n/a

 07-04-2005
KL wrote:
> hbsk wrote:
>
>>when an statement like (a < b) is invoked, it expands to the
>>following: a.operator<(b); ie, operator < is invoked for object a
>>passing object b as the parameter. hence you don't have to worry
>>about object a ( provided a and b are objects of the same
>>type/class/struct/union).that is also the reason you have only one

>
>
> I am still a bit confused. I don't know why I can't grasp this. Here is
> what I have so far:

I assume this is part of a definition for a class STRING, but you're
missing the beginning:

class STRING {

> STRING(string s){
>

There's really no reason to pass the ctor parameter s by value as this
will require making a duplicate copy lasting only for the duration of
the constructor execution. Try, STRING (const string& s)

> int a;
>
> for (a=0; a<100; a++)
>
> letters[a] = ' ';

Indenting your code will make it easier for others (and you!) to read...

>
> a = 0;
>
> length = s.length();
>
> while (a < length) {
>
> letters[a] = s.at(a);
>
> a++;}

Is letters an array of size 100? If so, this is a risky bit of code if
s is longer than that.
>
> };

If I've counted correctly, the above } closes the ctor definition and
should not be followed by a semicolon.

>
> bool operator<(const STRING&) const;

OK, so you've declared a const member function operator<. Looks good.
>
> };
>
> bool operator<(const STRING& b) const {
>
> for (int a=0;a<100;a++){
>
> return (s.letters[a]<b.letters[a];
>
> }
>
> }

OK, there are a couple problems here. First, your function definition
needs its name fully qualified. This should be:

bool STRING:perator<(const STRING& b) const {

Because the fcn definition is not placed inside the STRING class
definition, the compiler doesn't know that this is a member function of
STRING as opposed to any other class with a < operator.

Fixing that will probably make your code compile (I can't say for sure
since you've left out other parts of the STRING class definition such as
the definition of the array letters) but it probably won't do what you
want it to do. Your for loop will only be executed once because on its
first iteration it encounters a return statement and that's the end of
that. You probably want to check if the characters are equal and if so
move on to the next character before returning a result. Consider aaa <
azz, for example.

Also you need to be careful when comparing characters by value. For
example, it may be that capital Z is less than lowercase a. Is that
there's any guarantee that a < b < c < ... < z. Perhaps someone who
knows these fine details better than I do can comment on this.

>
>
> I get the feeling that this is not completely correct. Any insights?

Yeah. See above

Hope that helps,
Mark

Rolf Magnus
Guest
Posts: n/a

 07-04-2005
KL wrote:

> hbsk wrote:
>> when an statement like (a < b) is invoked, it expands to the
>> following: a.operator<(b); ie, operator < is invoked for object a
>> passing object b as the parameter. hence you don't have to worry
>> about object a ( provided a and b are objects of the same
>> type/class/struct/union).that is also the reason you have only one
>> parameter in the overloaded function.

>
> I am still a bit confused. I don't know why I can't grasp this. Here is
> what I have so far:
> STRING(string s){
>
> int a;
>
> for (a=0; a<100; a++)
>
> letters[a] = ' ';
>
> a = 0;
>
> length = s.length();
>
> while (a < length) {
>
> letters[a] = s.at(a);
>
> a++;}
>
> };
>
> bool operator<(const STRING&) const;
>
> };
>
> bool operator<(const STRING& b) const {
>
> for (int a=0;a<100;a++){
>
> return (s.letters[a]<b.letters[a];
>
> }
>
> }
>
>
> I get the feeling that this is not completely correct. Any insights?

Well, whatever the contents of your STRINGs might be, the for loop is only
run once, because it contains an unconditional return statememt. So what
happens in the loop?

First iteration:
a = 0;
return s.letters[0] < b.letters[0];
Done.

What you probably want for your STRINGs is a lexographical compare. If the
first letter of s is "less" than the first letter of b, you want to return
true. If it's "greater", you want to return false. But if they are equal,
you want to check the second letter and do the same comparison again.
Now try to put the above algorithm in code and try it.
But you're still not done. You have to think about what happens if you reach
the end of one of the strings, since they have probably seldomly both a
lenght of exactly 100 characters. And what happens (what does it mean) if
you reach the end of both of them at the same time?

KL
Guest
Posts: n/a

 07-04-2005
Rolf Magnus wrote:
> KL wrote:
>
>> hbsk wrote:
>>> when an statement like (a < b) is invoked, it expands to the
>>> following: a.operator<(b); ie, operator < is invoked for object a
>>> passing object b as the parameter. hence you don't have to worry
>>> about object a ( provided a and b are objects of the same
>>> type/class/struct/union).that is also the reason you have only one
>>> parameter in the overloaded function.

>>
>> I am still a bit confused. I don't know why I can't grasp this.
>> Here is what I have so far:
>> STRING(string s){
>>
>> int a;
>>
>> for (a=0; a<100; a++)
>>
>> letters[a] = ' ';
>>
>> a = 0;
>>
>> length = s.length();
>>
>> while (a < length) {
>>
>> letters[a] = s.at(a);
>>
>> a++;}
>>
>> };
>>
>> bool operator<(const STRING&) const;
>>
>> };
>>
>> bool operator<(const STRING& b) const {
>>
>> for (int a=0;a<100;a++){
>>
>> return (s.letters[a]<b.letters[a];
>>
>> }
>>
>> }
>>
>>
>> I get the feeling that this is not completely correct. Any insights?

>
> Well, whatever the contents of your STRINGs might be, the for loop is
> only run once, because it contains an unconditional return statememt.
> So what happens in the loop?
>
> First iteration:
> a = 0;
> return s.letters[0] < b.letters[0];
> Done.
>
> What you probably want for your STRINGs is a lexographical compare.
> If the first letter of s is "less" than the first letter of b, you
> want to return true. If it's "greater", you want to return false. But
> if they are equal, you want to check the second letter and do the
> same comparison again.

OK will think on this once I fully wake up

> Now try to put the above algorithm in code and try it.
> But you're still not done. You have to think about what happens if
> you reach the end of one of the strings, since they have probably
> seldomly both a lenght of exactly 100 characters. And what happens
> (what does it mean) if you reach the end of both of them at the same
> time?

I was thinking it wouldn't matter for this excercise, because STRING is
loaded with spaces before it gets data. So.....I can just run it for 100
characters, because once the data is done, the rest will be spaces. Am I ok
in this line of thinking?

--

KL

Your argument is sound, nothing but sound. -Benjamin Franklin

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

KL
Guest
Posts: n/a

 07-04-2005
KL wrote:
> Rolf Magnus wrote:
>> KL wrote:
>>
>>> hbsk wrote:
>>>> when an statement like (a < b) is invoked, it expands to the
>>>> following: a.operator<(b); ie, operator < is invoked for object a
>>>> passing object b as the parameter. hence you don't have to worry
>>>> about object a ( provided a and b are objects of the same
>>>> type/class/struct/union).that is also the reason you have only one
>>>> parameter in the overloaded function.
>>>
>>> I am still a bit confused. I don't know why I can't grasp this.
>>> Here is what I have so far:
>>> STRING(string s){
>>>
>>> int a;
>>>
>>> for (a=0; a<100; a++)
>>>
>>> letters[a] = ' ';
>>>
>>> a = 0;
>>>
>>> length = s.length();
>>>
>>> while (a < length) {
>>>
>>> letters[a] = s.at(a);
>>>
>>> a++;}
>>>
>>> };
>>>
>>> bool operator<(const STRING&) const;
>>>
>>> };
>>>
>>> bool operator<(const STRING& b) const {
>>>
>>> for (int a=0;a<100;a++){
>>>
>>> return (s.letters[a]<b.letters[a];
>>>
>>> }
>>>
>>> }
>>>
>>>
>>> I get the feeling that this is not completely correct. Any
>>> insights?

>>
>> Well, whatever the contents of your STRINGs might be, the for loop is
>> only run once, because it contains an unconditional return statememt.
>> So what happens in the loop?
>>
>> First iteration:
>> a = 0;
>> return s.letters[0] < b.letters[0];
>> Done.
>>
>> What you probably want for your STRINGs is a lexographical compare.
>> If the first letter of s is "less" than the first letter of b, you
>> want to return true. If it's "greater", you want to return false. But
>> if they are equal, you want to check the second letter and do the
>> same comparison again.

>
> OK will think on this once I fully wake up
>
>> Now try to put the above algorithm in code and try it.
>> But you're still not done. You have to think about what happens if
>> you reach the end of one of the strings, since they have probably
>> seldomly both a lenght of exactly 100 characters. And what happens
>> (what does it mean) if you reach the end of both of them at the same
>> time?

>
> I was thinking it wouldn't matter for this excercise, because STRING
> is loaded with spaces before it gets data. So.....I can just run it
> for 100 characters, because once the data is done, the rest will be
> spaces. Am I ok in this line of thinking?

Ixnay that last bit! I think I got it figured out....mostly. Problem is I
get a true value returned no matter what. Could you look at this and help
me see where my reasoning ran out the door?

bool operator<(const STRING& b) const{

for (int a=0;a<100;a++){

if( letters[a] != b.letters[a]){

return (letters[a]<b.letters[a]);

}

}

};

--

KL

Your argument is sound, nothing but sound. -Benjamin Franklin

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----