CSC123/252 Quiz Study Guide Topics of Focus: 1. C# delegates 2. Inheritance in C# - understand the *purpose* as well as the mechanics of dynamic dispatch. Know the consequences of the keywords "virtual", "override" and "new" with respect to inheritance. Understand the difference between interface and superclass. 3. Types and type casting, including how they differ in C/C++ and Java/C# 4. The Observer and Visitor Design Patterns. For the problems that require you to write C# code, there'll be nothing exotic and you'll probably work off of some code that I will be providing. -------------- Practice Problems 1. Consider the following inheritance structure in C# and code that tries to use it: interface I { void f(); } class S {} class A : S,I { public void f() {Console.WriteLine("A.f"); } } class B : S,I { public void f() {Console.WriteLine("B.f"); } } ... Given: I u = new B(); S v = new A(); A n = new A(); For each case below, determine and explain if it's valid, causes a compile-time error, or a runtime error. Consider each case independently. 1. v = n; 2. n = (A) new B(); 3. n = (A) u; 4. v.f(); 5. ((A)v).f(); 6. ((I)v).f(); Write a real program to check your answers. 2. In the Visitor Pattern, where is the "accept" function located? (in visitor or visitee class?). If the visitee classes are A, B and C, write the interface for the visitor classes. 3. Describe the difference between natural and ad-hoc polymorphism. 4. Describe at least one important way in which C++ templates for types differ from C#/Java generics (parametric types). 5. Some people uses the superclass "object" to implement polymorphic code. What are the disadvantages of this approach and how do parametric types improve the situation. ============== Answers: (don't look until you tried first) ============== 1. Write your own program to find the answers. 2. the accept function is located inside each visitee class. interface visitee { object visit(A x); object visit(B x); object visit(C x); } or interface visitee { T visit(A x); T visit(B x); T visit(C x); } 3. A procedure is naturally polymorphic if no further work is needed to make it polymorphic. For example: finding the size of a binary tree. An ad-hoc polymorphic function uses overloading or dynamic dispatch so that one piece of code can invoke different algorithms when needed. In other words, a naturally polymorphic function is one procedure that works on multiple types of data. An ad-hoc polymorphic function is several different procedures which are dispatched to automatically. For example, "+" is ad-hoc polymorphic: it is one symbol which stands for many procedures, including integer addition, floating-point addition, and string concatenation. 4. C++ templates makes a fresh copy of code for each instantiation of the template. The template itself is not actually a piece of code like a generic class. C++ implements no contraints on type variables, such as "where T : A" in C#. Thus no type-checking of the template code is possible. Thus we can compile a template class/function without knowing if there are inherent type errors. Java/C# statically type checks polymorphic code, and this is a very advanced programming language feature. 5. There are two disadvantages to using "object" instead of a type variable : 1. need to type-cast, for example, (int)obj before using. This is a runtime overhead. 2. can't check the type casting at compile time, since "object" can possibly be anything. This is the principal disadvantage.