Online Book Reader

Home Category

Professional C__ - Marc Gregoire [110]

By Root 1265 0
if you omit the virtual keyword in the superclass.

Sub mySub;

Super& ref = mySub;

ref.someMethod(); // Calls Sub's version of someMethod()

Remember that even though a superclass reference or pointer knows that it is actually a subclass, you cannot access subclass methods or members that are not defined in the superclass. The following code will not compile because a Super reference does not have a method called someOtherMethod():

Sub mySub;

Super& ref = mySub;

mySub.someOtherMethod(); // This is fine.

ref.someOtherMethod(); // BUG

The subclass knowledge characteristic is not true of nonpointer nonreference objects. You can cast or assign a Sub to a Super because a Sub is a Super. However, the object will lose any knowledge of the subclass at this point:

Sub mySub;

Super assignedObject = mySub; // Assigns a Sub to a Super.

assignedObject.someMethod(); // Calls Super's version of someMethod()

One way to remember this seemingly strange behavior is to imagine what the objects look like in memory. Picture a Super object as a box taking up a certain amount of memory. A Sub object is a box that is a little bit bigger because it has everything a Super has plus a bit more. When you have a reference or pointer to a Sub, the box doesn’t change — you just have a new way of accessing it. However, when you cast a Sub into a Super, you are throwing out all the “uniqueness” of the Sub class to fit it into a smaller box.

Subclasses retain their overridden methods when referred to by superclass pointers or references. They lose their uniqueness when cast to a superclass object. The loss of overridden methods and subclass data is called slicing.

Preventing Overriding

C++11 allows you to mark a method as final which means that the method cannot be overridden in a subclass. Trying to override a final method will result in a compiler error. Take the following Super class:

class Super

{

public:

Super();

virtual void someMethod() final;

};

Trying to override someMethod(), as in the following Sub class, will result in a compiler error because someMethod() is marked as final in the Super class.

class Sub : public Super

{

public:

Sub();

virtual void someMethod(); // Error

virtual void someOtherMethod();

};

INHERITANCE FOR REUSE


Now that you are familiar with the basic syntax for inheritance, it’s time to explore one of the main reasons that inheritance is an important feature of the C++ language. Inheritance is a vehicle that allows you to leverage existing code. This section presents a real-world application of inheritance for the purpose of code reuse.

The WeatherPrediction Class

Imagine that you are given the task of writing a program to issue simple weather predictions, working with both Fahrenheit and Celsius. Weather predictions may be a little out of your area of expertise as a programmer, so you obtain a third-party class library that was written to make weather predictions based on the current temperature and the present distance between Jupiter and Mars (hey, it’s plausible). This third-party package is distributed as a compiled library to protect the intellectual property of the prediction algorithms, but you do get to see the class definition. The class definition for WeatherPrediction is as follows:

// Predicts the weather using proven new-age techniques given the current

// temperature and the distance from Jupiter to Mars. If these values are

// not provided, a guess is still given but it's only 99% accurate.

class WeatherPrediction

{

public:

// Sets the current temperature in fahrenheit

virtual void setCurrentTempFahrenheit(int inTemp);

// Sets the current distance between Jupiter and Mars

virtual void setPositionOfJupiter(int inDistanceFromMars);

// Gets the prediction for tomorrow's temperature

virtual int getTomorrowTempFahrenheit();

// Gets the probability of rain tomorrow. 1 means

// definite rain. 0 means no chance of rain.

virtual double getChanceOfRain();

// Displays the result to the user in this format:

// Result: x.xx chance. Temp. xx

virtual void showResult();

Return Main Page Previous Page Next Page

®Online Book Reader