Classes which depend on each other?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 634
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Classes which depend on each other?

 
0
  #1
Mar 17th, 2008
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
  1. #ifndef LINE_H
  2. #define LINE_H
  3.  
  4. #include "Point.h"
  5. class Point;
  6.  
  7. class Line
  8. {
  9. // Ax+By+C = 0
  10. double A,B,C;
  11. Point *p1, *p2;
  12.  
  13. public:
  14.  
  15. Line();
  16.  
  17. Line(Point P1, Point P2);
  18.  
  19. ~Line() {}
  20.  
  21. double getA() { return A;}
  22. double getB() { return B;}
  23. double getC() { return C;}
  24. //Point getP1() {return *p1;}
  25. //Point getP2() {return *p2;}
  26. };
  27.  
  28. #endif

Line.cpp

  1. #include "Line.h"
  2.  
  3. Line::Line()
  4. {
  5. A=0;
  6. B=0;
  7. C=0;
  8. }
  9.  
  10. Line::Line(Point P1, Point P2)
  11. {
  12. p1=&P1;
  13. p2=&P2;
  14. double x1,x2,y1,y2;
  15. x1=P1.getX();
  16. x2=P2.getX();
  17. y1=P1.getY();
  18. y2=P2.getY();
  19.  
  20. //derived from slope(m) intercept(b) form
  21. double m,b;
  22. m = (y2-y1)/(x2-x1);
  23. b = y1 - ((y2-y1)/(x2-x1))*x1;
  24.  
  25. C = 1;//arbitrary, so set to 1
  26. B = -1/b;
  27. A = -B*m;
  28. }

Point.h
  1. #ifndef POINT_H
  2. #define POINT_H
  3.  
  4. #include "Line.h"
  5. class Line;
  6.  
  7. class Point
  8. {
  9. double x, y, z;
  10.  
  11. public:
  12.  
  13. Point();
  14.  
  15. Point(double x_create, double y_create, double z_create);
  16.  
  17. ~Point() {}
  18.  
  19. double getX() { return x; }
  20. double getY() { return y; }
  21. double getZ() { return z; }
  22.  
  23. void setX(double Xin) { x=Xin; }
  24. void setY(double Yin) { y=Yin; }
  25. void setZ(double Zin) { z=Zin; }
  26.  
  27. };
  28.  
  29. #endif

Point.cpp
  1. #include "Point.h"
  2.  
  3. Point::Point()
  4. {
  5. x=0;
  6. y=0;
  7. z=0;
  8. }
  9.  
  10. Point::Point(double x_create, double y_create, double z_create)
  11. {
  12. x=x_create;
  13. y=y_create;
  14. z=z_create;
  15. }

test.cpp
  1. #include "Line.h"
  2. #include "Point.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. Point MyPoint(1,2,0);
  10. cout << MyPoint.getX() << " " << MyPoint.getY() << " " << MyPoint.getZ() << endl;
  11.  
  12. Point MyPoint2(2,5,0);
  13. cout << MyPoint2.getX() << " " << MyPoint2.getY() << " " << MyPoint2.getZ() << endl;
  14.  
  15. Line MyLine(MyPoint, MyPoint2);
  16. cout << MyLine.getA() << " " << MyLine.getB() << " " << MyLine.getC() << endl;
  17. //cout << MyLine.getP1() << " " << MyLine.getP2() << endl;
  18.  
  19. int i;
  20. cin >> i;
  21. return 0;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Classes which depend on each other?

 
0
  #2
Mar 17th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 634
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Classes which depend on each other?

 
0
  #3
Mar 17th, 2008
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()
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Classes which depend on each other?

 
0
  #4
Mar 17th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Classes which depend on each other?

 
0
  #5
Mar 17th, 2008
if you just used pointers
  1. #ifndef LINE_H
  2. #define LINE_H
  3.  
  4. class Point;
  5.  
  6. class Line
  7. {
  8. // Ax+By+C = 0
  9. double A,B,C;
  10. Point *p1, *p2;
  11.  
  12. public:
  13.  
  14. Line();
  15.  
  16. Line(Point *P1, Point *P2);
  17.  
  18. ~Line() {}
  19.  
  20. double getA() { return A;}
  21. double getB() { return B;}
  22. double getC() { return C;}
  23. Point *getP1() {return p1;}
  24. Point *getP2() {return p2;}
  25. };
  26.  
  27. #endif
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 634
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Classes which depend on each other?

 
0
  #6
Mar 18th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC