Overloading Operators

Discussion in 'C++' started by KL, Jul 2, 2005.

  1. KL

    KL Guest

    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
     
    KL, Jul 2, 2005
    #1
    1. Advertising

  2. KL

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jul 2, 2005
    #2
    1. Advertising

  3. KL

    KL Guest

    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 =----
     
    KL, Jul 3, 2005
    #3
  4. KL

    hbsk Guest

    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.
     
    hbsk, Jul 3, 2005
    #4
  5. KL

    John Carson Guest

    "KL" <> wrote in message
    news:42c76c6f$
    >
    > 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.


    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
     
    John Carson, Jul 3, 2005
    #5
  6. KL

    KL Guest

    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 =----
     
    KL, Jul 3, 2005
    #6
  7. KL

    Mark P Guest

    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:


    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::eek: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
    what you want? Also I'm not sure about this, but I don't know 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
     
    Mark P, Jul 4, 2005
    #7
  8. KL

    Rolf Magnus Guest

    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?
     
    Rolf Magnus, Jul 4, 2005
    #8
  9. KL

    KL Guest

    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, Jul 4, 2005
    #9
  10. KL

    KL Guest

    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 =----
     
    KL, Jul 4, 2005
    #10
  11. KL

    Mark P Guest

    KL wrote:
    > 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]);
    >
    > }
    >
    > }
    >
    > };
    >
    >
    >

    I don't see why this whould always return true, but it still has a
    problem. What happens if the two strings are equal? What (if anything,
    *hint*) gets returned?
     
    Mark P, Jul 4, 2005
    #11
  12. KL

    KL Guest

    Mark P wrote:
    > KL wrote:
    >> 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]);
    >>
    >> }
    >>
    >> }
    >>
    >> };
    >>
    >>
    >>

    > I don't see why this whould always return true, but it still has a
    > problem. What happens if the two strings are equal? What (if
    > anything, *hint*) gets returned?


    DOH! It is always the little things.....and I don't get why I always get a
    true value, but I do...gonna ponder on that some more and work on a
    different return for ....... uh oh....it is a bool, it can only return true
    or false. So I can't test for strings that are equal....or can I?

    --

    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, Jul 4, 2005
    #12
  13. KL

    Rolf Magnus Guest

    KL wrote:

    >> I don't see why this whould always return true, but it still has a
    >> problem. What happens if the two strings are equal? What (if
    >> anything, *hint*) gets returned?

    >
    > DOH! It is always the little things.....and I don't get why I always get
    > a true value, but I do...gonna ponder on that some more and work on a
    > different return for ....... uh oh....it is a bool, it can only return
    > true or false. So I can't test for strings that are equal....or can I?


    The question was what your operator returns in the case of both being equal.
    How often does your loop iterate? What happens as last thing? What is
    returned? What is it supposed to return?

    And btw: You cannot test the strings for equality with one call to your
    operator<, but with two, you can.
     
    Rolf Magnus, Jul 4, 2005
    #13
  14. KL

    KL Guest

    Rolf Magnus wrote:
    > KL wrote:
    >
    >>> I don't see why this whould always return true, but it still has a
    >>> problem. What happens if the two strings are equal? What (if
    >>> anything, *hint*) gets returned?

    >>
    >> DOH! It is always the little things.....and I don't get why I
    >> always get a true value, but I do...gonna ponder on that some more
    >> and work on a different return for ....... uh oh....it is a bool, it
    >> can only return true or false. So I can't test for strings that are
    >> equal....or can I?

    >
    > The question was what your operator returns in the case of both being
    > equal. How often does your loop iterate? What happens as last thing?
    > What is returned? What is it supposed to return?


    If the letters are equal then it increments by one and tests for equality of
    the next two letters, once there are letters that aren't equal then it tests
    for inequality. This loops thru until an inequality is found, or 100
    characters are read. My problem seems to be if they strings are identical,
    and the fact that I always get a true statement.

    > And btw: You cannot test the strings for equality with one call to
    > your operator<, but with two, you can.


    I don't understand why I can't test for equality the way I have it?

    --

    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, Jul 4, 2005
    #14
  15. KL

    Mark P Guest

    KL wrote:
    > Mark P wrote:
    >

    [snip]
    >>
    >>I don't see why this whould always return true, but it still has a
    >>problem. What happens if the two strings are equal? What (if
    >>anything, *hint*) gets returned?

    >
    >
    > DOH! It is always the little things.....and I don't get why I always get a
    > true value, but I do...gonna ponder on that some more and work on a
    > different return for ....... uh oh....it is a bool, it can only return true
    > or false. So I can't test for strings that are equal....or can I?
    >


    As to why you always get "true", it would be helpful if you'd post a
    complete, compiling section of code that exhibits the problem.

    As to other issue I mentioned, it's not that there's no proper return
    value. If two strings are equal, it's usually sensible for < to return
    false since the one is *not* less than the other. The problem in your
    case is that nothing gets returned because the return statement is never
    executed.
     
    Mark P, Jul 4, 2005
    #15
  16. KL

    KL Guest

    Mark P wrote:
    > KL wrote:
    >> Mark P wrote:
    >>

    > [snip]
    >>>
    >>> I don't see why this whould always return true, but it still has a
    >>> problem. What happens if the two strings are equal? What (if
    >>> anything, *hint*) gets returned?

    >>
    >>
    >> DOH! It is always the little things.....and I don't get why I
    >> always get a true value, but I do...gonna ponder on that some more
    >> and work on a different return for ....... uh oh....it is a bool, it
    >> can only return true or false. So I can't test for strings that are
    >> equal....or can I?

    >
    > As to why you always get "true", it would be helpful if you'd post a
    > complete, compiling section of code that exhibits the problem.
    >
    > As to other issue I mentioned, it's not that there's no proper return
    > value. If two strings are equal, it's usually sensible for < to
    > return false since the one is *not* less than the other. The problem
    > in your case is that nothing gets returned because the return
    > statement is never executed.



    class STRING {

    char letters[100];

    int length;

    public:

    STRING( ){

    length = 0;

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

    letters[a] = ' ';

    }

    STRING(int num);

    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& b) const{

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

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

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

    }

    }

    };



    friend STRING operator+(const STRING&, const STRING&);

    friend int substring(const STRING&, const STRING&);

    friend STRING insert(const STRING&, const STRING&, int n);

    friend STRING delet(const STRING&, int n, int x);

    friend ostream& operator<<(ostream&, const STRING&);

    friend istream& operator>>(istream&, STRING&);

    };

    ostream& operator<< (ostream& os, const STRING& str) {

    int a = 0;

    while (a < str.length)

    os << str.letters[a++];

    return os;

    }

    istream& operator>> (istream& is, STRING& str) {

    string s;

    getline(is, s);

    str.length = s.length();

    if (str.length > 100) str.length = 100;

    for (int a=0; a<str.length; a++)

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

    return is;

    }

    STRING operator+(const STRING& a, const STRING& b) {

    STRING c,temp_a;

    int index=a.length;

    int result_length;

    int temp_b;

    temp_a=a;

    result_length=a.length + b.length;

    if (result_length > 100){

    result_length = 100;

    }

    temp_b=0;

    while (index < result_length){

    temp_a.letters[index] = b.letters[temp_b];

    temp_b+=1;

    index+=1;

    }

    temp_a.length=result_length;

    c=temp_a;

    return c;

    }

    int substring (const STRING& source, const STRING& search){

    int num = search.length, pt=0, h=0, g=0,index=0,x;

    STRING temp;

    while (h <= (source.length - num )) {

    index=0;

    while (index < search.length){

    temp.letters[index] = source.letters[h+index];

    index++;

    }

    h++;

    x=0;

    g=0;

    while (g < num){

    if (temp.letters[g] == search.letters[g]){

    x++;

    g++;

    }

    else

    g=num;

    }

    if (x==num)

    {

    pt=h-1;

    return pt;

    }

    }

    return -1;

    }



    STRING insert( const STRING& h, const STRING& i, int n){

    STRING temps;

    int stringsizes=h.length + i.length, k = 0, x=0 ,l = 0, z =0, j=0,

    il=i.length;

    if (n>h.length)

    stringsizes=stringsizes+n-h.length;

    if (stringsizes>100){

    stringsizes=100;

    }

    temps.length=stringsizes;

    while (j< n){

    temps.letters[j]=h.letters[j];

    j++;

    }

    while (x<il){

    temps.letters[j]=i.letters[x];

    j++;

    x++;

    }

    while (j<stringsizes){

    temps.letters[j] = h.letters[n];

    n++;

    j++;

    }

    return temps;

    }



    STRING delet(const STRING& a, int n, int x){

    int count=0, d=0;

    STRING f;

    f.length=a.length;

    while (count<n){

    f.letters[count]=a.letters[count];

    count++;

    }

    d=(n+x)+1;

    while (d<100){

    f.letters[count]=a.letters[d];

    count++;

    d++;

    }

    return f;

    }

    int main( ) {

    STRING a, b;

    bool n;

    string c;

    cout << "Enter a string: ";

    cin >> a;

    cout << "Enter a second string: ";

    cin >> b;

    n = a<b;

    if (n=true){

    c="The first string is less than the second string";

    }

    else {

    c="The second string is less that the first string";

    }

    cout << endl<<n << endl;

    cout << c << endl;


    return 0;

    }


    OK this is what I got so far. It is still giving me true every time. I
    thought I had that logic ok. I open the for loop, then the if != and return
    true if a<b, otherwise I close the if loop and continue the for loop. Am I
    totally lost? Do I possibly need a return after the end of the for loop?
    Shouldn't it return false if a>b?
    --

    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, Jul 4, 2005
    #16
  17. KL

    KL Guest

    Rolf Magnus wrote:
    > KL wrote:
    >
    >> int main( ) {
    >>
    >> STRING a, b;
    >>
    >> bool n;
    >>
    >> string c;
    >>
    >> cout << "Enter a string: ";
    >>
    >> cin >> a;
    >>
    >> cout << "Enter a second string: ";
    >>
    >> cin >> b;
    >>
    >> n = a<b;
    >>
    >> if (n=true){

    >
    > Look at that condition again.


    Ohhhhh right it returns a 0 or a 1 as a boolean, right? God, booleans will
    be the death of me...among other C++ errors I make repeatedly!
    >
    >> c="The first string is less than the second string";
    >>
    >> }
    >>
    >> else {
    >>
    >> c="The second string is less that the first string";
    >>
    >> }
    >>
    >> cout << endl<<n << endl;
    >>
    >> cout << c << endl;
    >>
    >>
    >> return 0;
    >>
    >> }
    >>
    >>
    >> OK this is what I got so far. It is still giving me true every
    >> time. I thought I had that logic ok. I open the for loop, then the
    >> if != and return true if a<b, otherwise I close the if loop and
    >> continue the for loop. Am I totally lost?

    >
    > No, the logic is ok, except for the case where both strings are fully
    > equal. The reason you seem to get true every time is not a fault in
    > your operator<, but a fault in your main() function. See above.
    >
    >> Do I possibly need a return after the end of the for loop?

    >
    > Yes.

    Now I must figure out what it can return besides 1 or 0. Going to think on
    that.....
    >
    >> Shouldn't it return false if a>b?

    >
    > It probably does.




    --

    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, Jul 5, 2005
    #17
  18. KL

    KL Guest

    Rolf Magnus wrote:
    > KL wrote:
    >
    >> int main( ) {
    >>
    >> STRING a, b;
    >>
    >> bool n;
    >>
    >> string c;
    >>
    >> cout << "Enter a string: ";
    >>
    >> cin >> a;
    >>
    >> cout << "Enter a second string: ";
    >>
    >> cin >> b;
    >>
    >> n = a<b;
    >>
    >> if (n=true){

    >
    > Look at that condition again.
    >
    >> c="The first string is less than the second string";
    >>
    >> }
    >>
    >> else {
    >>
    >> c="The second string is less that the first string";
    >>
    >> }
    >>
    >> cout << endl<<n << endl;
    >>
    >> cout << c << endl;
    >>
    >>
    >> return 0;
    >>
    >> }
    >>
    >>
    >> OK this is what I got so far. It is still giving me true every
    >> time. I thought I had that logic ok. I open the for loop, then the
    >> if != and return true if a<b, otherwise I close the if loop and
    >> continue the for loop. Am I totally lost?

    >
    > No, the logic is ok, except for the case where both strings are fully
    > equal. The reason you seem to get true every time is not a fault in
    > your operator<, but a fault in your main() function. See above.
    >
    >> Do I possibly need a return after the end of the for loop?

    >
    > Yes.
    >
    >> Shouldn't it return false if a>b?

    >
    > It probably does.


    OMG! The == !!! I told ya I keep making the same mistakes! That is just
    one of them.

    OK so that works ok as long as the STRINGs aren't equal. Since we were told
    to write this as a bool, I am going to forget about if it is equal. Will
    check with the teacher on that though.

    Thanks for your help! I am sorry I was being so dense. It's been a VERY
    long week here.

    --

    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, Jul 5, 2005
    #18
  19. KL

    Rolf Magnus Guest

    KL wrote:

    > int main( ) {
    >
    > STRING a, b;
    >
    > bool n;
    >
    > string c;
    >
    > cout << "Enter a string: ";
    >
    > cin >> a;
    >
    > cout << "Enter a second string: ";
    >
    > cin >> b;
    >
    > n = a<b;
    >
    > if (n=true){


    Look at that condition again.

    > c="The first string is less than the second string";
    >
    > }
    >
    > else {
    >
    > c="The second string is less that the first string";
    >
    > }
    >
    > cout << endl<<n << endl;
    >
    > cout << c << endl;
    >
    >
    > return 0;
    >
    > }
    >
    >
    > OK this is what I got so far. It is still giving me true every time. I
    > thought I had that logic ok. I open the for loop, then the if != and
    > return true if a<b, otherwise I close the if loop and continue the for
    > loop. Am I totally lost?


    No, the logic is ok, except for the case where both strings are fully equal.
    The reason you seem to get true every time is not a fault in your
    operator<, but a fault in your main() function. See above.

    > Do I possibly need a return after the end of the for loop?


    Yes.

    > Shouldn't it return false if a>b?


    It probably does.
     
    Rolf Magnus, Jul 5, 2005
    #19
  20. KL

    Rolf Magnus Guest

    KL wrote:

    >>> OK this is what I got so far. It is still giving me true every
    >>> time. I thought I had that logic ok. I open the for loop, then the
    >>> if != and return true if a<b, otherwise I close the if loop and
    >>> continue the for loop. Am I totally lost?

    >>
    >> No, the logic is ok, except for the case where both strings are fully
    >> equal. The reason you seem to get true every time is not a fault in
    >> your operator<, but a fault in your main() function. See above.
    >>
    >>> Do I possibly need a return after the end of the for loop?

    >>
    >> Yes.
    >>
    >>> Shouldn't it return false if a>b?

    >>
    >> It probably does.

    >
    > OMG! The == !!! I told ya I keep making the same mistakes! That is just
    > one of them.


    I was hoping that you find that one yourself. It makes the learning
    experience stronger. ;-)

    > OK so that works ok as long as the STRINGs aren't equal. Since we were
    > told to write this as a bool, I am going to forget about if it is equal.


    No, it's not something like a 3rd state you need to return. Of course, you
    stick with bool, which only has two possible values, true and false.
    But if your strings are equal, the first is not less than the second one. So
    your operator should definitely return false. What does it do instead?

    > Will check with the teacher on that though.
    >
    > Thanks for your help! I am sorry I was being so dense. It's been a VERY
    > long week here.


    Actually, it was fun giving you hints, but still letting you work it out
    yourself. :)
     
    Rolf Magnus, Jul 5, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Natalie
    Replies:
    0
    Views:
    403
    Ron Natalie
    Jul 9, 2003
  2. C Wood
    Replies:
    4
    Views:
    1,013
    Stuart Golodetz
    Jul 10, 2003
  3. B.R.Kumar

    overloading operators

    B.R.Kumar, Oct 10, 2003, in forum: C++
    Replies:
    2
    Views:
    340
    rossum
    Oct 10, 2003
  4. Zenon
    Replies:
    6
    Views:
    448
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Oct 25, 2003
  5. Nimmi Srivastav

    Overloading new and delete operators

    Nimmi Srivastav, Jan 31, 2004, in forum: C++
    Replies:
    3
    Views:
    2,689
    John Harrison
    Feb 2, 2004
Loading...

Share This Page