Last week I asked about this so I tried to write another example to try it out. I have a Line class which needs to have and members of type Point and member functions that return type Point. The Point class needs to have member functions that return type Line (this has been omitted from the example because the solution will be the same).
Last week I was informed that if I have a situation like this, then in class Line I can only have members of type *Point. Clearly, I would return them to the main program with return &MyPoint. However, with member functions, since they can only return *Point, there is obviously no way to get the value to main without in main doing Point P=&Line.getP1(). That seems like it would get a bit old.
1) is this all correct?
2) is there not a better way to do it?
Thanks!
David
Line.h
#ifndef LINE_H
#define LINE_H
#include "Point.h"
class Point;
class Line
{
// Ax+By+C = 0
double A,B,C;
Point *p1, *p2;
public:
Line();
Line(Point P1, Point P2);
~Line() {}
double getA() { return A;}
double getB() { return B;}
double getC() { return C;}
//Point getP1() {return *p1;}
//Point getP2() {return *p2;}
};
#endif
Line.cpp
#include "Line.h"
Line::Line()
{
A=0;
B=0;
C=0;
}
Line::Line(Point P1, Point P2)
{
p1=&P1;
p2=&P2;
double x1,x2,y1,y2;
x1=P1.getX();
x2=P2.getX();
y1=P1.getY();
y2=P2.getY();
//derived from slope(m) intercept(b) form
double m,b;
m = (y2-y1)/(x2-x1);
b = y1 - ((y2-y1)/(x2-x1))*x1;
C = 1;//arbitrary, so set to 1
B = -1/b;
A = -B*m;
}
Point.h
#ifndef POINT_H
#define POINT_H
#include "Line.h"
class Line;
class Point
{
double x, y, z;
public:
Point();
Point(double x_create, double y_create, double z_create);
~Point() {}
double getX() { return x; }
double getY() { return y; }
double getZ() { return z; }
void setX(double Xin) { x=Xin; }
void setY(double Yin) { y=Yin; }
void setZ(double Zin) { z=Zin; }
};
#endif
Point.cpp
#include "Point.h"
Point::Point()
{
x=0;
y=0;
z=0;
}
Point::Point(double x_create, double y_create, double z_create)
{
x=x_create;
y=y_create;
z=z_create;
}
test.cpp
#include "Line.h"
#include "Point.h"
#include <iostream>
using namespace std;
int main()
{
Point MyPoint(1,2,0);
cout << MyPoint.getX() << " " << MyPoint.getY() << " " << MyPoint.getZ() << endl;
Point MyPoint2(2,5,0);
cout << MyPoint2.getX() << " " << MyPoint2.getY() << " " << MyPoint2.getZ() << endl;
Line MyLine(MyPoint, MyPoint2);
cout << MyLine.getA() << " " << MyLine.getB() << " " << MyLine.getC() << endl;
//cout << MyLine.getP1() << " " << MyLine.getP2() << endl;
int i;
cin >> i;
return 0;
}