Thread: Need help understanding overload program

  1. #1
    Registered User
    Join Date
    Jul 2010

    Need help understanding overload program


    I have a question regarding following code. It is overloading plus (+) operator.
    #include <iostream>
    using namespace std;
    class CVector {
    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
    In that case, b would be param. You could even say:

    c = a.operator+(b);

  3. #3
    Join Date
    Oct 2007
    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
    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
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Instead of:
    CVector operator + (CVector v);
    Why not use:
    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