Question: Create a class called XYPoint that has public floats to hold the x and y values of a 2D point. The class should have a constructor with default values (0,0) and a method called Distance( XYPoint ) that returns the Euclidean distance between the argument and the point the method is called on. Also override the stream insertion and extraction operators so that users can print the point to the screen in the format [x, y] and also input new values for a given point. Test your program with the points [4 2] and [-6 8]. Turn in copies of all three files needed to compile your program: main.cpp, XYPoint.cpp, and XYPoint.h.
Teacher's Comments: You were asked to override both the stream extraction (>>) and stream insertion (<<) operators. You have correctly overridden <<, but you did not override >>. And you need to do it in same program.
I dont know how to fix my program.
Here is my code:
//Header File
#ifndef XYPOINT_H
#define XYPOINT_H
#include <iostream>
using namespace std;
class XYPoint
{
public:
XYPoint();
XYPoint(float newx = 0,float newy= 0);
class Foo
{
public:
//empty constructor, so you can declare an object without initialization
Foo() {};
//constructor, I prefer this style, but don't follow the naming style ;)
Foo(int num1, int num2): intFoo1(num1), intFoo2(num2) {};
//overload the >>
friend istream& operator>>(istream& istr, Foo& obj);
private:
int intFoo1, intFoo2;
};
istream& operator>>(istream& istr, Foo& obj)
{
/* the simplest is to mimic cin >> intFoo1 >> intFoo2;
* but I suggest you figure out some kind of type-checking
*/
}
//in main
{
...
Foo f1;
cin >> f1; // user enters 3 and 4 or whatever
//etc
}