// CSC 123/252 C# Assignment Part 2 // C# allows "operator overloading." Overloading is resolved at compile // time, as the following program verifies: main prints True then False // when it should have printed False then true, because it called the // version of the overloads on static type "time" instead of // dynamic type "Time". // Your assignment: make these overloads use dynamic dispatch, so that // the overloads will apply the correct algorithm depending on the // dynamic type of a "time" object. The Main function should print // False then True. You may change both the time and Time classes but // YOU MAY NOT CHANGE Main!!! // There are elegant and inelegant ways to do this assignment. The less // code you write the better the solution. // compile your program in Windows with csc opoverload.cs, or on // Mono with mcs operoverload.cs, and run with mono operoverload.exe using System; 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"; } public static bool operator < (time a, time b) { 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 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 ///////////////////////////// DO NOT TOUCH THE FOLLOWING!!! public class opoverload { public static void Main() { 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 ?? } // DON'T TOUCH MAIN! }