Inheritance question

I was wondering what the difference is between simply using "include" and inheritance. For example, from I have read, one could do this,

 
class foo : public A { ... }; // could also be protected/private 


But you could also do

1
2
3
4
5
6

#include "A.h"

class foo { A something; ... }; // and then just use something to access 
                                // the objects inside class A


Public inheritance models the "IS-A" relationship of object-oriented design. Every foo can be used as A and fulfills all contracts of A.

Your "inclusion" models the composition relationship: every foo owns an A. It is part of foo's structure. This is actually somewhat similar to *private* inheritance, with the difference that private inheritance ("implemented-in-terms-of") gives access to protected and virtual members of A, while composition does not.
Last edited on
Topic archived. No new replies allowed.