Printing to Screen in a Class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Printing to Screen in a Class

 
0
  #1
May 18th, 2005
ok im stuck how do u print to the screen in the main file from a class?

this is what im doin

  1. ostream &operator <<(ostream &outs, const Point &p)
  2. {
  3.  
  4. outs << "(" << p.getX() << ", " << p.getY() << ")"; // print out the value of the point as (x, y)
  5.  
  6. return outs;
  7. }

i want to print that in main.

this is my main

  1. #include "line.cpp"
  2. #include "point.cpp"
  3.  
  4. void main()
  5. {
  6. Point xVal;
  7. Point yVal;
  8.  
  9. xVal.setPoint(4.3,5.6);
  10. yVal.setPoint(6.8,3.9);
  11. xVal.getX();
  12. yVal.getY();
  13. xVal.printPolar();
  14. yVal.printPolar();
  15. <<();
  16.  
  17. }

how do i call the "<<" function so it prints right i keep gettin an error on the line
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: Printing to Screen in a Class

 
0
  #2
May 18th, 2005
here are the other files.

Point.h

  1. //*************************************************************************
  2. //
  3. // Point Class definition file: point.h
  4. //
  5. //*************************************************************************
  6.  
  7. #ifndef point_h
  8. #define point_h
  9.  
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. class Point
  15. {
  16. private:
  17. double x, // define a point as (x,y)
  18. y;
  19.  
  20. public:
  21. Point(); // default constructor
  22. Point(double xVal, double yVal); // argument constructor
  23.  
  24. void setPoint(double xVAl, double yVal); // allow change to current point value
  25.  
  26. double getX() const; // return the x value
  27. double getY() const; // return the y value
  28.  
  29. void printPolar() const; // print the polar coordinate form of the point (x, y)
  30. };
  31.  
  32. ostream &operator <<(ostream &outs, const Point &p); // overload the insertion operator for point objects
  33.  
  34. #endif

