| | |
Classes which depend on each other?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 634
Reputation:
Solved Threads: 46
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
Line.cpp
Point.h
Point.cpp
test.cpp
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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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; }
>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.
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.
I'm here to prove you wrong.
•
•
Join Date: Feb 2008
Posts: 634
Reputation:
Solved Threads: 46
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()
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.
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.
I'm here to prove you wrong.
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
if you just used pointers
C++ Syntax (Toggle Plain Text)
#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
![]() |
Similar Threads
- Why won't this compile? (Java)
- Giving up on C++ (Geeks' Lounge)
- a question about inherite classes (Java)
- Hijackthis report, I just don't know (Viruses, Spyware and other Nasties)
- Pls help me (Java)
- Zone Alarm Security Alerts. (Viruses, Spyware and other Nasties)
- C++ Beginner - #include recursion problem (C++)
- C++ is dying a slow death (C++)
- NOOB question , graphical tiles (C++)
Other Threads in the C++ Forum
- Previous Thread: Read hexadecimal value from a file..
- Next Thread: sine
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap bmp c++ c/c++ calculator char class classes code compile compiler console conversion count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






