Review Questions

   


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?

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

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?

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?

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?

6:

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

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.

8:

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

9:

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

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?

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.

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?


   


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

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