14.7 Programming Exercises
Exercies 14-1: Two classes share a file. Other areas of the program need to know
when this file is busy. Create a function that returns 1 when the
file is being used by either of these two classes.
Exercies 14-2: You are asked to write a booking program for the veterinarian; Dr.
Able Smith, PHD (Pigs, Horses, Dogs). Define a class type for each
animal. Each class should keep track in a private static variable of
the number of animals that have been defined using that class. Define
a function that returns the total number of animals (all three types
combined).
Exercies 14-3: Write a class in which each instance of the class can access a
stack—not one stack per instance, but one stack, period. Any
instance of the class can lock the stack for its own exclusive use
and unlock it later. Define member functions to perform the lock and
unlock functions.
As an added attraction, make the unlock function check to see that
the current instance of the class was the same instance that locked
the stack in the first place.
Exercies 14-4: You need to supply some I/O routines for handling lines in a file.
The basic definition of the line-number class is:
class line_number {
public:
void goto_line(int line);
int get_current_line( );
long int get_char_pos( );
}
The member functions are defined as:
- void goto_line(int line);
-
Positions the input file at specified line.
- int get_current_line( );
-
Returns the current line number (as set by
goto_line).
- long int get_char_pos( );
-
Returns the character position of the current line. (This is the
tricky one.)
Several line_number classes may be in use at any
time. The class maintains its own internal list so that it knows
which line_number classes are in use. When
goto_line is called, the function will scan the
list of line_number classes to find the one
nearest the given line number and use it to start scanning for the
given line number.
For example, suppose there are four active
line_number variables:
beginning
|
Line 0
|
chapter_start
|
Line 87
|
current_heading
|
Line 112
|
current_location
|
Line 52
|
You wish to move current_location to line 90. The
goto_line function would search the list for the
line nearest the new location (in this case
chapter_start) and use it to jump to line 87. It
then would read the file, character by character, until it saw three
end-of-line characters to position itself at line 90.
|