Please explain overloading arithmetic and relational operators

This is a discussion on Please explain overloading arithmetic and relational operators within the C++ Programming forums, part of the General Programming Boards category; Wait, what?...

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Wait, what?

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> Well, does "very rarely" mean that it won't work, or does it mean that I've completely skated around the issue?

    It means "you can do it but then why unless you have to?". There are times, like when invoking an operator from a base class, that you *will* need to to it, but again, that's typically rare.

    >> And what do you mean "defined << for [my] class?" Isn't "<<" already defined somewhere in the vast wonderful libraries that I take for granted?

    For some *other* class, yes, but for yours - how could it?
    Last edited by Sebastiani; 11-30-2008 at 05:19 PM. Reason: repetition of repetition
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You keep repeating this:
    If I had two integers, a and b, how would I add them like that?
    and it worries me. Have you never really seen a+b in a program before? For integers, or for floating-point (float or double), or anything?

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> and it worries me. Have you never really seen a+b in a program before? For integers, or for floating-point (float or double), or anything?

    Me, too. I decided against posting a complete example for similar reasons.

    Could you post some code, OP, so that we can see where you are at on this?
    Last edited by Sebastiani; 11-30-2008 at 05:37 PM. Reason: fixed typose, restatement
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    it's ok. i got a friend at another school to help me. I suppose the reason I sound so inept is because i'm a little stressed out by not being able to figure this out on my own. I'm only in my second semester of C++, and yes, I do know how to add normal integers. I just don't understand the point of adding them like this and perhaps that's why I can't reason my way through it. I also know a bit of Java (dabbled over the summer) and learned Pascal in my introduction classes quite a few years ago.

    So, thanks for all your help!

    I'm writing it like this and hopefully it will work:

    Code:
    class rationalADT
    {
       int operator+(int a, int b)
       {
           int T;
           T = a + b;
           return T;
       }

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by aperson View Post
    it's ok. i got a friend at another school to help me. I suppose the reason I sound so inept is because i'm a little stressed out by not being able to figure this out on my own. I'm only in my second semester of C++, and yes, I do know how to add normal integers. I just don't understand the point of adding them like this and perhaps that's why I can't reason my way through it. I also know a bit of Java (dabbled over the summer) and learned Pascal in my introduction classes quite a few years ago.

    So, thanks for all your help!

    I'm writing it like this and hopefully it will work:

    Code:
    class rationalADT
    {
       int operator+(int a, int b)
       {
           int T;
           T = a + b;
           return T;
       }
    The compiler already knows how to add an int and an int together. It won't allow you to tell it otherwise. What you need to tell the compiler how to do is to add two Rationals together. If you have a rational a, and a rational b, how do you define a+b? That's the question that you need to answer. There is no point in trying to define addition for integers, that's already built in. You need to add the new stuff in your class.

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Shouldn't rationalADT be the arguments to the overloaded operator? Didn't bother to read the posts before this, did you?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    no... the instructions state explicitly NOT to add the rationals. I just wanted to know how to write the addition like this so that i could figure everything else out. I think i have it now.

    Thanks (snide comments aside) though!

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by aperson View Post
    no... the instructions state explicitly NOT to add the rationals. I just wanted to know how to write the addition like this so that i could figure everything else out. I think i have it now.

    Thanks (snide comments aside) though!
    Well, I wish you the best of luck then. We'll see you tomorrow. (Edit: By which I mean, perhaps you were just supposed to come up with the knowledge of how to add fractions and the syntax for what an overloaded operator is supposed to look like?)
    Last edited by tabstop; 11-30-2008 at 07:47 PM.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Code:
       int operator+(int a, int b)
       {
           int T;
           T = a + b;
           return T;
       }
    Not only does that not work because it is already defined for ints, but even if one were to replace int with SomeClass then it still wouldn't work because it would be infinitely recursive, which you may have noticed if you simplified it to:
    Code:
       int operator+(int a, int b)
       {
           return a + b;
       }
    This is just one of the reasons that when you're introduced to operator overloading you will be given an example like the Fraction one you were given earlier. It only makes sense to define an operator for some kind of useful class, and not somthing that is not built-in.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by tabstop
    Nothing in your class is already defined somewhere.
    That said, note that some things in your class might be implicitly defined if you do not define them, namely, the default constructor (which will not be implicitly defined if any constructor is user defined), copy constructor, copy assignment operator (another case of operator overloading!) and destructor.

    Speaking of overloading operator+ for a class: semantically, operator+ would not change the state of the class, so it should be a const member function. However, if you implement a corresponding operator+= instead, then you could make operator+ a non-member non-friend function by implementing it in terms of operator+=.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21