Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Problem overloading functions (http://www.go4expert.com/forums/problem-overloading-functions-t7712/)

Lief Webster 5Dec2007 07:18

Problem overloading functions
Alright, I've got this program that is supposed to ask the user for input of two ordered pairs, then use a class and a + operator to produce the midpoint. Unfortunately, whenever it is run, it gets the ordered pairs and then invariably says the midpoint is
(4370432,4370432). Here is the code:


//Overloading Operators

#include <iostream>

using namespace std;

class CMidpoint {
          int x , y;
          CMidpoint() {};
          CMidpoint( int , int );
          CMidpoint operator + ( CMidpoint );

CMidpoint::CMidpoint( int a , int b )
        x = a;
        y = b;

CMidpoint CMidpoint::operator + ( CMidpoint param )
        CMidpoint temp;
        temp.x = x + param.x;
        temp.y = y + param.y;

        return (temp);

int main()
        CMidpoint c;

        int i , j , k , l;

        cout << "Please enter the x and y value (separated by a space) of an ordered pair: " << endl;
        cin >> i >> j;

        CMidpoint a ( i , j );

        cout << "Please enter the x and y value (separated by a space) of another ordered pair: " << endl;
        cin >> k >> l;

        CMidpoint b ( k , l );

        cout << "Your ordered pairs:" << endl;
        cout << "\t(" << i << "," << j << ")" << endl;
        cout << "\t(" << k << "," << l << ")" << endl;

        cout << "(Press any key to confirm...)" << endl;
        a + b = c;

        cout << "The midpoint of the points is ";
        cout << "(" << c.x << "," << c.y << ")." << endl;

        return 0;

How can I get it to display the actual midpoint, rather than some random number?

Salem 5Dec2007 12:27

Re: Problem overloading functions
> a + b = c;
c = a + b;

Lief Webster 6Dec2007 03:06

Re: Problem overloading functions
That works! But why should the order have any bearing on what the result is? Is it because I overloaded the + operator, but c++ has a default = operator?

Salem 6Dec2007 03:14

Re: Problem overloading functions
Does a + b = c work for you for when you use say doubles? I doubt it.

lvalue = rvalue_expression
is written into the language. You can't just rearrange that to suit.

seeguna 6Dec2007 15:17

Re: Problem overloading functions
Let us consider the stmt
c= a + b

In operator overloading concept, value next to + operator i.e operand value' b' only passed to Overloading function that why in ur pgm 'b' value is copied to' param' operand......

operand ' a' value is passed automatically ,we no need to pass it........

As per ur stmt,
if we put a+b=c,
Which value get passed? Think over it?

Lief Webster 7Dec2007 04:37

Re: Problem overloading functions
Alright, I've got another problem. In the following code, I want to set m to equal c/2 (to finish off the midpoint formula). Why is this code incorrect?


CMidpoint CMidpoint::operator / ( CMidpoint otherparam )
    CMidpoint othertemp;
    othertemp.x = otherparam.x / 2;
    othertemp.y = otherparam.y / 2;
    return ( othertemp );


m = 2 / c;

Lief Webster 7Dec2007 04:39

Re: Problem overloading functions
Actually, the second code block should say


m = c / 2;

Salem 7Dec2007 13:43

Re: Problem overloading functions
I think you also need a CMidpoint::operator / which takes an int as a parameter, and you use that parameter in your division.

seeguna 7Dec2007 17:03

Re: Problem overloading functions
if u use c=a/b(for overloading /) means i.e correct

but i think that m=c/2 or m=2/c for overloading is wrong.....

Lief Webster 9Dec2007 04:28

Re: Problem overloading functions
I've got another question. I'm not totally grasping this overloading operators concept, and the book I'm trying to (partially) learn from isn't helping matters. It gives me the following program:


class Vector
    int x, y;
    Vector operator + (Vector & OtherVector);
    Vecotr & operator += (Vector & OtherVector);

Vector Vector::operator + (Vector & OtherVector)
    Vector TempVector;
    TempVector.x = x + OtherVector.x;
    TempVector.y = y + OtherVector.y;
    return TempVector;

Vector & Vector::operator += (Vector & OtherVector)
    x += OtherVector.x;
    y += OtherVector.y;
    return * this;

Vector VectorOne;
Vector VectorTwo;
Vector VectorThree;

VectorOne = VectorTwo + VectorThree;
VectorThree += VectorOne;

Besides from the obvious things like lacking main(), I would appreciate it if someone would explain this code block to me, as I can't really understand the book. What's with all the references? Also, this is the first time I've seen 'this' - could someone tell me what that's all about also?

Thanks alot.

All times are GMT +5.5. The time now is 12:09.