Need help understanding overload program

This is a discussion on Need help understanding overload program within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a question regarding following code. It is overloading plus (+) operator. Code: #include <iostream> using namespace std; ...

  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
    265
    In that case, b would be param. You could even say:

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

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    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,244
    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%.

  5. #5
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    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, 01:07 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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