## CSC 123/252 C\# Assignment ### Part I: Implementing Multiple Inheritance C\# (and Java, Kotlin, etc) does not allow for the inheritance of multiple classes (abstract or not), only interfaces. In ``` class A : B1, B2, B3 { ... } ``` only `B1` can be a class, `B2` and `B3` must be interfaces But we want to have multiple inheritance anyway! Recall that I managed to emulate inheritance in C. A "subclass" is just a class that can also behave like its superclass (we're only interested in public inheritance). We just need to store an instance of the "superclass" inside the subclass and rebuild the interface. For example In C if I wanted to emulate that `struct B` "inherits" from `struct A`, I just define B to contain an instance of A: `struct B { struct A super, ...}`. Then I can re-write all the functions on B to be called on A by redirecting the calls to inner instance, without having to re-create the contents of the functions. Two base (super) classes A and B have been pre-compiled and available in the file **`csasn1bases.dll`**. They implement the following interfaces ``` public interface Ia // implemented by class A { void f(); void g(); void h1(); } public interface Ib // implemented by class B { void f(); void g(); void h2(); } ``` Furthermore, the implementations of these function both classes are all `virtual`, which means they can be overridden. You are to show how to construct a class ``` public class C : A, Ia, Ib ``` That inherits from A but implements both interfaces by "faking" the additional inheritance from B. This class should inherit both h1 from A and h2 from B. FURTHERMORE, ***C MUST inherit `f()` from class A and `g()` from class B***, so some overriding would be needed. Your class C must work with the supplied .dll file. To compile a C\# program together with a .dll, do `csc yourprogram.cs /r:csasn1bases.dll` or `mcs program.cs /r:csasn1bases.dll` in Mono (Mac/Linux). FYI: to create a .dll, write a program without a Main and do `csc /t:library program.cs` or `mcs /t:library program.cs`. You shouldn't have to do this step, except if your verion of .Net is not compatible with my .dll (unlikely, but see me if true). Test your program with the following main (place in public class that match the filename): ``` public static void Main() { C n = new C(); //don't put any print statements in your C constructor n.f(); n.g(); n.h1(); n.h2(); //exact output will show proper inheritance } ``` **Anyone who just copy and paste the code from A and B by decompiling the .dll will lose 100000 points**. This doesn't mean you can't define f, g and h1/h2 in your subclasses It means that you cannot copy the "Console.WriteLine" code, because it really represents arbitrarily complex code. Your solution must work in general, regardless of how these functions are implemented. ### Part Ib. Slightly Harder Scenario This time there are no interfaces Ia, Ib to give you a clue. Instead you just known that the .dll contains classes A2 and B2 with the following structure. ``` public class A2 { protected virtual void f() { /* inherit and see... */ } protected virtual void g() { /* inherit and see... */ } protected virtual void h1() { /* inherit and see... */ } } public class B2 { protected virtual void f() { /* inherit and see... */ } protected virtual void g() { /* inherit and see... */ } protected virtual void h2() { /* inherit and see... */ } } ``` Each of the functions do something that will be revealed once you do the assignment correctly. For this assignment, you are not allowed to decompile the .dll and you are **not allowed to use reflection.** Write a `public class C2` that inherits all the functionality of A2 and B2, with the same stipulation that it should **take f() from A2 and g() from B2**. Hint: you may define other interfaces, classes to help you, but YOU MAY NOT CHANGE A2, B2 (do not change the .dll) Hint: to call the base class version of a function, call `base.function(..)` Unlike part 1, this time both inheritances may have to be emulated. ``` public static void Main() // main for testing part II { C2 n = new C2(); //don't put any print statements in your C2 constructor n.f(); n.g(); n.h1(); n.h2(); //exact output will show proper inheritance } ```