[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppComposition

(C++) Composition

Make a class a member of another class to mimic a 'HAS-A' relationship between the two classes. To model a 'IS-A' relations use inheritance.

Differs from  ?aggregation that the members of the composition will be destroyed if the composition is destroyed. In an  ?aggregation, the members can remain in memory.

For example: making the class Engine a member of the class Car.

Prefer composition to inheritance [1].

Reference

1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 34: 'Prefer composition to inheritance'.

last edited (November 15, 2006) by bilderbikkel, Number of views: 1716, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.