Need help with C++ program!!

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);


friend ostream& operator<<(ostream& stream, XYPoint& p );
float distance( XYPoint p,XYPoint q) ;
private:
float x;
float y;

};

#endif




//XYPoint.cpp File

#include <iostream>
using namespace std;
#include<cmath>
#include "XYPoint.h"

XYPoint::XYPoint()
{
x = 0;
y = 0;
}

XYPoint::XYPoint( float newx, float newy )
{
x = newx;
y = newy;
}

ostream& operator<<( ostream& stream, XYPoint& p)
{
stream << p.x << " " << p.y<<endl;
return stream;
}

float XYPoint::distance( XYPoint p,XYPoint q )
{
float distance = sqrt(pow((q.x-p.x),2)+pow((q.y-p.y),2));
return distance;
}



//main.cpp


#include "XYPoint.h"
#include<iostream>
#include <cmath>
using namespace std;

int main(void)
{
float x = 0;
float y = 0;

cout<<"Enter x value:";
cin>>x;
cout<<"Enter y value:";
cin>>y;

XYPoint point1(x,y);
XYPoint point2(-6, 8);

cout<<point1<<endl;
cout<<point2<<endl;

cout<<"Distance between these points is "<<point2.distance(point1,point2)<<endl;

return 1;
}
To override the stream extraction operator is essentially replacing these statements:
1
2
3
4
    cout<<"Enter x value:";
    cin>>x;
    cout<<"Enter y value:";
    cin>>y;


into this:
1
2
3
    XYPoint p2;
    cout << "Enter a point value (or whatever): " << endl;
    cin >> p2;


and, (like with the insertion operator) we're dealing with I/O stream object, in this case the istream, e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
}


oh, and figure out how to use the code tags

edit: added an empty constructor
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Header File

#ifndef XYPOINT_H
#define XYPOINT_H
#include <iostream>
using namespace std;

class XYPoint
{
    public:
        XYPoint();
        XYPoint(float newx = 0,float newy= 0);
        friend ostream& operator << (ostream& stream, XYPoint& p);
        float distance(XYPoint p, XYPoint q) ;
    private:
        float x;
        float y;
};

#endif

// XYPoint.cpp File

#include <iostream>
using namespace std;
#include<cmath>
#include "XYPoint.h"

XYPoint::XYPoint()
{
    x = 0;
    y = 0;
}

XYPoint::XYPoint(float newx, float newy)
{
    x = newx;
    y = newy;
}

ostream& operator << (ostream& stream, XYPoint& p)
{
    stream << p.x << " " << p.y << endl;
    return stream;
}

float XYPoint::distance( XYPoint p,XYPoint q )
{
    float distance = sqrt(pow((q.x-p.x),2)+pow((q.y-p.y),2));
    return distance;
}

// Main.cpp File

#include "XYPoint.h"
#include<iostream>
#include <cmath>
using namespace std;

int main(void)
{
    float x = 0;
    float y = 0;

    cout << "Enter x value:";
    cin >> x;
    cout << "Enter y value:";
    cin >> y;

    XYPoint point1(x, y);
    XYPoint point2(-6, 8);

    cout << point1 << endl;
    cout << point2 << endl;

    cout << "Distance between these points is " << point2.distance(point1, point2) << endl;

    return 0;
}


To help people to help you. Also, please make more indents and white space.

cout<<"TEST"<<endl;


looks a lot worse than...

cout << "TEST" << endl;


They both same efficiency but you can see the flow better especially when there is more code.
Topic archived. No new replies allowed.