/* CSC 123/252 C# assignment sample solutions compile with mcs csasnsol24.cs /r:csasn1bases.dll */ using System; // Part I.I public class C : A, Ia, Ib { private B b = new B(); public override void g() { b.g(); } public virtual void h2() { b.h2(); } // the rest are directly inherited from A }//C // Part I.II // Note, if you want to keep the items inherited from A protected // in the subclass, you can use direct inheritance on A, // but if they're to become public, you will need to adopt them // to public versions of the functions anyway. My solution // emulates both inheritances using "proxy" classes that // directly inherit from A and B and re-export the protected // methods as public. class proxyA2 : A2 { public void pf() { f(); } public void pg() { g(); } public void ph1() { h1(); } } class proxyB2 : B2 { public void pf() { f(); } public void pg() { g(); } public void ph2() { h2(); } } public class C2 { private proxyA2 a2 = new proxyA2(); private proxyB2 b2 = new proxyB2(); public void f() { a2.pf(); } public void g() { b2.pg(); } public void h1() { a2.ph1();} public void h2() { b2.ph2(); } } //////// PART II (Operator Overloading) class time { protected int mins, secs; public time(int m, int s) { mins = m + s/60; secs = s % 60; }// public override string ToString() { // overrides object.ToString return mins+"m"+secs+"s"; } ///////////////// KEY: have the overloaded operator call the ///////////////////// dynamically dispatched function: public virtual int totalseconds() => mins*60 + secs; // ******* public static bool operator < (time a, time b) { return a.totalseconds() < b.totalseconds(); //return (a.mins*60+a.secs) < (b.mins*60+b.secs); } // must define < and > in pairs public static bool operator > (time a, time b) { return b < a; } }//time class Time : time { // Time also has hours protected int hours; public Time(int h, int m, int s) : base(m,s) { hours = h + mins/60; mins = mins % 60; } public override string ToString() { // overrides object.ToString return hours+"h"+mins+"m"+secs+"s"; } public override int totalseconds() => hours*3600 + mins*60 + secs; // *** /* don't even need to define these anymore: they're inherited public static bool operator < (Time a, Time b) { return (a.hours*3600+a.mins*60+a.secs) < (b.hours*3600+b.mins*60+b.secs); } public static bool operator > (Time a, Time b) { return b < a; } */ }// Time public class csasnsol24 { // main class public static void Main() { // part I-1 C n = new C(); n.f(); n.g(); n.h1(); n.h2(); // part I-2 C2 n2 = new C2(); n2.f(); n2.g(); n2.h1(); n2.h2(); /////////////// Part II time a = new Time(2,30,20); // 2h30m20s time b = new Time(1,40,10); // 1h40m10s time c = new time(50,30); // 50m30s Console.WriteLine( a < b ); //?? true or false ?? Console.WriteLine( c < b ); // ?? is it looking at the hours ?? }//main }