Answers to Chapter 16 Review Questions

   


Chapter 16

1:

You have just started working on two software projects and identified the following classes in the two programs:

Program 1: Airplane, jet engines, wings, airplane body, passenger seats, and cockpit

Program 2: Person, student, employee, undergraduate, graduate, secretary, cleaner, and director

Which program seems able to benefit the most from inheritance? Why? Which concept is the other program likely to benefit from?

A:

Program 1 contains an airplane and the parts of an airplane. We can say airplane has-a jet engine and airplane has-a wing and so on. Consequently, it seems the aggregation concept will play an important role when implementing program 1.

In program 2, we find many is-a relationships. For example, student is-a person, employee is-a person, and secretary is-an employee. Consequently, the program seems to be able to benefit from the inheritance concept.

2:

Consider the following class hierarchy.


graphics/16infig04.gif

In which class would you include each of the following class members:

  1. The brandName instance variable

  2. The Autodial method

  3. The Start method

  4. The Tuning method

  5. The purchasePrice instance variable

A:
  1. ElectronicDevice

  2. MobilePhone

  3. ElectronicDevice

  4. Radio

  5. ElectronicDevice

3:

Suppose a class named Animal has a public method called Move. If the Dog class is derived from the Animal class, can you call the method Move for an instance of the Dog class as in myDog.Move();, even if you don't specify any such method in the Dog class?

A:

Yes, you can make the call myDog.Move(). The Move method is inherited by the Dog class from the Animal class.

4:

This is what the Move method header looks like in the Animal class:

 public void Move() 

Can you override this method in the Dog class? Why or why not?

A:

No, you cannot override this method. To enable this, Move must be declared virtual.

5:

Animal also contains a private instance variable called name. Can you access name from within the Dog class definition? Is this an advantage or a disadvantage?

A:

No, you cannot access name from within the Dog class definition. This is an advantage because it supports the encapsulation principle.

6:

Animal further contains a private function member. Can you call this function member from within the definition of the Dog class?

A:

No.

7:

Animal contains another method with the following header:

 protected virtual void MoveADistance(int distance) 

A fellow programmer has written the following method header in the derived Dog class to override Animal's method:

 public override int MoveADistance(double distance) 

This method header contains several mistakes. Locate and correct the mistakes.

A:

The following is the correct header:

 protected override void MoveADistance(int distance) 
8:

Can you prevent a class from acting as a base class? If so, how?

A:

Yes, by sealing it with the sealed keyword.

9:

Why does it usually make sense to call a base class constructor from a constructor of a derived class?

A:

A part of the derived class is made up of the base class. The base class constructors know how to initialize this part.

10:

Our Animal class from the previous questions has exactly one constructor that takes one parameter of type int. A fellow programmer has implemented the Dog class with a constructor that does not contain any explicitly defined constructor initializer. Why does this setup give rise to a compiler error?

A:

Any constructor of a derived class that does not contain an explicit constructor initializer automatically gets an implicit constructor initializer attached, which calls the default constructor of the base class. This happens, even if the default constructor of the base class has not been defined.

As soon as one constructor has been defined for a class, the default constructor is no longer supplied automatically. The base class in this case does not have a default constructor because it contains one explicitly defined constructor that takes one argument.

The combination of the two described scenarios causes the compiler error.

11:

The Animal class is also equipped with a complex method that returns the metabolic rate of a basic animal cell. Its method header in the Animal class looks like the following:

 public virtual double MetabolicRateCell() 

After the MetabolicRateCell for an Animal has been calculated, it is easy to calculate the MetabolicCellRate for a Dog; simply add 100 to this figure.

You need to override MetabolicRateCell() in the Dog class and write its implementation. Write the code for the overriding method in the Dog class so that you use the figure returned from the MetabolicRateCell method in the base class.

A:
 public override double MetabolicRateCell() {     return base.MetabolicRateCell() + 100; } 
12:

You now derive a class from the Dog class called Poodle. Does the Poodle class contain the method originally defined in Animal called Move?

A:

Yes. Class members are inherited across several inheritance levels.

Answers to Chapter 16 Programming Exercises

1:

Write four classes called ElectronicDevice, Radio, Computer, and MobilePhone. Let ElectronicDevice be the base class for the other three classes and include the following three class members in its definition:

  • A private instance variable called brandName (of type string)

  • A public property called BrandName to access brandName

  • A private instance variable called isOn (of type bool)

Also include two methods called SwitchOn (which must write "On" and set isOn to true) and SwitchOff (which must write "Off" and set isOn to false). The three subclasses remain empty for now.

