summaryrefslogtreecommitdiff
path: root/chapter2/newpoint.h
blob: 159f1fbefb9a1432d600adc8bdebddfe8ff28660 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// FILE: newpoint.h (revised from point.h in Figure 2.9 on page 58)
// CLASS PROVIDED: point (an ADT for a point on a two-dimensional plane)
//
// CONSTRUCTOR for the point class:
//   point(double initial_x = 0.0, double initial_y = 0.0)
//     Postcondition: The point has been set to (initial_x, initial_y).
//
// MODIFICATION MEMBER FUNCTIONS for the point class:
//   void shift(double x_amount, double y_amount)
//     Postcondition: The point has been moved by x_amount along the x axis
//     and by y_amount along the y axis.
//
//   void rotate90( )
//     Postcondition: The point has been rotated clockwise 90 degrees.
//
// CONSTANT MEMBER FUNCTIONS for the point class:
//   double get_x( ) const
//     Postcondition: The value returned is the x coordinate of the point.
//
//   double get_y( ) const
//     Postcondition: The value returned is the y coordinate of the point.
//
// NONMEMBER FUNCTIONS for the point class:
//   double distance(const point& p1, const point& p2)
//     Postcondition: The value returned is the distance between p1 and p2.
//
//   point middle(const point& p1, const point& p2)
//     Postcondition: The point returned is halfway between p1 and p2.
//
//   point operator +(const point& p1, const point& p2)
//     Postcondition: The sum of p1 and p2 is returned.
//
//   bool operator ==(const point& p1, const point& p2)
//     Postcondition: The return value is true if p1 and p2 are identical.
//
//   bool operator !=(const point& p1, const point& p2)
//     Postcondition: The return value is true if p1 and p2 are not identical.
//
//   ostream& operator <<(ostream& outs, const point& source)
//     Postcondition: The x and y coordinates of source have been 
//     written to outs. The return value is the ostream outs.
//
//   istream& operator >>(istream& ins, point& target)
//     Postcondition: The x and y coordinates of target have been 
//     read from ins. The return value is the istream ins.
//
// VALUE SEMANTICS for the point class:
//   Assignments and the copy constructor may be used with point objects.

#ifndef MAIN_SAVITCH_NEWPOINT_H   
#define MAIN_SAVITCH_NEWPOINT_H  
#include <iostream> // Provides ostream and istream

namespace main_savitch_2B
{          
    class point
    {
    public:
        // CONSTRUCTOR
        point(double initial_x = 0.0, double initial_y = 0.0);
        // MODIFICATION MEMBER FUNCTIONS
        void shift(double x_amount, double y_amount);
        void rotate90( );
        // CONSTANT MEMBER FUNCTIONS
        double get_x( ) const { return x; } 
        double get_y( ) const { return y; }
        // FRIEND FUNCTION
        friend std::istream& operator >>(std::istream& ins, point& target);
    private:
        double x, y; // x and y coordinates of this point
    };

    // NONMEMBER FUNCTIONS for the point class 
    double distance(const point& p1, const point& p2);
    point middle(const point& p1, const point& p2);
    point operator +(const point& p1, const point& p2);
    bool operator ==(const point& p1, const point& p2);
    bool operator !=(const point& p1, const point& p2);
    std::ostream& operator <<(std::ostream & outs, const point& source);
}

#endif