Source of testfrnd.cpp


  1: // Filename: TESTFRND.CPP
  2: // Purpose:  To test overloading of the output operator <<.

  4: #include <iostream>
  5: using namespace std;


  8: class Point
  9: {
 10:     friend void Display(/* inout */ ostream& outputStream,
 11:                         /* in */    const Point& p);
 12:     // Pre:  The output stream outputStream is open.
 13:     // Post: Point p has been output in the form (x, y).
 14:     //       outputStream is still open.

 16: public:

 18:     Point(/* in */ int x = 0,
 19:           /* in */ int y = 0);

 21: private:

 23:     int x;
 24:     int y;
 25: };


 28: int main()
 29: {
 30:         Point p(1,2);
 31:         Display(cout, p);
 32:         cout << endl;

 34:         return 0;
 35: }


 38: //******************************************************************
 39: Point::Point(/* in */ int xInit,
 40:              /* in */ int yInit)
 41: {
 42:     x = xInit;
 43:     y = yInit;
 44: }


 47: //******************************************************************
 48: void Display(/* inout */ ostream& outputStream,
 49:              /* in */    const Point& p)
 50: // Pre:  The output stream outputStream is open.
 51: // Post: Point p has been output in the form (x, y).
 52: //       outputStream is still open.
 53: {
 54:     outputStream << "(" << p.x << ", " << p.y << ")";
 55: }