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?
Consider the following class hierarchy.
In which class would you include each of the following class members:
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?
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?
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?
Animal further contains a private function member. Can you call this function member from within the definition of the Dog class?
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.
Can you prevent a class from acting as a base class? If so, how?
Why does it usually make sense to call a base class constructor from a constructor of a derived class?
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?
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.
You now derive a class from the Dog class called Poodle. Does the Poodle class contain the method originally defined in Animal called Move?