home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / tstpoint.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  55 lines

  1. //             tstpoint.cpp
  2. //
  3. // Synopsis  - The program instantiates two Point objects, 
  4. //             initializes the values, displays the objects, and 
  5. //             calculates and displays the distance of each point 
  6. //             from the origin.
  7. //
  8. // Objective - To demonstrate the instantiation of objects and 
  9. //             the invocation of class member functions by an 
  10. //             object,and passing an object as an argument to a 
  11. //             top-level function.
  12.  
  13. // Include Files
  14. #include <iostream.h>
  15. #include <math.h>
  16. #include "point.h"                                         // Note 1
  17.  
  18. // Function Prototypes
  19. double distance_from_origin( Point p );                    // Note 3
  20. // PRECONDITION:  p should have both x and y initialized
  21. //
  22. // POSTCONDITION: The return value is the distance of p
  23. //                from the origin.
  24.  
  25. int main()
  26. {
  27.     Point p1, p2;                                          // Note 2
  28.  
  29.     p1.set_x( 0 );                                         // Note 4
  30.     p1.set_y( 0 );                                         // Note 4
  31.  
  32.     cout << "Point 1 is ";
  33.     p1.display();                                          // Note 4
  34.  
  35.     p2.set_x( 3 );                                         // Note 5
  36.     p2.set_y( 4 );                                         // Note 5
  37.     cout << " and Point 2 is ";
  38.     p2.display();                                           // Note 5
  39.     cout << endl;
  40.  
  41.     cout << "The distance of Point 1 from the origin is "
  42.          << distance_from_origin( p1 ) << endl;            // Note 3
  43.  
  44.     cout << "The distance of Point 2 from the origin is "
  45.          << distance_from_origin( p2 ) << endl;               // Note 3
  46.  
  47.     return 0;
  48. }
  49.  
  50. /********************************** distance_from_origin() *********/
  51. double distance_from_origin( Point p )
  52. {                                               // Note 4 and Note 5
  53.     return sqrt( p.x_is()*p.x_is() + p.y_is()*p.y_is() );
  54. }
  55.