Write the appropriate code to test that even though the three subclasses have empty definitions, you can utilize their inherited class members.

2:

Override the SwitchOn and SwitchOff methods in each of the subclasses so that they apart from the actions they perform in the ElectronicDevice also write the name of the device that is being switched on or off. For example when you call the SwitchOn method for Radio you should see the following output on the screen

 On 

Radio and isOn should still be assigned the value true. (Hint: Use the base access to achieve this functionality.)

3:

Equip the ElectronicDevice class with two constructors:

  • A default constructor that sets brandName to "unknown" and isOn to false.

  • A constructor that takes one argument of type string that is used to initialize brandName while isOn also is set to false.

Add the following instance variables to the program:

  • Add currentFrequency of type double to Radio.

  • Add internalMemory of type int to Computer.

  • Add lastNumberDialed of type uint to MobilePhone.

Add the following two constructors to each of the Radio, Computer and MobilePhone classes:

  • A default constructor that not only initializes the instance values defined in the particular class but also the instance variables inherited (brandName and isOn) from ElectronicDevice.

  • A constructor that takes two arguments one to set the value defined for the class (either currentFrequency, internalMemory, or lastNumberDialed) and one to set the value of brandName. (Hint: Call the appropriate constructor initializers to implement this.)

4:

Write a class called LaptopComputer that is derived from the Computer class. Define the instance variable maxBatteriLife of type uint for this class and provide two constructors:

  • One default constructor that assigns appropriate values to maxBatteriLife as well as the other class members LaptopComputer inherits.

  • One constructor with which you can pass initial values to maxBatteriLife as well as the class members LaptopComputer inherits from its descendants.

A:

When you have written the code for all the programming exercises, your code should look somewhat similar to what is shown next. You might want to add additional public properties to access private instance variables and expand the test code further than what is shown here.

 using System; class ElectronicDevice {     private string brandName;     private bool isOn;     public ElectronicDevice()     {         brandName = "unknown";         isOn = false;     }     public ElectronicDevice(string initBrandName)     {         brandName = initBrandName;         isOn = false;     }     public string BrandName     {         get         {             return brandName;         }         set         {             brandName = value;         }     }     public virtual void SwitchOn()     {         isOn = true;         Console.WriteLine("On");     }     public virtual void SwitchOff()     {         isOn = false;         Console.WriteLine("Off");     } } class Radio : ElectronicDevice {     private double currentFrequency;     public Radio() : base()     {         currentFrequency = 0;     }     public Radio(double initCurrentFrequency, string initBrandName) : base(initBrandName)     {         currentFrequency = initCurrentFrequency;     }     public override void SwitchOn()     {         base.SwitchOn();         Console.WriteLine("Radio");     }     public override void SwitchOff()     {         base.SwitchOff();         Console.WriteLine("Radio");     } } class Computer : ElectronicDevice {     private int internalMemory;     public Computer() : base()     {         internalMemory = 0;     }     public Computer(int initInternalMemory, string initBrandName) : base(initBrandName)     {         internalMemory = initInternalMemory;     }     public override void SwitchOn()     {         base.SwitchOn();         Console.WriteLine("Computer");     }     public override void SwitchOff()     {         base.SwitchOff();         Console.WriteLine("Computer");     } } class MobilePhone : ElectronicDevice {     private uint lastNumberDialled;     public MobilePhone() : base()     {         lastNumberDialled = 0;     }     public MobilePhone(uint initLastNumberDialled, string initBrandName) :  graphics/ccc.gifbase(initBrandName)     {         lastNumberDialled = initLastNumberDialled;     }     public override void SwitchOn()     {         base.SwitchOn();         Console.WriteLine("Mobile Phone");     }     public override void SwitchOff()     {         base.SwitchOff();         Console.WriteLine("Mobile Phone");     } } class LaptopComputer : Computer {     private uint maxBatteriLife;     public LaptopComputer() : base()     {         maxBatteriLife = 0;     }     public LaptopComputer(uint initMaxBatLife, int initInternalMemory, string  graphics/ccc.gifinitBrandName) : base(initInternalMemory, initBrandName)     {         maxBatteriLife = initMaxBatLife;     } } class Tester {     public static void Main()     {         Radio myRadio = new Radio(100, "Bang & Olufsen");         Console.WriteLine("BrandName: " + myRadio.BrandName);         LaptopComputer myLaptop = new LaptopComputer(12, 256, "IBM");         Console.WriteLine("BrandName: " + myLaptop.BrandName);     } } 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net