Point.cpp

  1. //***********************************************************************
  2. //
  3. // Point implementation file: Point.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <iostream>
  8. #include <cmath>
  9. #include <iomanip>
  10.  
  11. #include "point.h" // include the Point definition file
  12.  
  13. using namespace std;
  14.  
  15.  
  16. const double PI = acos(-1); // define PI
  17.  
  18. //******************************************************************
  19. //
  20. // Constructor name: Point
  21. //
  22. // Purpose: creates a default Point object of (0,0) i.e. the origin
  23. //
  24. //*******************************************************************
  25. Point::Point()
  26. {
  27. x = y = 0.0;
  28. }
  29.  
  30. //******************************************************************
  31. //
  32. // Constructor name: Point
  33. //
  34. // Purpose: creates a Point object of value (xVal, yVal)
  35. //
  36. // Input parameters: double xVal, x coordinate
  37. // double yVal, y coordinate
  38. //
  39. //*******************************************************************
  40. Point::Point(double xVal, double yVal)
  41. {
  42. x = xVal;
  43. y = yVal;
  44. }
  45.  
  46.  
  47. //******************************************************************
  48. //
  49. // Function name: setpoint
  50. //
  51. // Purpose: changes the x and y values of the point
  52. // to new values
  53. //
  54. // Input parameters: double xVal a new x value
  55. // double yVal a new y value
  56. //
  57. // Output parameters: none
  58. //
  59. // Return Value: void
  60. //
  61. //*******************************************************************
  62. void Point::setPoint(double xVal, double yVal)
  63. {
  64. x = xVal;
  65. y = yVal;
  66. }
  67.  
  68. //******************************************************************
  69. //
  70. // Function name: getX
  71. //
  72. // Purpose: returns the current x value of the point
  73. //
  74. // Input parameters: none
  75. //
  76. // Output parameters: none
  77. //
  78. // Return Value: double; the current value of x
  79. //
  80. //*******************************************************************
  81. double Point::getX() const
  82. {
  83. return x;
  84. }
  85.  
  86. //******************************************************************
  87. //
  88. // Function name: getY
  89. //
  90. // Purpose: returns the current y value of the point
  91. //
  92. // Input parameters: none
  93. //
  94. // Output parameters: none
  95. //
  96. // Return Value: double; the current value of y
  97. //
  98. //*******************************************************************
  99. double Point::getY() const
  100. {
  101. return y;
  102. }
  103.  
  104. //******************************************************************
  105. //
  106. // Function name: printPolar
  107. //
  108. // Purpose: prints to cout the value of the point in polar coordinates
  109. //
  110. // Input parameters: none
  111. //
  112. // Output parameters: none
  113. //
  114. // Return Value: void
  115. //
  116. //*******************************************************************
  117. void Point::printPolar() const
  118. {
  119. double r = sqrt(pow(x,2) + pow(y,2)), // calculate the distance of the point from the origin
  120. theta = atan(y / x); // calculate the angle of the point to the x-axis
  121.  
  122. int ioFlags = cout.flags(); // save the cout flags for latter use
  123.  
  124. cout << showpoint << fixed << setprecision(4); // set the cout object for 4 decimal places
  125.  
  126. cout << r << '<' << theta; // print the radius and angle of the point use , for the angle symbol
  127.  
  128. cout.flags(ioFlags); // restore the cout object to its original condition
  129. }
  130.  
  131.  
  132. //******************************************************************
  133. //
  134. // Function name: <<
  135. //
  136. // Purpose: prints to an ostream object the value of the point as (x, y) format
  137. //
  138. // Input parameters: outs and ostream object
  139. // p a point object
  140. //
  141. // Output parameters: outs an ostream object
  142. //
  143. // Return Value: a reference to an ostream object
  144. //
  145. //*******************************************************************
  146. ostream &operator <<(ostream &outs, const Point &p)
  147. {
  148.  
  149. outs << "(" << p.getX() << ", " << p.getY() << ")"; // print out the value of the point as (x, y)
  150.  
  151. return outs;
  152. }

line.h

  1. //*************************************************************************
  2. //
  3. // Line Class definition file: line.h
  4. //
  5. //*************************************************************************
  6.  
  7. #ifndef line_h
  8.  
  9. #define line_h
  10.  
  11. #include "point.h" //need the point definition file
  12.  
  13.  
  14. class Line
  15. {
  16. private:
  17. Point p1, // define a line as two points
  18. p2;
  19.  
  20. double slope, // slope of the line if it exists
  21. y_intercept; // y intercept of the line if it exists
  22.  
  23. bool vertical; // indicates if the line is vertical or not
  24.  
  25. void calcSlpYInt(); // calculates the line slope and y intercept
  26.  
  27. public:
  28. Line(); // default Line constructor
  29. Line(double x1, double y1, double x2, double y2); // Line constructor with double arguments for the two points
  30. Line(const Point &P1, const Point &P2); // Line constructor with point arguments
  31.  
  32. void setLine(double x1, double y1, double x2, double y2); // change the values of the line points with doubles
  33. void setLine(const Point &P1, const Point &P2); // change the value of the line points with new point objects
  34.  
  35. Point getP1() const; // return the point p1 of the line
  36. Point getP2() const; // return the point p2 of the line
  37.  
  38. double getSlope() const; //return the slope value if it exists
  39. double getYIntercept() const; // return the y intercept if it exits
  40.  
  41. bool vert() const; // returns a boolean value indicating if the line is vertical or not
  42.  
  43. };
  44.  
  45. ostream &operator <<(ostream &outs, const Line &l); // overload the insertion operator for line objects
  46.  
  47. #endif

