1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Problem overloading functions

Discussion in 'C++' started by Lief Webster, Dec 5, 2007.

  1. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    > a + b = c;
    Try
    c = a + b;
     
  3. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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.
     
  5. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    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?
     
  6. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    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;
     
  7. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Actually, the second code block should say

    Code:
    m = c / 2;
     
  8. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    I think you also need a CMidpoint::eek:perator / which takes an int as a parameter, and you use that parameter in your division.
     
  9. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    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.....
     
  10. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page