using System; // superclass public class team { protected int wins, losses; public team(int w, int l) {wins=w; losses=l;} // public int wins() { return wins; } // accessor (get) // public void wins(int x) { wins = x; } // modifier (set) public int W // property replaces 2 methods above { get { return wins; } // set{ if (value>=0) wins = value; // "value" is keyword // } } // can now call get and set with just a.W and a.W=... public void win() { wins++; } public void lose() { losses++; } public virtual double wp() { return ((double)wins)/(wins+losses); } } // class team public class hockeyteam : team { private int ties; public hockeyteam(int w, int l, int t) : base(w,l) { ties = t; } public override double wp() { return (wins+(ties/2.0))/(wins+losses+ties); } public void tie() { ties++; } } // note: can have more than one public class in file public class hkteam { public static void Main() { team jets = new team(5,0); hockeyteam rangers = new hockeyteam(3,3,1); jets.lose(); rangers.tie(); Console.WriteLine("jets' wp is "+jets.wp()); Console.WriteLine("rangers' wp is "+rangers.wp()); // a "polymorphic" array of teams: team[] NY = new team[4]; NY[0] = new team(0,1); NY[1] = new team(2,3); NY[2] = new hockeyteam(2,4,1); NY[3] = new team(2,2); // NY[0].tie() - not valid, compile time error //((hockeyteam)NY[0].tie(); // compiles but crashes // NY[2].tie() - still not valid, static type = team ((hockeyteam)NY[2]).tie(); // valid printwp(NY); } // prints winning percentage of every team: public static void printwp(team[] T) // note foreach loop { foreach (team x in T) {Console.WriteLine(x.wp());} } } // hkteam