Composition vs aggregation vs association summary

本文探讨了C++中对象间的关系类型,包括“part-of”(组成部分)、“has-a”(拥有)、“uses-a”(使用)、“depends-on”(依赖于)和“member-of”(成员)。通过实例说明了这些关系如何映射到现实生活中的概念,并讨论了它们在C++类设计中的应用。

Relationships between objects

There are many different kinds of relationships two objects may have in real-life, and we use specific “relation type” words to describe these relationships. For example: a square “is-a” shape. A car “has-a” steering wheel. A computer programmer “uses-a” keyboard. A flower “depends-on” a bee for pollination. A student is a “member-of” a class. And your brain exists as “part-of” you (at least, we can reasonably assume so if you’ve gotten this far).

All of these relation types have useful analogies in C++.

In this chapter, we’ll explore the nuances of the relation types “part-of”, “has-a”, “uses-a”, “depends-on”, and “member-of”, and show how they can be useful in the context of C++ classes. We’ll also explore a couple of related topics that don’t fit nicely anywhere else.

 

 

Composition vs aggregation vs association summary

Here’s a summary table to help you remember the difference between composition, aggregation, and association:

PropertyCompositionAggregationAssociation
Relationship typeWhole/partWhole/partOtherwise unrelated
Members can belong to multiple classesNoYesYes
Members existence managed by classYesNoNo
DirectionalityUnidirectionalUnidirectionalUnidirectional or bidirectional
Relationship verbPart-ofHas-aUses-a

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值