Thread: Extractor overloading and memberwise assignment for Cartesian class

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    4

    Extractor overloading and memberwise assignment for Cartesian class

    I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output
    Please enter the first coordinates: Please enter the second coordinates: (4.86129e-270, -1.97785e-41)
    (4.86143e-270, -1.97785e-41)

    Here is the code
    Code:
    #include <iostream>
    #include <istream>
    #include <ostream>
    
    
    using namespace std;
    
    
    class Cartesian
    {
    private:
    double x;
    double y;
    public:
    Cartesian( double= 0, double= 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
    
    
    };
    
    
    Cartesian::Cartesian(double a, double b)
    {
        x=a;
        y=b;
    }
    
    
    istream& operator>>( istream& in, Cartesian& num)
    {
        double a, b;
        num.x= a;
        num.y= b;
    
    
        in >> a >> b;
    
    
    return in;
    }
    
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
        cout << "(" << num.x << ", " << num.y << ")" << endl;
    
    
    return out;
    }
    
    
    int main()
    {
        Cartesian coord1, coord2;
        cout << "Please enter the first coordinates: ";
        cin >> coord1;
        cout << "Please enter the second coordinates: ";
        cin>> coord2;
        cout << coord1;
        cout << coord2;
        
        
        return 0;
    }
    Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so?




  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're kidding right?

    Look at lines 32-37. It creates two uninitialised variables a and b, copies their initial (undetermined) values into num.x and num.y [that explains the funny values you're seeing], reads value from the stream into a and b, and then (as the function returns) destroys the variables a and b. In that logic, there is no path by which the values read from the stream find their way into affecting values of num.x or num.y.

    As an aside, given that the output stream operator outputs the coordinates in brackets and with a comma, usually the input stream operator is expected to cope with presence of bracket and comma in the stream (as well as their absence).
    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.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    And what should I do as an alternative?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Assign the values of a and b to num after reading a and b from the stream
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    Okay so I've changed my code around a bit, but now I get errors saying the x and y in x=c and y=d don't name types. What should I do?
    Code:
    #include <iostream>#include <istream>
    #include <ostream>
    
    
    using namespace std;
    
    
    class Cartesian
    {
    private:
    double x;
    double y;
    public:
    Cartesian( double= 0, double= 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
    double c;
    double d;
    x=c;
    y=d;
    
    
    };
    
    
    Cartesian::Cartesian(double a, double b)
    {
        x=a;
        y=b;
    }
    
    
    istream& operator>>( istream& in, Cartesian& num)
    {
    	in >> num.x;
    	in >> num.y;
        
    
    
    return in;
    }
    
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
    	cout << "(" << num.x << ", " << num.y << ")" << endl;
    
    
    return out;
    }
    
    
    int main()
    {
    	Cartesian coord1, coord2;
        cout << "Please enter the first x-coordinate: ";
    	cin >> coord1.c;
    	cout << "Please enter the first y-coordinate: ";
    	cin >> coord1.d;
    	 cout << "Please enter the second x-coordinate: ";
    	cin >> coord2.c;
    	cout << "Please enter the second y-coordinate: ";
    	cin >> coord2.d;
    	
    
    
    	cout << coord1;
    	cout << coord2;
    
    
    	
    	
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Remove lines 17 through 20. Work out some proper mechanism of setting Cartesian's x and y members.

    Stop doing things by guesswork.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    I've gotten the program to run, but it doesn't ask the user to input data. Can anyone tell me what part of my code is wrong?
    Code:
    #include <iostream>
    #include <istream>
    #include <ostream>
    
    
    using namespace std;
    
    
    class Cartesian
    {
    private:
    double x;
    double y;
    public:
    Cartesian( double= 0, double= 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
    }
    ;
    
    
    Cartesian::Cartesian(double a, double b)
    {
        x=a;
        y=b;
    }
    
    
    istream& operator>>( istream& in, Cartesian& num)
    {
        in >> num.x;
    	in >> num.y;
        
    
    
    return in;
    }
    
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
    	cout << "(" << num.x << ", " << num.y << ")" << endl;
    
    
    return out;
    }
    
    
    int main()
    {
    	Cartesian coord1, coord2;
    	cout << "Please enter the first coordinates in the form x y: ";
    	cin >> coord1;
    	cout << "Please enter the second coordinates in the form x y: ";
    	cin >> coord2;
    	cout << coord1;
    	cout << coord2;
    
    
    	
    	
    	return 0;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I can't reproduce your problem. When I run the program, I can enter, for example:
    Code:
    Please enter the first coordinates in the form x y: 1.2 3.4
    Please enter the second coordinates in the form x y: 5.6 7.8
    (1.2, 3.4)
    (5.6, 7.8)
    
    Process returned 0 (0x0)   execution time : 22.090 s
    Press any key to continue.
    So it works for me. If you have any information besides "it doesn't ask the user" it would help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem understanding how to use memberwise assignment
    By JonathanS in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2012, 04:19 AM
  2. Overloading assignment operator
    By ryanmcclure4 in forum C++ Programming
    Replies: 20
    Last Post: 10-28-2012, 09:44 AM
  3. Assignment operator overloading
    By hsgw in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2007, 06:44 PM
  4. Overloading [] and Assignment
    By Mastadex in forum C++ Programming
    Replies: 6
    Last Post: 12-24-2006, 12:22 PM
  5. overloading the assignment operator
    By Unregistered36 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2001, 06:51 AM

Tags for this Thread