Thread: Please explain overloading arithmetic and relational operators

  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,708
    >> 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 06:19 PM. Reason: repetition of repetition
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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,708
    >> 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 06:37 PM. Reason: fixed typose, restatement
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,336
    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,708
    Shouldn't rationalADT be the arguments to the overloaded operator? Didn't bother to read the posts before this, did you?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,336
    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 08:47 PM.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    28,413
    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+=.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread