3 What is the confusion, anyway?

Assume that we have the following class definition:

 
1class X 
2{ 
3  private
4    int i; 
5  protected
6    int j; 
7  public
8    int k; 
9};

Now, we find that altough class X is useful, it does not fully represent the attributes of a type of program objects. Assume that we need to represent a new data member, and call that m.

We have “two” ways to approach this problem. Let us consider the following definitions:

 
1class Y : public X 
2{ 
3  public
4    int m; 
5}
6 
7class Z 
8{ 
9  public
10    X c1; 
11    int m; 
12};

In both cases, class Y and class Z has the extra data member m. However, class Y inherits all the members of class X, whereas class Z has a class X member.

 3.1 Inheritance
 3.2 Composition