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;
}``````

## Recommended Answers

>The Point class needs to have member functions that return type Line
That sounds like a design flaw, actually.

>if I have a situation like this, then in class Line I can only have members of type *Point
That's only a problem if both classes are mutually dependent. In …

>I thought we could address the problem without having the circularity.
The best way is to not introduce circularity. Personally, I think you should be designing your Point class to be completely independent, then the Line class to use the Point class. In other words, I see no reason why …

## All 5 Replies

>The Point class needs to have member functions that return type Line
That sounds like a design flaw, actually.

>if I have a situation like this, then in class Line I can only have members of type *Point
That's only a problem if both classes are mutually dependent. In other words, the Line class has a Point data member and the Point class has a Line data member. This kind of circular relationship causes infinite recursion. However, in your case the Point class doesn't contain a Line object, so you don't have to store pointers to Point objects in the Line class, if that makes sense.

right right, I do need both classes to have the other type. I should have been clearer that in this example I didn't actually have the Point class contain a Line object, but I thought we could address the problem without having the circularity.

The reason I want these is so I can do MyPoint.GetLineTo(AnotherPoint)
and
MyLine.GetTwoPoints()

>I thought we could address the problem without having the circularity.
The best way is to not introduce circularity. Personally, I think you should be designing your Point class to be completely independent, then the Line class to use the Point class. In other words, I see no reason why a Point should know or care how to make a Line. That's the job of the Line class.

if you just used pointers

``````#ifndef LINE_H
#define LINE_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``````

Thanks guys. I ended up making the point class completely independent and everything is much smoother now. I tested out the pointers also, and it works fine, but then I just have to dereference everything in the main program , which still seems annoying to my beginner self. haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.