/* ------- The Story of a Hockey Team with a Quarterback ------- */ /* What Mother Never Told You About C++ Part II: Type Casting and OOP */ using namespace std; #include #include class team // super class for teams { public: string name; // name of team }; class hockeyteam : public team { public: string city; string goalie; hockeyteam(string s, string c, string g) {name=s; city = c; goalie=g;} }; class footballteam : public team { public: string quarterback; footballteam(string s, string q) {name=s; quarterback=s;} }; int main(int argc, char** args) { team *A; // a variable with static type "team". if (argc>1) A = new footballteam("NY Jets","mark whatever"); else A = new hockeyteam("NY Islanders","Uniondale","good goalie"); cout << "The quarterback for the " << A->name << " is " << ((footballteam*)A)->quarterback << endl; return 0; } /* Output (with no command-line argument) The quarterback for the NY Islanders is Uniondale What is the moral of this "story"? A hockey team can have a quarterback? A city name can be used for the name of a person? This program uses the same capability of unrestricted type-casting that can be very useful, even critical, in systems-level programming languages (namely C). However, here we are not dealing with low-level binary data but rather with ABSTRACT DATA TYPES. When applications programmers think about these data types, they will likely think of what they correspond to in the real world as opposed to their byte-by-byte representation in memory. If a type (class) represents some objects with a well-understood semantics, is it legal to simply cast it into a different type with a completely different semantics? We are nolonger talking about whether a 4-byte integer can be converted to an array of 4 chars, but whether it is legitimate to treat a hockey team as a football team!. We can adopt not a "restricted" but a completely different *meaning* of type-casting in an oop language. This is the approach of recent oop languages such as Java/C# (and Visual C++ .Net). Type casting is still allowed between classes (if one inherits from the other), but for the purpose of *polymorphism*, not low-level data manipulation. Your assignment is to convert, as closely as possible, this code into C#. Don't jump to conclusions. You may find that the behavior of the C# program is more subtle than you think. Are there ways for even C# to improve? Think think think! This assignment is optional but the issues covered will be on the exam, so this is an excellent practice problem. In other words, you'd be silly not to do it. */