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.

346Part V: Optional FeaturesI haven’t created the hierarchy shown in Figure 26-2 after all. The inheritancehierarchy I have actually created is the one shown in Figure 26-4.FurnitureweightFurnitureweightBedSofasleep( )watchTV( )Figure 26-4:Actualresult of myfirst attempt.foldOut( )SleeperSofaThe MultipleInheritanceFactoring program demonstrates this duplication ofthe base class. Section 2 specifies exactly which weight object by recastingthe pointer SleeperSofa first to a Sofa* and then to a Furniture*.But SleeperSofa containing two Furniture objects is nonsense.SleeperSofa needs only one copy of Furniture. I want SleeperSofato inherit only one copy of Furniture, and I want Bed and Sofa to sharethat one copy. C++ calls this virtual inheritance because it uses the virtualkeyword.I hate this overloading of the term virtual because virtual inheritance hasnothing to do with virtual functions.Armed with this new knowledge, I return to class SleeperSofa and implementit as follows://// VirtualInheritance - using virtual inheritance the// Bed and Sofa classes can share a common base//#include

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

Saved successfully!

Ooh no, something went wrong!