next up previous
Next: Piles d'entiers Up: No Title Previous: Relations entre classes :

Liaison retardée

Cet exercice illlustre la notion de liaison retardée indépendamment de la notion de sous-typage.

Soit le programme suivant :

exception CrLf;;

class lecture_chaine (m) =
  object (self)
  val msg = m
  val mutable res = ""

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

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

  method private lire_chaine_aux2 =
    let s = self#lire_char in
          if s = "\n" then raise CrLf
          else begin res <- res ^ s; self#lire_chaine_aux2 end
        
  method lire_chaine =
    try
      self#lire_chaine_aux
    with End_of_file  -> ()
      | CrLf -> ()

  method input = res <- ""; print_string msg; flush stdout;
                 self#lire_chaine
		   
  method get = res
end;;


class lecture_mdp (m) =
  object (self)
  inherit lecture_chaine m
  method lire_char = let c  = input_char stdin in
                     if (c != '\n') then begin
                       output_char stdout '*'; flush stdout
                     end;
                     let s = " " in s.[0] <- c; s
end;;


let login = new lecture_chaine("Login : ");;
let passwd = new lecture_mdp("Passwd : ");;

login#input;;
passwd#input;;

print_string (login#get);;print_newline();;
print_string (passwd#get);;print_newline();;

1.
Dessiner les relations entre classes
2.
Tracer les différents envois de message.
3.
En supposant être dans un mode caractères sans écho, indiquer ce qu'affiche le programme.



Emmanuel CHAILLOUX
1998-10-20