Free mag vol1 | Page 284

CHAPTER 6  UNDERSTANDING INHERITANCE AND POLYMORPHISM } Now that you have established an “is-a” relationship, SalesPerson and Manager have automatically inherited all public members of the Employee base class. To illustrate, update your Main() method as follows: // Create a subclass object and access base class functionality. static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; Console.ReadLine(); } Controlling Base Class Creation with the base Keyword Currently, SalesPerson and Manager can only be created using the freebee default constructor (see Chapter 5). With this in mind, assume you have added a new six-argument constructor to the Manager type, which is invoked as follows: static void Main(string[] args) { ... // Assume Manager has a constructor matching this signature: // (string fullName, int age, int empID, // float currPay, string ssn, int numbOfOpts) Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); Console.ReadLine(); } If you look at the parameter list, you can clearly see that most of these arguments should be stored in the member variables defined by the Employee base class. To do so, you might implement this custom constructor on the Manager class as follows: public Manager(string fullName, int age, int empID, float currPay, string ssn, int numbOfOpts) { // This property is defined by the Manager class. StockOptions = numbOfOpts; // Assign incoming parameters using the // inherited properties of the parent class. ID = empID; Age = age; Name = fullName; Pay = currPay; // OOPS! This would be a compiler error, // if the SSN property were read-only! SocialSecurityNumber = ssn; 221