summaryrefslogtreecommitdiff
path: root/chapter2/throttle.h
blob: 8707b3735c3f6f3eca55ff13c628d3fcad15586d (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
// FILE: throttle.h
// CLASS PROVIDED: throttle (part of the namespace main_savitch_chapter2)
//
// CONSTRUCTORS for the throttle class:
//   throttle( )
//     Postcondition: The throttle has one position above the shut_off position,
//     and it is currently shut off.
//
//   throttle(int size)
//     Precondition:  size > 0.
//     Postcondition: The throttle has size positions above the shut_off
//     position, and it is currently shut off.
//
// MODIFICATION MEMBER FUNCTIONS for the throttle class:
//   void shut_off( )
//     Postcondition: The throttle has been turned off.
//
//   void shift(int amount)
//     Postcondition: The throttle's position has been moved by
//     amount (but not below 0 or above the top position).
//
// CONSTANT MEMBER FUNCTIONS for the throttle class:
//   double flow( ) const
//     Postcondition: The value returned is the current flow as a 
//     proportion of the maximum flow.
//
//   bool is_on( ) const
//     Postcondition: If the throttle's flow is above 0 then
//     the function returns true; otherwise it returns false. 
//
// VALUE SEMANTICS for the throttle class: (See discussion on page 51.)
//    Assignments and the copy constructor may be used with throttle objects.

#ifndef MAIN_SAVITCH_THROTTLE 
#define MAIN_SAVITCH_THROTTLE              

namespace main_savitch_2A
{
    class throttle
    {
    public:
        // CONSTRUCTORS
        throttle( );
        throttle(int size);
        // MODIFICATION MEMBER FUNCTIONS
        void shut_off( ) { position = 0; }
        void shift(int amount);
        // CONSTANT MEMBER FUNCTIONS
        double flow( ) const { return position / double(top_position); } 
        bool is_on( ) const { return (position > 0); }
    private:
        int top_position;
        int position;
    };

}

#endif