Thread: overloading operators

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    overloading operators

    this code is frustrating me; it's an example from cplusplus.com(classes II)

    i can't differentiate between which members are the constructors, why the overloading function is even called, and why saying, "CVector c;" is even valid. Are all of the members constructors(except for x,y)? what the hell is this, "CVector () {};"? oh, and why is cvector repeated so many times on the overloaded function call?
    Code:
    #include <iostream>
    using namespace std;
    
    class CVector {
      public:
        int x,y;
        CVector () {};
        CVector (int,int);
        CVector operator + (CVector);
    };
    
    CVector::CVector (int a, int b) {
      x = a;
      y = b;
    }
    
    CVector CVector::operator+ (CVector param) {
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    
    int main () {
      CVector a (3,1);
      CVector b (1,2);
      CVector c;
      c = a + b;
      cout << c.x << "," << c.y;
      return 0;
    }

    rgetgtrgvtrgvtr
    Last edited by thracian; 06-23-2008 at 05:10 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let's start with the easy one: saying "CVector c;" is valid because you defined what to do when some idiot tried to construct a CVector without two ints: if the parameter list is () -- that is, empty -- do
    Code:
    { }
    -- that is, nothing.

    In the overloaded operator:
    Code:
    CVector CVector::operator+ (CVector param)
    You know how functions work: return type; class to which the function belongs; type of parameter.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Let's start with the easy one: saying "CVector c;" is valid because you defined what to do when some idiot tried to construct a CVector without two ints: if the parameter list is () -- that is, empty -- do
    Code:
    { }
    -- that is, nothing.

    In the overloaded operator:
    Code:
    CVector CVector::operator+ (CVector param)
    You know how functions work: return type; class to which the function belongs; type of parameter.
    All right, so then what calls the overloaded function?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, it's name is "+". Do you have any "+" in your code?

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Well, it's name is "+". Do you have any "+" in your code?
    So then what is the arguement that is sent, or 'param'?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you happen to possibly have a CVector after your +, that could be considered to be added to the other vector?

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Do you happen to possibly have a CVector after your +, that could be considered to be added to the other vector?
    the b object comes after the operator, i don't understand what you mean....

    The function operator+ of class CVector is the one that is in charge of overloading the addition operator (+). This function can be called either implicitly using the operator, or explicitly using the function name:
    Code:
    c = a + b;
    c = a.operator+ (b);
    in the case of the example, the operator+ is called implicitly with the + sign? i still don't understand what arguement is sent..

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, what's the thing after the + sign (function call)? b, it looks like. So hey. b is the parameter. The two lines you gave are exactly 100% the same. But rather than make you type all that you can lose the word operator, and since the parentheses are redundant, you can lose them here too.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    When the constructor is called for the a and b objects:
    CVector a (3,1);
    CVector b (1,2);

    a.x is assigned as 3 and a.y=1 as well as for b?



    but then when this is called:
    Code:
    CVector CVector::operator+ (CVector param) {
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    it refers to the x and y alone which is what?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by thracian View Post
    When the constructor is called for the a and b objects:
    CVector a (3,1);
    CVector b (1,2);

    a.x is assigned as 3 and a.y=1 as well as for b?
    Looks like b.x is 1 and b.y is 2.

    Quote Originally Posted by thracian View Post
    but then when this is called:
    Code:
    CVector CVector::operator+ (CVector param) {
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    it refers to the x and y alone which is what?
    As always with member functions, variables by themselves refer to the members of the object from which the function is called. You could think of x as this->x and y as this->y, if you really wanted to. Since this + was called from a (that's the part before the dot, right, the object whose method we are calling) x and y refer to a.x and a.y.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Ah, well I guess it makes sense now. Oh, and yeah, that's what I meant for the first part(b.x=1, and b.y=2), sorry for not making it clearer.

    still trying to understand why cvector is repeated so many times. the tutorial says the format is this:
    Code:
    type operator sign (parameters) { /*...*/ }
    it just doesn't look right because in previous examples, the name of the constuctor is used with the scope operator instead of repeated like here.
    Code:
    CVector CVector::operator+ (CVector param)
    is operator+ a member? is it just something you include because of the format?


    this is my first day learning classes so thanks for bearing with me man

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because the constructor is a function with no return type (not even void) whose name is the name of the class.

    So CVector::CVector(int, int) is the same as before with the return type, the name of the class, and the name of the function.

    Here you are defining the function CVector (taking two int parameters) as a member of the class CVector (and since the names are the same, it will be the constructor). Before you were defining the function operator+ (which you can abbreviate as just + when calling it, though not defining it) as a member of the class CVector, and returning a CVector.

    Edit: ooh another example:
    Code:
    int CVector::taxicab_distance(const CVector& other) {
        return abs(x-other.x)+abs(y-other.y)
    }
    See if you can work out how that would be called.
    Last edited by tabstop; 06-23-2008 at 07:09 PM.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    That's because the constructor is a function with no return type (not even void) whose name is the name of the class.

    So CVector::CVector(int, int) is the same as before with the return type, the name of the class, and the name of the function.

    Here you are defining the function CVector (taking two int parameters) as a member of the class CVector (and since the names are the same, it will be the constructor). Before you were defining the function operator+ (which you can abbreviate as just + when calling it, though not defining it) as a member of the class CVector, and returning a CVector.

    Edit: ooh another example:
    Code:
    int CVector::taxicab_distance(const CVector& other) {
        return abs(x-other.x)+abs(y-other.y)
    }
    See if you can work out how that would be called.
    ohhh! cvector was also the return type, goddamnit, i completely missed that the first time.

    int - return type
    cvector- class
    taxecab_distance - member function


    thanks man
    Last edited by thracian; 06-23-2008 at 07:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM