-- this class is the super class of classes A and B and -- is used for testing purposes of Inheritance and para- -- meter passing to the functions. class A inherits IO{ value : Int <- 6; -- initiation of the variable is done here name : String; -- this method is used to initiate the attributes of the object. one -- important note is it returns a type od SELF_TYPE where it will -- be useful(return their own types) for classes inherited from class A. init(nm : String) : SELF_TYPE { { name <- nm; self; } }; -- this method is used to print the name information of the object. print() : String { { out_string(name); out_string("\n"); name; } }; }; -- this class inherits from class A. class B inherits A { -- this method is a recursive method and is used to test the recursive -- function calling in Cool. it calculates factorial value of a given -- integer and returns this value back. factorial(value : Int) : Int { if (value <= 1) then 1 else value * factorial(value-1) -- recursive call fi }; }; -- this class inherits from class B and class A(since B inherits from class A). class C inherits B { -- this function is used to test the method overloading in child classes. -- also it uses the print() function of the ancesstor class A by calling -- this function in @.id(, ..., ) form. print() : String { { out_string("My print function!!!"); self@A.print(); } }; -- this function simply adds given two numbers. add2Numbers(num1 : Int, num2 : Int) : Int { num1 + num2 }; -- this function simply adds given three numbers and uses function call -- in the form (,...,) to test such a function calling. add3Numbers(num1 : Int, num2 : Int, num3 : Int) : Int { add2Numbers(add2Numbers(num1, num2),num3) }; }; -- this class is the Main class where the program is run using the main() function. class Main inherits IO { -- we have three attributes that are used to test different cases. a : A; b : B; c : C; main() : Object { { a <- (new A).init("Object A"); -- A type object a.print(); b <- (new B).init("Object B"); -- B type object b.print(); out_int(b.factorial(17)); -- testing recursive func. out_string("\n"); c <- (new C).init("Object C"); -- C type object c.print(); -- a method call with parameter passing as function calls and arithmetic -- expressions. out_int(c.add3Numbers(c.factorial(0+1), 2 * 1, c@B.factorial(3))); out_string("\n"); -- this statement is used to test let statement attribute initiation. -- two variables of same name are used to test which value is used. let num1 : Int <- 5, num2 : Int, num1 : Int <- 10, char : String <- "Test!!!\t" in { out_string(char); num2 <- num1; -- the second value(10) is used for num1 out_string("The value of num2 is : "); out_int(num2); out_string("\n"); }; -- this case statement is used to see effect of init method where the return type is -- SELF_TYPE. It checks the type of test expression and prints the value based on the -- object type. case ((new B).init("Object C2")) of a:A => out_string("AAA\n"); b:B => out_string("BBB\n"); c:C => out_string("CCC\n"); esac; } }; };