line.cpp file

  1. //*************************************************************************
  2. //
  3. // Line Class implementation file: line.cpp
  4. //
  5. //*************************************************************************
  6.  
  7. #include <cstdlib>
  8. #include "line.h"
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. //******************************************************************
  15. //
  16. // Function name: calcSlpYint
  17. //
  18. // Purpose: calculates the slope and y intercept of the line object
  19. // to new values
  20. //
  21. // Input parameters: none
  22. //
  23. // Output parameters: none
  24. //
  25. // Return Value: void
  26. //
  27. //*******************************************************************
  28. void Line::calcSlpYInt()
  29. {
  30. if ( abs(p1.getX() - p2.getX()) < 0.0001 ) // check for a vertical line
  31.  
  32. vertical = true; // line is vertical
  33. else
  34. { // line is not vertical so calculate
  35. slope = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); // slope
  36. y_intercept = p2.getY() - slope * p2.getX(); // and y intercept
  37. vertical = false;
  38. }
  39.  
  40. }
  41.  
  42.  
  43. //******************************************************************
  44. //
  45. // Constructor name: Line
  46. //
  47. // Purpose: creates a default Line object containing the points (0,0) and (1,0)
  48. // i.e. a unit vector along the x-axis
  49. //
  50. //*******************************************************************
  51. Line::Line():p1(0,0), p2(1,0)
  52. {
  53. slope = y_intercept = 0; // for a horizontal line the slope and y intercept are 0
  54. vertical = false; // it is not a vertical line
  55.  
  56. }
  57.  
  58. //******************************************************************
  59. //
  60. // Constructor name: Line
  61. //
  62. // Purpose: creates a Line object with points (x1, y1) and (x2, y2)
  63. //
  64. // Input parameters: double x1, 1st x coordinate
  65. // double y1, 1st y coordinate
  66. // double x2, 2nd x coordinate
  67. // double y2, 2nd y coordinate
  68. //
  69. //******************************************************************
  70. Line::Line(double x1, double y1, double x2, double y2):p1(x1, y1), p2(x2,y2)
  71. {
  72. calcSlpYInt();
  73. }
  74.  
  75. //******************************************************************
  76. //
  77. // Constructor name: Line
  78. //
  79. // Purpose: creates a Line object with Point objects P1 and P2
  80. //
  81. // Input parameters: P1, 1st point
  82. // P2, 2nd point
  83. //
  84. //******************************************************************
  85. Line::Line(const Point &P1, const Point &P2):p1(P1.getX(), P1.getY()), p2(P2.getX(), P2.getY())
  86. {
  87. calcSlpYInt();
  88.  
  89. }
  90.  
  91.  
  92. //******************************************************************
  93. //
  94. // Function name: setLine
  95. //
  96. // Purpose: changes the point values of the line
  97. // to new values
  98. //
  99. // Input parameters: double x1, 1st x coordinate
  100. // double y1, 1st y coordinate
  101. // double x2, 2nd x coordinate
  102. // double y2, 2nd y coordinate
  103. //
  104. // Output parameters: none
  105. //
  106. // Return Value: void
  107. //
  108. //*******************************************************************
  109. void Line::setLine(double x1, double y1, double x2, double y2)
  110. {
  111. p1.setPoint(x1, y1); // change the value of point p1
  112. p2.setPoint(x2, y2); // change the value of point p2
  113.  
  114. calcSlpYInt();
  115. }
  116.  
  117.  
  118. //******************************************************************
  119. //
  120. // Function name: setLine
  121. //
  122. // Purpose: changes the point values of the line
  123. // to new values
  124. //
  125. // Input parameters: P1 a point replacement for p1
  126. // P2 a point replacement for p2
  127. //
  128. // Output parameters: none
  129. //
  130. // Return Value: void
  131. //
  132. //*******************************************************************
  133. void Line::setLine(const Point &P1, const Point &P2)
  134. {
  135. p1 = P1;
  136. p2 = P2;
  137.  
  138. calcSlpYInt();
  139. }
  140.  
  141.  
  142. //******************************************************************
  143. //
  144. // Function name: getP1
  145. //
  146. // Purpose: returns the point object p1
  147. //
  148. // Input parameters: none
  149. //
  150. // Output parameters: none
  151. //
  152. // Return Value: Point; the current value of p1
  153. //
  154. //*******************************************************************
  155. Point Line::getP1() const
  156. {
  157. return p1;
  158. }
  159.  
  160. //******************************************************************
  161. //
  162. // Function name: getP2
  163. //
  164. // Purpose: returns the point object p2
  165. //
  166. // Input parameters: none
  167. //
  168. // Output parameters: none
  169. //
  170. // Return Value: Point; the current value of p2
  171. //
  172. //*******************************************************************
  173. Point Line::getP2() const
  174. {
  175. return p2;
  176. }
  177.  
  178.  
  179. //******************************************************************
  180. //
  181. // Function name: getSlope
  182. //
  183. // Purpose: returns the slope of the line
  184. //
  185. // Input parameters: none
  186. //
  187. // Output parameters: none
  188. //
  189. // Return Value: double the slope of the line
  190. //
  191. //*******************************************************************
  192. double Line::getSlope() const
  193. {
  194. return slope;
  195. }
  196.  
  197.  
  198. //******************************************************************
  199. //
  200. // Function name: getYIntercept
  201. //
  202. // Purpose: returns the y intercept of the line
  203. //
  204. // Input parameters: none
  205. //
  206. // Output parameters: none
  207. //
  208. // Return Value: double the y intercept of the line
  209. //
  210. //*******************************************************************
  211. double Line::getYIntercept() const
  212. {
  213. return y_intercept;
  214. }
  215.  
  216.  
  217. //******************************************************************
  218. //
  219. // Function name: vert
  220. //
  221. // Purpose: returns true if the line is vertical and false if the line is not vertical
  222. //
  223. // Input parameters: none
  224. //
  225. // Output parameters: none
  226. //
  227. // Return Value: bool the condition of the line in terms of being vertical
  228. //
  229. //*******************************************************************
  230. bool Line::vert() const
  231. {
  232. return vertical;
  233. }
  234.  
  235.  
  236. //******************************************************************
  237. //
  238. // Function name: <<
  239. //
  240. // Purpose: overloads the insertion operator to print out the equation of the line
  241. //
  242. // Input parameters: outs an ostream object
  243. // l a line object
  244. //
  245. // Output parameters: outs an ostream object
  246. //
  247. // Return Value: a reference to an ostream object
  248. //
  249. //*******************************************************************
  250. ostream &operator <<(ostream &outs, const Line &l)
  251. {
  252.  
  253. int ioFlags = outs.flags(); // keep current state of ostream object
  254.  
  255. if(l.vert()) // check if the line is vertical or not
  256. outs << "x = " << l.getP1().getX(); // it is vertical so print the x = equation
  257. else // not vertical so print the y=mx+b equation
  258. {
  259. outs << "y = " << l.getSlope() << "x";
  260. if(abs(l.getYIntercept()) > 0.001) // only print the y intercept if it is non zero
  261. outs << showpos << l.getYIntercept(); // print the + sign if the y intercept is positive
  262. }
  263.  
  264. outs.flags(ioFlags); // return ostream object to original state
  265.  
  266. return outs;
  267. }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Printing to Screen in a Class

 
0
  #3
May 18th, 2005
If you defined your operator<< correctly, which seems to be the case, then you don't need to do anything special. That's the nice thing about extending iostreams, you can use them just like you would the standard library stuff:
  1. cout << xVal << ' ' << yVal << '\n';
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: Printing to Screen in a Class

 
0
  #4
May 18th, 2005
oh didnt think it was that easy thanks
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Printing to Screen in a Class

 
0
  #5
May 18th, 2005
It's just that easy. Of course, if you want to get some really cool features going, it takes more work. But for the most part the simple stuff is enough.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC