11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 26: Inheriting Multiple Inheritance 341int main(int nNumberofArgs, char* pszArgs[]){SleeperSofa ss;// you can watch TV on a sleeper sofa like a sofa...ss.watchTV(); // Sofa::watchTV()//...and then you can fold it out...ss.foldOut(); // SleeperSofa::foldOut()// ...and sleep on itss.sleep();}// wait until user is ready before terminating program// to allow the user to see the program resultssystem(“PAUSE”);return 0;Here the class SleeperSofa inherits from both Bed and Sofa. This isapparent from the appearance of both classes in the class declaration.SleeperSofa inherits all the members of both base classes. Thus, both of thecalls ss.sleep() and ss.watchTV() are legal. You can use a SleeperSofa asa Bed or a Sofa. Plus the class SleeperSofa can have members of its own,such as foldOut(). The output of this program appears as follows:Watch TVFold outSleepPress any key to continue . . .Is this a great country or what?Straightening Out InheritanceAmbiguitiesAlthough multiple inheritance is a powerful feature, it introduces several possibleproblems. One is apparent in the preceding example. Notice that both Bedand Sofa contain a member weight. This is logical because both have a measurableweight. The question is, “Which weight does SleeperSofa inherit?”The answer is “both.” SleeperSofa inherits a member Bed::weight and aseparate member Sofa::weight. Because they have the same name, unqualifiedreferences to weight are now ambiguous. This is demonstrated in thefollowing snippet:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!