Test Your Knowledge: Exercises

Exercise 11-1. Create a base class, Telephone, and derive a class ElectronicPhone from it. In Telephone, create a protected string member phonetype and a public method Ring( ) which outputs a text message such as this: “Ringing the <phonetype>.” In ElectronicPhone, the constructor should set the phonetype to “Digital.” In the Run( ) method, call Ring( ) on the ElectronicPhone to test the inheritance.

Exercise 11-2. Extend Exercise 11-1 to illustrate a polymorphic method. Have the derived class override the Ring( ) method to display a different message.

Exercise 11-3. Change the Telephone class to abstract, and make Ring( ) an abstract method. Derive two new classes from Telephone: DigitalPhone and TalkingPhone. Each derived class should set the phonetype, and override the Ring( ) method.

Exercise 11-4. Phones these days do a lot more than ring, as you know. Add a method to DigitalPhone called VoiceMail( ) that outputs the message “You have a message. Press Play to retrieve.” Now add a new class, DigitalCellPhone, that derives from DigitalPhone and implements a version of VoiceMail( ) that outputs the message “You have a message. Call to retrieve.”

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.