Closer. In full disclosure, this is an assignment, and I only have about 20 mins left to turn it in >.<
" Create a class called XYZPoint that has public floats to hold
the x, y, and z values of a 3D point. The class should have a constructor
with default values (0,0,0) and a method called Distance( XYZPoint )
that returns the Euclidean distance between the argument and the point
the method is called on. Also override the stream insertion operator so
that users can print the point to the screen in the format [x, y, z].
Test your program with the points [5 3 1] and [-3 7 1]. Turn in copies
of all three files needed to compile your
program: main.cpp, XYZPoint.cpp, and XYZPoint.h"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
//header
#ifndef XYZ_H
#define XYZ_H
class XYZPoint
{
public:
XYZPoint( ); //Default Constructor
XYZPoint( float newX, float newY, float newZ ); //Constructor
friend ostream& operator<<(ostream& stream, const XYZPoint& P );
void Distance( XYZPoint n );
float x;
float y;
float z;
};
#endif //XYZ_H
|
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
|
//.cpp
#include <iostream>
#include "xyz.h"
#include <cmath>
using namespace std;
XYZPoint::XYZPoint( float newX, float newY, float newZ ) //constructor
{
x = newX;
y = newY;
z = newZ;
}
XYZPoint::XYZPoint ( ) //default constructor
{
x = 0.0;
y = 0.0;
z = 0.0;
}
void XYZPoint::Distance( XYZPoint n)
{
XYZPoint temp ( 0, 0, 0 );
temp.x = sqrt(n.x*n.x);
temp.y = sqrt(n.y*n.y);
temp.z = sqrt(n.z*n.z);
cout << temp.x << " " << temp.y << " " << temp.z << endl;
return ;
}
ostream& operator<<( ostream& stream, const XYZPoint& n )
{
stream << "[ " << n.x << " " << n.y << " " << n.z << " ]" << endl;
return stream;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
//main
#include <iostream>
#include "xyz.h"
using namespace std;
int main()
{
//cout << "Hello world." << endl;
XYZPoint P0( 0, 0, 0 );
XYZPoint P1( 5, 3, 1 );
XYZPoint P2( -3, 7, 1);
P1.Distance(P2);
return 1;
}
|
It will calculate now, but I haven't finished the distance part. I also started working out the output. It will all compile, but it won't put out in the format I want. Just like my women.