[ fromfile: multiple-inheritance.xml id: multinsimpleex ]
The example in this section demonstrates multiple inheritance syntax and usage.
The two base classes shown in Figure 22.2, Rectangle
and ScreenRegion
, each have particular roles to play on the screen.
One class is concerned with shape and location, whereas the other is concerned with color and visibility
characteristics.
A Window
must be a Rectangle
and a ScreenRegion
.
They are defined in Example 22.7.
Example 22.7. src/multinheritance/window.h
[ . . . . ] class Rectangle { public: Rectangle( Const Point& ul, int length, int width); Rectangle( const Rectangle& r) ; void move (const Point &newpoint); private: Point m_UpperLeft; int m_Length, m_Width; }; class ScreenRegion { public: ScreenRegion( Color c=White); ScreenRegion (const ScreenRegion& sr); virtual color Fill( Color newColor) ; void show(); void hide(); private: Color m_Color; // other members... }; class Window: public Rectangle, public ScreenRegion { public: Window( const Point& ul, int len, int wid, Color c) : Rectangle(ul, len, wid), ScreenRegion(c) {} Window( const Rectangle& rect, const ScreenRegion& sr) : Rectangle(rect), ScreenRegion(sr) {} // Other useful member functions ... };
There are some syntax items in the classHead of Window
that deserve some attention.
An access specifier (public
or protected
) must
appear before each base class name if the derivation is not private.
Default derivation is private
It is possible to have a mixture of public, protected
, and private
derivations.
The comma (,
) character separates the base classes.
The order of base class initialization is the order in which the base classes are listed in the classHead.
Example 22.8 has some client code for the Window class.
Example 22.8. src/multinheritance/window.cpp
Default Initialization or assignment proceeds member-by-member in the order that data members are declared in the class definition: base classes first, followed by derived class members.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |