Lecture 10 – September 26, 2007
We looked
at functions in the body of package Fractions and worked in teams to determine
if they were correct or not.
We traced
several of them on the board.
We
discussed that when we call an overloaded function we need to use a somewhat
cumbersome call such as
myFraction :=
Fractions.”+” ( fractionA,
fractionB);
This is a
form of prefix notation.
If we use a
USE clause for the package we can use infix notation to call the function as
myFraction :=
fractionA + fractionB;
It is
important to know how to use the fully qualified notation because there are
times when a USE clause is not sufficient (i.e. the compiler can not tell which
function from which package you want).
This can happen if the same operator is overloaded in a number of
packages and the operands in the function are of the same time.
We looked
at a snippet of code from Professor Adams’ test program and discussed the fact
that whatever occurs between the word GENERIC and the word PACKAGE in a generic
package specification is a package parameter that must be used in the package
instantiation.
ÏÏÏWITH Discrete_Set;
ÏÏÏWITH Ada.text_io;
ÏÞßàPROCEDURE set_test IS
ÏϧÏïÏTYPE fruit IS (apple,
berry, cherry, plum, peach, apricot, mango); -- enumerated type declaration
ÏÏ§ÏØÓìPACKAGE fruit_io IS NEW ada.text_io.enumeration_io
(fruit); -- instantiation for
printing/reading values of the enumerated type
ÏÏ§ÏØÓìPACKAGE mySet IS NEW Discrete_Set(Element_Type => fruit); -- instantiation of generic package Discrete_Set
Ïϧ
ÏϧÏíÏsetA, setB, setC : mySet.Set_Type; -- declarations of variables of Set_Type (called set
variables in assignment)
ÏϧÏíÏisInSet : boolean;
Ïϧ
Ïϧbegin
Ïϧ
ÏϨ¹¹ÏsetA := mySet.Universal_Set; --
initializing setA to Universal_Set
ÏϨ¹¹ÏsetB := mySet.Empty_Set; --
initializing setB to the Empty_Set
ÏϨ¹¹ÏsetC := mySet."+"(setA, setB); --
initializing setC to the union of setA
and setB
Ïϧ
Ïϧ -- listing all of the
elements that are in setA,one per line
ÏϨ¹¹±FOR i IN fruit LOOP
ÏϧÏÏ7¹³´IF (mySet.is_Member (setA, i)) THEN -- yes, I cheated and embedded a function call in an IF statement
ÏϧÏÏ5Ï6¨¹¹Ïfruit_io.put (i);
ÏϧÏÏ5Ï6¾¹¹ÏAda.text_io.new_line;
ÏϧÏÏ5϶ÏEND IF;
ÏϧÏϰEND LOOP;
ÏϨ¹¹ÏAda.text_io.new_line;
End set_test;
Note
the following:
Your enumerated type does not have
to be fruit.
Your program will be longer than
mine which just shows you declarations and two function calls.