22.3.1.  Multiple Inheritance Syntax

[ fromfile: multiple-inheritance.xml id: multinsimpleex ]

Figure 22.2.  Window and ScreenRegion

Window and ScreenRegion

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) {} 1

    Window( const Rectangle& rect, const ScreenRegion& sr)
        : Rectangle(rect), ScreenRegion(sr) {}        2
             
    // Other useful member functions ...
};

1

Use base class ctors.

2

Use base class copy ctors.


Example 22.8. src/multinheritance/window.cpp

#include "window.h"

int main() {
    Window w(Point(15,99), 50, 100, Color(22));
    w.show();            1
    w.move (Point(4,6)); 2
    return 0;
 }

1

Calls ScreenRegion::show();

2

Calls Rectangle::move();


Order of Member Initialization