Previous Contents Next


Stacks as Objects

Let us reconsider the stacks example, this time in object oriented style.
  1. Define a class intstack using Objective CAML's lists, implementing methods push, pop, top and size.

  2. Create an instance containing 3 and 4 as stack elements.

  3. Define a new class stack containing elements answering the method
    print : unit -> unit.

  4. Define a parameterized class ['a] stack, using the same methods.

  5. Compare the different classes of stacks.

Delayed Binding

This exercise illustrates how delayed binding can be used in a setting other than subtyping.

Given the program below:
  1. Draw the relations between classes.

  2. Draw the different messages.

  3. Assuming you are in character mode without echo, what does the program display?

exception CrLf;;
class chain_read (m) =
object (self)
val msg = m
val mutable res = ""

method char_read =
let c = input_char stdin in
if (c != '\n') then begin
output_char stdout c; flush stdout
String.make 1 c

method private chain_read_aux =
while true do
let s = self#char_read in
if s = "\n" then raise CrLf
else res <- res ^ s;

method private chain_read_aux2 =
let s = self#lire_char in
if s = "\n" then raise CrLf
else begin res <- res ^ s; self#chain_read_aux2 end

method chain_read =
with End_of_file -> ()
| CrLf -> ()

method input = res <- ""; print_string msg; flush stdout;

method get = res

class mdp_read (m) =
object (self)
inherit chain_read m
method char_read = let c = input_char stdin in
if (c != '\n') then begin
output_char stdout '*'; flush stdout

let s = " " in s.[0] <- c; s

let login = new chain_read("Login : ");;
let passwd = new mdp_read("Passwd : ");;
print_string (login#get);;print_newline();;
print_string (passwd#get);;print_newline();;

Abstract Classes and an Expression Evaluator

This exercise illustrates code factorization with abstract classes.

All constructed arithmetic expressions are instances of a subclass of the abstract class expr_ar.
  1. Define an abstract class expr_ar for arithmetic expressions with two abstract methods: eval of type float, and print of type unit, which respectively evaluates and displays an arithmetic expression.

  2. Define a concrete class constant, a subclass of expr_ar.

  3. Define an abstract subclass bin_op of expr_ar implementing methods eval and print using two new abstract methods oper, of type (float * float) -> float (used by eval) and symbol of type string (used by print).

  4. Define concrete classes add and mul as subclasses of bin_op that implement the methods oper and symbol.

  5. Draw the inheritance tree.

  6. Write a function that takes a sequence of Genlex.token, and constructs an object of type expr_ar.

  7. Test this program by reading the standard input using the generic lexical analyzer Genlex. You can enter the expressions in post-fix form.

The Game of Life and Objects.

Define the following two classes:
  1. Write the class cell.

  2. Write an abstract class absWorld that implements the abstract methods display, getCell and setCell. Leave the method nextGen abstract.

  3. Write the class world, a subclass of absWorld, that implements the method nextGen according to the growth rules.

  4. Write the main program which creates an empty world, adds some cells, and then enters an interactive loop that iterates displaying the world, waiting for an interaction and computing the next generation.

Previous Contents Next