Chapter 4:
objects
Questions
1. What
is the data structure of a basic type? What
is the structure of a reference type? Draw
an example of each.
2. What
happens when an object is created? State
what happens when there is no creation routine for the class, and when
there is a creation routine for the class. Why
must a creation routine be a procedure?
3. What
code is needed in the client and in the supplier classes to create an object
using a creation routine? If there is
a creation routine in a class, can I create an object without calling that
creation routine? Can a creation routine
receive arguments? Can a class have
several creation routines?
4.
Describe the mechanism used by Eiffel to find the code needed to execute
a feature call from one client to another (object.feature).
What happens when there is no explicit object
mentioned? What is the value of Current?
5. What
is the difference between an operator and a function? What
is an infix operator? What is a prefix
operator? Give three examples of operators
in Eiffel.
6. Show
the format of an infix operator definition. Write
a definition for the infix operator #mod (modulus). What
is the signature of this operator?
7. Define
a class FAHRENHEIT that stores a temperature in degrees Fahrenheit.
Define a class CELSIUS that stores a temperature in degrees Celsius
(Centigrade). Write a function in FAHRENHEIT that returns a REAL
number, the temperature in Centigrade. Write a function in CELSIUS
that returns a REAL number, the temperature in Fahrenheit. Write
a DRIVER root class to read in a single temperature, create the
two objects, and display their values.
The basic form of the creation routine
in the root class is
make is
-- drive the rest of the system
local
fahrenheit: FAHRENHEIT
celsius: CELSIUS
do
io.putstring ("Enter the temperature in fahrenheit: ")
io.readreal
!!fahrenheit.make (io.lastreal)
!!celsius.make (fahrenheit.to_celsius)
fahrenheit.show
celsius.show
end -- make
8. A farmer
has 3 pigs. A pig has a name, weight, and age. Write a system that creates
the three pigs, then shows their average weight. Write your system in the
following stages:
a) List
the classes
b) Draw
a client chart
c) For
the class PIG, define the attributes and the signature and header
for each routine
d) For
the root class FARMER, define the attributes, signatures, and headers
e) Code
the class PIG
f)
Code the class FARMER
g) Run
the system
9. Look
at the rock concert specification in Chapter 2, question 16. Change the
solution from its procedural form to an OO form; that is, change it from
a single block of code to a set of routines in a set of classes. Hint:
you need to create three objects of the same class.
10. Write
equality functions for the classes POINT, LINE, and TRIANGLE.
An equality function returns true if two objects are equal in some sense;
here, two objects (of the same type) are equal if they are in the same
location. An equality routine has the header
is_equal (this: like Current): BOOLEAN is ...
The keyword like allows the type
of an argument to be defined as "like this one".