Problem overloading functions

Go4Expert Member
5Dec2007,07:18   #1
Lief Webster's Avatar
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:

Code:
//Overloading Operators
//Midpoint

#include <iostream>

using namespace std;

class CMidpoint {
	public:
	   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;
	cin.ignore();

	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;
	cin.get();
	cin.ignore();
	
	a + b = c;

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

	cin.get();
	return 0;
}
How can I get it to display the actual midpoint, rather than some random number?
Ambitious contributor
5Dec2007,12:27   #2
Salem's Avatar
> a + b = c;
Try
c = a + b;
Go4Expert Member
6Dec2007,03:06   #3
Lief Webster's Avatar
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?
Ambitious contributor
6Dec2007,03:14   #4
Salem's Avatar
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.
Go4Expert Member
6Dec2007,15:17   #5
seeguna's Avatar
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?
Go4Expert Member
7Dec2007,04:37   #6
Lief Webster's Avatar
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?

Code:
CMidpoint CMidpoint::operator / ( CMidpoint otherparam )
{
    CMidpoint othertemp;
    
    othertemp.x = otherparam.x / 2;
    othertemp.y = otherparam.y / 2;
    
    return ( othertemp );
}
Code:
m = 2 / c;
Go4Expert Member
7Dec2007,04:39   #7
Lief Webster's Avatar
Actually, the second code block should say

Code:
m = c / 2;
Ambitious contributor
7Dec2007,13:43   #8
Salem's Avatar
I think you also need a CMidpoint::operator / which takes an int as a parameter, and you use that parameter in your division.
Go4Expert Member
7Dec2007,17:03   #9
seeguna's Avatar
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.....
Go4Expert Member
9Dec2007,04:28   #10
Lief Webster's Avatar
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:

Code:
class Vector
{
public:
    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.