Problem overloading functions

Lief Webster's Avatar, Join Date: Oct 2007
Go4Expert Member
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?
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
> a + b = c;
Try
c = a + b;
Lief Webster's Avatar, Join Date: Oct 2007
Go4Expert Member
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's Avatar, Join Date: Nov 2007
Ambitious contributor
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's Avatar
Go4Expert Member
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's Avatar, Join Date: Oct 2007
Go4Expert Member
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;
Lief Webster's Avatar, Join Date: Oct 2007
Go4Expert Member
Actually, the second code block should say

Code:
m = c / 2;
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
I think you also need a CMidpoint::operator / which takes an int as a parameter, and you use that parameter in your division.
seeguna's Avatar
Go4Expert Member
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's Avatar, Join Date: Oct 2007
Go4Expert Member
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.