
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

The concept of abstraction (information hiding) can be supported both through scoping rules and through type checking. For example, the following ML code defines an abstraction through scoping rules and higher-order functions.
fun mk_exam(passwd : string) = let val p_problems = ref nil; val p_solutions = ref nil; fun add_problem(question:string, answer: string) = (p_problems := question :: !p_problems; p_solutions := answer :: !p_solutions); fun get_problems() = !p_problems; fun get_solutions(pass) = if (pass = passwd) then !p_solutions else nil in { add=add_problem, problems = get_problems, solutions = get_solutions } end; val midterm = mk_exam("17db"); #add(midterm)("problem1", "solution1"); #problems(midterm)();
Run the above ML code and check out what is the returned type for midterm and what is the last returned value.
abstype Exam = Exam of string list ref * string list ref * string with fun mk_exam(passwd) = Exam(ref nil, ref nil, passwd) fun add_problem(Exam(p_problems,p_solutions,passwd), question, answer) = (p_problems := question :: !p_problems; p_solutions := answer :: !p_solutions) fun get_problems(Exam(p_problems,p_solutions,passwd)) = !p_problems fun get_solutions(Exam(p_problem,p_solutions,passwd), pass) = if (pass = passwd) then !p_solutions else nil end;
Translate the above abstraction into an object-oriented class in C++.