Thread: Need help understanding overload program

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    30

    Need help understanding overload program

    Hello,

    I have a question regarding following code. It is overloading plus (+) operator.
    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;
    }
    Question: When function + is called, which object is traced to param is it a or b? and while executing this function, x = ?, y =?

    Thanks for any help..

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    In that case, b would be param. You could even say:

    Code:
    c = a.operator+(b);

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent any code you post!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by OCcounty View Post
    Question: When function + is called, which object is traced to param is it a or b?
    To split hairs, neither. A copy of b is (logically) passed to the function as the argument named "param". Although a compiler is allowed to eliminate the copy, it is not required to.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Instead of:
    Code:
    CVector operator + (CVector v);
    Why not use:
    Code:
    CVector operator+(const CVector& v);
    Now you have const reference to object, instead of copy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Understanding the pseudocode/logic for this program
    By Sholcomb1 in forum C Programming
    Replies: 11
    Last Post: 12-08-2006, 02:07 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM