2 A simple case

Listing 1 is a simple case of multiple inheritance.


Listing 1:simple
 
1class A 
2{ 
3  public
4    int i; 
5}
6 
7class B 
8{ 
9  public
10    int j; 
11}
12 
13class C : public A, public B 
14{ 
15  public
16    int k; 
17}
18 
19void test(void
20{ 
21  C myC; 
22 
23  myC.i = 0; 
24  myC.j = 0; 
25  myC.k = 0; 
26}

Class C inherits from both class A and B. Because both inheritances are public, an object of class C has access to both i and j, which are data members defined in A and B, respectively.

Note that in the case of multiple inheritance, the keyword public needs to be applied to each superclass. Otherwise, the default private assumption is made.