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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
// FILE: organism.h (part of the namespace main_savitch_14)
// CLASSES PROVIDED: organism, plant, animal, herbivore
//
//
// DOCUMENTATION for the organism class:
// The organism class can simulate any growing organism, such as a plant or
// animal.
//
// CONSTRUCTOR for the organism class:
// organism(double init_size = 1, double init_rate = 0)
// Precondition: init_size >= 0. Also, if init_size = 0, then init_rate is
// also zero.
// Postcondition: The organism being simulated has been initialized. The
// value returned from get_size( ) is now init_size (measured in ounces),
// and the value returned from get_rate( ) is now init_rate (measured in
// ounces per week).
//
// MODIFICATION MEMBER FUNCTIONS for the organism class:
// void simulate_week( )
// Postcondition: The size of the organism has been changed by its current
// growth rate. If the new size is less than zero, then the actual size is
// set to zero rather than to a negative value, and the growth rate is also
// set to zero.
//
// void assign_rate(double new_rate)
// Postcondition: The organism's growth rate has been changed to new_rate
// (measured in ounces per week).
//
// void alter_size(double amount)
// Postcondition: The given amount (in ounces) has been added to the
// organism's current size. If this results in a new size less than zero,
// then the actual size is set to zero rather than to a negative value, and
// the growth rate is also set to zero.
//
// void death( )
// Postcondition: The organism's current size and growth rate have been
// set to zero.
//
// CONSTANT MEMBER FUNCTIONS for the organism class:
// double get_size( ) const
// Postcondition: The value returned is the organism's current size
// (in ounces).
//
// double get_rate( ) const
// Postcondition: The value returned is the organism's current growth rate
// (in oz/week).
//
// bool is_alive( ) const
// Postcondition: If the current size is greater than zero, then the return
// value is true; otherwise the return value is false.
//
//
// DOCUMENTATION for the plant class:
// plant is a derived class of the organism class. All the organism public
// member functions are inherited by a plant. In addition, a plant has
// these extra member functions:
//
// CONSTRUCTOR for the plant class:
// plant(double init_size=0, double init_rate=0)
// Same as the organism constructor.
//
// MODIFICATION MEMBER FUNCTIONS for the plant class:
// void nibbled_on(double amount)
// Precondition: 0 <= amount <= get_size( ).
// Postcondition: The size of the plant has been reduced by amount.
// If this reduces the size to zero, then death is activated.
//
//
// DOCUMENTATION for the animal class:
// animal is a derived class of the organism class. All the organism public
// member functions are inherited by an animal. In addition, an animal has
// these extra member functions:
//
// CONSTRUCTOR for the animal class:
// animal(double init_size=0, double init_rate=0, double init_need=0)
// Precondition: init_size >= 0, and init_need >= 0. Also, if
// init_size = 0, then init_rate is also zero.
// Postcondition: The organism being simulated has been initialized. The
// value returned from get_size( ) is now init_size (measured in ounces),
// the value returned from get_rate( ) is now init_rate (measured in ounces
// per week), and the animal must eat at least init_need ounces of food
// each week to survive.
//
// MODIFICATION MEMBER FUNCTIONS for the animal class:
// void assign_need(double new_need)
// Precondition: new_need >= 0.
// Postcondition: The animal's weekly food requirement has been changed to
// new_need (measured in ounces per week).
//
// void eat(double amount)
// Precondition: amount >= 0.
// Postcondition: The given amount (in ounces) has been added to the amount
// of food that the animal has eaten this week.
//
// void simulate_week( ) -- overriden from the organism class
// Postcondition: The size of the organism has been changed by its current
// growth rate. If the new size is less than zero, then the actual size is
// set to zero rather than to a negative value, and the growth rate is also
// set to zero. Also, if the animal has eaten less than its required need
// over the past week, then death has been activated.
//
// CONSTANT MEMBER FUNCTIONS for the animal class:
// double still_need( ) const
// Postcondition: The return value is the ounces of food that the animal
// still needs to survive the current week (i.e., its total weekly need
// minus the amount that it has already eaten this week.)
//
// double total_need( ) const
// Postcondition: The return value is the total amount of food that the
// animal needs to survive one week (measured in ounces).
//
//
// DOCUMENTATION for the herbivore class:
// plant is a derived class of the animal class. All the animal public
// member functions are inherited by a herbivore. In addition, a herbivore has
// this extra member function:
//
// CONSTRUCTOR for the herbivore class:
// herbivore(double init_size=0, double init_rate=0, double init_need=0)
// Same as the animal constructor.
//
// MODIFICATION MEMBER FUNCTIONS for the herbivore class:
// void nibble(plant& meal)
// Postcondition: eat(amount) and meal.nibbled_on(amount) have both been
// activated. The amount is usually half of the plant, but it will never be
// more than 10% of the herbivore's weekly need, nor more than the amount that
// the animal still needs to eat to survive this week.
//
//
// VALUE SEMANTICS for the organism class and its derived classes:
// Assignments and the copy constructor may be used with all objects.
#ifndef ORGANISM_H // Prevent duplicate definition
#define ORGANISM_H
namespace main_savitch_14
{
class organism
{
public:
// CONSTRUCTOR
organism(double init_size = 1, double init_rate = 0);
// MODIFICATION FUNCTIONS
void simulate_week( ) { alter_size(rate); }
void assign_rate(double new_rate) { rate = new_rate; }
void alter_size(double amount);
void death( );
// CONSTANT FUNCTIONS
double get_size( ) const { return size; }
double get_rate( ) const { return rate; }
bool is_alive( ) const { return (size > 0); }
private:
double size;
double rate;
};
class plant : public organism
{
public:
// CONSTRUCTOR
plant(double init_size = 1, double init_rate = 0);
// MODIFICATION FUNCTIONS
void nibbled_on(double amount);
};
class animal : public organism
{
public:
// CONSTRUCTOR
animal(double init_size = 1, double init_rate = 0, double init_need = 0);
// MODIFICATION FUNCTIONS
void assign_need(double new_need);
void eat(double amount);
void simulate_week( ); // Overriden from the organism class
// CONSTANT FUNCTIONS
double still_need( ) const;
double total_need( ) const { return need_each_week; }
private:
double need_each_week;
double eaten_this_week;
};
class herbivore : public animal
{
public:
// CONSTRUCTOR
herbivore(double init_size = 1, double init_rate = 0, double init_need = 0);
// MODIFICATION FUNCTIONS
void nibble(plant& meal);
};
}
#endif
|